即使手动覆盖,ggplot2 图例也不匹配
我似乎无法让图例与我的图表相匹配。我已将形状和填充移至 aes 外部、aes 内部。手动覆盖时出现堆栈溢出,但似乎没有任何效果。
所以,我有四点(视觉上只出现三点:这对我的目的来说很好)。两个点是一个菱形,一个是绿色,一个是蓝色;然后两个是圆圈,一个是绿色的,另一个是蓝色的。所以我希望图例上有四个点充满颜色 - 所以两颗钻石:一颗绿色,一颗蓝色。然后是两个圆圈:一绿一蓝。
颜色和形状就在此图表上。有人可以帮忙解决一下传说吗? (我不想要线条的颜色,只想要形状和填充)。
可重现的代码
colours <- data.frame(purple = '#411D64',
blue = '#486C8B',
green = '#70B87B')
library(ggplot2)
library(latex2exp)
size_y_s0 <- data.frame(
name = 'Size Anchor',
shape = 'size0',
x = 0,
y = 1,
colour = colours$blue
)
size_y_sn <- data.frame(
name = 'Size Response',
shape = 'size*',
x = 0.4,
y = 0.9,
colour = colours$blue
)
income_x_s0 <- data.frame(
name = 'Income Anchor',
shape = 'income0',
x = 1,
y = 0,
colour = colours$green
)
income_x_sn <- data.frame(
name = 'Income Response',
shape = 'income*',
x = 0.9,
y = 0.4,
colour = colours$green
)
size_y_dcc <- data.frame(
name = "$D^{c}_{c}$",
x = c(0,0.4),
y = c(1,0.9),
colour = colours$blue
)
size_y_dcc_text <- data.frame(
name = "$D^{c}_{c}$",
text_x = 0.38,
text_y = 0.99,
angle = 352
)
size_y_dcp <- data.frame(
name = "$D^{c'}_{c}$",
x = c(0,0.9),
y = c(1,0.4),
colour = colours$green
)
size_y_dcp_text <- data.frame(
name = "$D^{c'}_{c}$",
text_x = 0.8,
text_y = 0.56,
angle = 340
)
data_plot1 <- list(
size_y_s0,
size_y_sn,
income_x_s0,
income_x_sn,
size_y_dcc,
size_y_dcp,
size_y_dcp_text,
size_y_dcc_text
)
ggplot() +
geom_line(data = data_plot1[[5]],
aes(x = x, y = y), colour = data_plot1[[5]]$colour, size = 5, show.legend = FALSE
) +
geom_line(data = data_plot1[[6]],
aes(x = x, y = y), colour = data_plot1[[6]]$colour, size = 5, show.legend = FALSE
) +
geom_point(data = data_plot1[[1]],
aes(x = x, y = y, fill = colour, shape = shape),fill = data_plot1[[1]]$colour, shape = 21, size = 5, show.legend = TRUE
) +
geom_point(data = data_plot1[[2]],
aes(x = x, y = y, fill = colour, shape = shape), fill = data_plot1[[2]]$colour, shape = 23, size = 5, show.legend = TRUE
) +
geom_point(data = data_plot1[[3]],
aes(x = x, y = y, fill = colour, shape = shape), fill = data_plot1[[3]]$colour, shape = 21, size = 5, show.legend = TRUE
) +
geom_point(data = data_plot1[[4]],
aes(x = x, y = y, fill = colour, shape = shape), fill = data_plot1[[4]]$colour, shape = 23, size = 5, show.legend = TRUE
) +
annotate(
geom = "text",
x = data_plot1[[7]]$text_x,
y = data_plot1[[7]]$text_y,
label = TeX(data_plot1[[7]]$name),
size = 15,
angle = data_plot1[[7]]$angle)+
annotate(
geom = "text",
x = data_plot1[[8]]$text_x,
y = data_plot1[[8]]$text_y,
label = TeX(data_plot1[[8]]$name),
size = 15,
angle = data_plot1[[8]]$angle) +
scale_fill_manual(
name = 'Legend',
guide = 'legend',
labels = c(
data_plot1[[1]]$name,
data_plot1[[2]]$name,
data_plot1[[3]]$name,
data_plot1[[4]]$name
),
values = c(
data_plot1[[1]]$colour,
data_plot1[[2]]$colour,
data_plot1[[3]]$colour,
data_plot1[[4]]$colour
)
) +
scale_shape_manual(
name = 'Legend',
guide = 'legend',
labels = c(
data_plot1[[1]]$name,
data_plot1[[2]]$name,
data_plot1[[3]]$name,
data_plot1[[4]]$name
),
values = c(
23,
21,
23,
21
)
)
I cannot seem to get the legend to match my graph. I have moved the shape and fill outside of the aes, inside the aes. Followed stack overflows on manual overrides and nothing seems to be working.
So, I have four points (visually only three appear: which is fine for my purposes). Two points are a diamond shape with one being green and the other being blue; then two are circles with one being green and the other being blue. So I want there to be four points on the legend filled with the color - so two diamonds: one greeen, one blue. And then two circles: one green and one blue.
The colors and shapes are right on this graph. Could someone please help with the legend bit. (I don't want the colors of the lines just the shapes and fill).
Reproducible code
colours <- data.frame(purple = '#411D64',
blue = '#486C8B',
green = '#70B87B')
library(ggplot2)
library(latex2exp)
size_y_s0 <- data.frame(
name = 'Size Anchor',
shape = 'size0',
x = 0,
y = 1,
colour = colours$blue
)
size_y_sn <- data.frame(
name = 'Size Response',
shape = 'size*',
x = 0.4,
y = 0.9,
colour = colours$blue
)
income_x_s0 <- data.frame(
name = 'Income Anchor',
shape = 'income0',
x = 1,
y = 0,
colour = colours$green
)
income_x_sn <- data.frame(
name = 'Income Response',
shape = 'income*',
x = 0.9,
y = 0.4,
colour = colours$green
)
size_y_dcc <- data.frame(
name = "$D^{c}_{c}quot;,
x = c(0,0.4),
y = c(1,0.9),
colour = colours$blue
)
size_y_dcc_text <- data.frame(
name = "$D^{c}_{c}quot;,
text_x = 0.38,
text_y = 0.99,
angle = 352
)
size_y_dcp <- data.frame(
name = "$D^{c'}_{c}quot;,
x = c(0,0.9),
y = c(1,0.4),
colour = colours$green
)
size_y_dcp_text <- data.frame(
name = "$D^{c'}_{c}quot;,
text_x = 0.8,
text_y = 0.56,
angle = 340
)
data_plot1 <- list(
size_y_s0,
size_y_sn,
income_x_s0,
income_x_sn,
size_y_dcc,
size_y_dcp,
size_y_dcp_text,
size_y_dcc_text
)
ggplot() +
geom_line(data = data_plot1[[5]],
aes(x = x, y = y), colour = data_plot1[[5]]$colour, size = 5, show.legend = FALSE
) +
geom_line(data = data_plot1[[6]],
aes(x = x, y = y), colour = data_plot1[[6]]$colour, size = 5, show.legend = FALSE
) +
geom_point(data = data_plot1[[1]],
aes(x = x, y = y, fill = colour, shape = shape),fill = data_plot1[[1]]$colour, shape = 21, size = 5, show.legend = TRUE
) +
geom_point(data = data_plot1[[2]],
aes(x = x, y = y, fill = colour, shape = shape), fill = data_plot1[[2]]$colour, shape = 23, size = 5, show.legend = TRUE
) +
geom_point(data = data_plot1[[3]],
aes(x = x, y = y, fill = colour, shape = shape), fill = data_plot1[[3]]$colour, shape = 21, size = 5, show.legend = TRUE
) +
geom_point(data = data_plot1[[4]],
aes(x = x, y = y, fill = colour, shape = shape), fill = data_plot1[[4]]$colour, shape = 23, size = 5, show.legend = TRUE
) +
annotate(
geom = "text",
x = data_plot1[[7]]$text_x,
y = data_plot1[[7]]$text_y,
label = TeX(data_plot1[[7]]$name),
size = 15,
angle = data_plot1[[7]]$angle)+
annotate(
geom = "text",
x = data_plot1[[8]]$text_x,
y = data_plot1[[8]]$text_y,
label = TeX(data_plot1[[8]]$name),
size = 15,
angle = data_plot1[[8]]$angle) +
scale_fill_manual(
name = 'Legend',
guide = 'legend',
labels = c(
data_plot1[[1]]$name,
data_plot1[[2]]$name,
data_plot1[[3]]$name,
data_plot1[[4]]$name
),
values = c(
data_plot1[[1]]$colour,
data_plot1[[2]]$colour,
data_plot1[[3]]$colour,
data_plot1[[4]]$colour
)
) +
scale_shape_manual(
name = 'Legend',
guide = 'legend',
labels = c(
data_plot1[[1]]$name,
data_plot1[[2]]$name,
data_plot1[[3]]$name,
data_plot1[[4]]$name
),
values = c(
23,
21,
23,
21
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为列表
data_plot1
用作数据帧列表不是绘制目的的好选择。您可以在几个数据帧(点,文本等)上拆分Geoms,并可以更好地控制AES。请参阅一个示例I think the use of the list
data_plot1
as a list of data frames is not a good choice for plotting purposes. You can split the geoms on several data frames (points, texts, etc) and have a better control of the aes. See an example我没有足够的代表将其作为评论,但也许这有帮助:
在我看来,您的传奇甚至没有出现在此代码示例中。这可能是因为GGPLOT仅为美学映射创建传说(在AES函数内)。但是,您的geom_point()每一个都只使用一个数据框架,其中一个行和变量级别。这是因为Geoms不会互相传达其数据,因为它是在每个Geom中本地定义的。您可以考虑一种将所有数据帧绑定到兼容“长”格式(例如使用Pivot_longer()和bind_rows())的方法,因此您只需要一个geom_line()和一个geom_point()和一个是映射颜色和绘制颜色和绘制颜色的geom_point()填写数据中的某种组。
您想要提供GGPLOT的数据形状,以表现出您的期望,即以下样式:
I dont have enough rep to put this as a comment, but maybe this one helps:
It seems to me that your legend is not even appearing for this code example. This might be because ggplot only creates legends for aesthetic mappings (within the aes function). However every of your geom_point() does only use one data frame with one row and variable level. This is because the geoms do not communicate their data with eachother, since it is defined locally within each geom. You could think about a way to bind all your data frames into a ggplot compatible 'long' format (e.g. with pivot_longer() and bind_rows()), so you only need one geom_line() and one geom_point() which is mapping colour and fill over some kind of group within your data.
The shape of the data you want to provide ggplot to behave as you expect it is following style: