[英]Legend linetype in ggplot
I am pretty much new in R environment, please excuse me if I am asking something obvious. 我在R环境中非常陌生,如果我要提出明显的要求,请原谅。 I need the legend in my plot to reflect the color and linetype in the graph but I cannot figure out how I need to change my code.
我需要绘图中的图例以反映图形中的颜色和线型,但是我无法弄清楚需要如何更改代码。 I greatly appreciate your help.
非常感谢您的帮助。
tibble::tribble(
~variable, ~value, ~bin, ~ci,
"Advanced HS", 302.097396142857, "FFD", 21.2978862705524,
"Beginner HS", 394.264259996296, "FFD", 28.8027249069651,
"Children", 379.642674397368, "FFD", 18.1482664147856,
"Adults", 217.875580083333, "FFD", 2.38006646944708,
"Advanced HS", 305.244904223809, "SFD", 15.5487255740993,
"Beginner HS", 350.108404085185, "SFD", 28.6379128865829,
"Children", 358.329722218421, "SFD", 12.6900032541364,
"Adults", 228.522062813542, "SFD", 2.73230976574123,
"Advanced HS", 484.181295019048, "GD", 33.9933197272878,
"Beginner HS", 960.745730144444, "GD", 55.8118108393556,
"Children", 676.158669963158, "GD", 44.238558449867,
"Adults", 259.127882341667, "GD", 4.30648424130776,
"Advanced HS", 702.136573061905, "TT", 47.0883793662974,
"Beginner HS", 1541.69112013704, "TT", 84.3091269009313,
"Children", 976.54035988421, "TT", 60.0584639291095,
"Adults", 318.08245218125, "TT", 7.99092674935576
)
mytheme <- theme_bw() + theme(axis.title = element_text(size = rel(2)),
strip.text = element_text(size = rel(2)),
legend.position = "bottom",
legend.key = element_blank(),
text = element_text(size = 28),
legend.text = element_text(size = 28),
plot.margin = margin(20, 20, 20, 20),
axis.text.x = element_text(hjust = 0.5, vjust = 0.5, size = 35),
axis.text.y = element_text(hjust = 0.5, vjust = 0.5, size = 35),
strip.background = element_rect(fill = 'gray96'))
tiff("r.tiff", units="in", width=10, height=7, res=300)
ggplot(data = Figure_1) +
mytheme + scale_y_continuous(breaks = seq(0,2000, 500))+
aes(x = bin, y = value, group = variable, color = variable) + scale_x_discrete(limits = c ("FFD","SFD", "GD", "TT")) +
geom_errorbar(aes(ymin=value-ci, ymax=value+ci), width=.1) +
geom_line(aes(group=variable, linetype = variable), size =2) + scale_linetype_manual(values=c("solid", "longdash", "dotdash", "dotted")) +
theme(legend.title = element_blank()) +
guides(col = guide_legend(reverse = FALSE, override.aes = list(size=6))) +
xlab("") +
ylab("Duration (ms)")
dev.off()
Your linetypes not showing up in the legend is some combination of 您未在图例中显示的线型是以下几种组合
geom_errorbar
layer is using linetype='solid' which is over-drawing or over-writing (I am not sure which) the linetype from the geom_line
layer geom_errorbar
图层使用的是geom_errorbar
='solid',它覆盖或覆盖了geom_line
图层的线型(我不确定是哪一个) To fix (1), use show.legend=FALSE
in geom_errobar
要修复(1),请在
geom_errobar
使用show.legend=FALSE
geom_errorbar(aes(ymin=value-ci, ymax=value+ci), width=.1, show.legend=FALSE)
At this point, this will make no apparent difference to the resulting image because of the second issue (2): the lines are so big in the legend/the space to display the pattern is so small that even one dot of the purple dotted line consumes the entire space allocated to it (no space to show a second dot). 此时,由于第二个问题(2),这不会对结果图像产生明显的影响:图例中的线条太大/显示图案的空间很小,即使紫色虚线的一个点也是如此消耗分配给它的整个空间(没有空间显示第二个点)。
To fix this there are a few options which you will have to decide between based on aesthetics: 要解决此问题,您必须根据美观情况在一些选项之间进行选择:
guides
line to have size=1
instead of size=6
you will at least see the dotted and dot-dash pattern in the legend, but still won't see a difference between the dash-dash and solid linetypes because again the space to show the line is not big enough to show the pattern. guides
线更改为size=1
而不是size=6
,则至少会在图例中看到虚线和点划线模式,但在虚线和实线类型之间仍然看不到区别,因为同样,显示线条的空间不足以显示图案。 (plus given the scale of everything else in the graphic I suspect size=1
would be unacceptably small in the finished graph). size=1
在完成的图形中会小得无法接受)。 You can + theme(legend.key.width=units(5, 'lines'))
in combo with guides(..., size=2)
to make the bit of the legend that shows the lines wider (while still having thick enough lines to see), in order to show enough of a repeat of the pattern, but this pushes the last element of the legend off the size of your image (shown below) so you might need to increase the image size (or reduce the legend font size) smaller to fit. 您可以
+ theme(legend.key.width=units(5, 'lines'))
与guides(..., size=2)
以使图例的显示线条更宽(同时仍然很粗)足以看到图案的重复),但这会使图例的最后一个元素偏离图像的大小(如下所示),因此您可能需要增加图像的大小(或减小图像的大小)。图例字体大小)较小以适合。 NB default legend.key.width
is unit(1.2, 'lines')
in theme_bw()
so I chose the 5 by trial and error. 注意默认的
legend.key.width
在theme_bw()
是unit(1.2, 'lines')
,所以我通过反复试验选择了5。
guides(lty = guide_legend(reverse = FALSE, override.aes = list(size=2), byrow=TRUE, ncol=2))
to get the legend to wrap onto two lines, though then perhaps you should stick with the original vertically-oriented legend on the right-hand side of the graph. guides(lty = guide_legend(reverse = FALSE, override.aes = list(size=2), byrow=TRUE, ncol=2))
将图例包裹在两行上,尽管那样也许您应该坚持图表右侧的原始垂直图例。 Note I had to change col=
to lty=
(they are a combined legend anyway) col=
更改为lty=
(无论如何它们都是组合图例) as a general observation, for this specific figure, it looks like the colour already identifies the lines so perhaps it is acceptable to only show the colour on the legend and omit the linetype (though then when printing black and white it will be difficult to distinguish them which may be the reason you put the linetype in in the first place) 作为一般观察,对于该特定图形,看起来颜色已经标识了线条,因此仅在图例上显示颜色并省略线型也许是可以接受的(尽管那样在打印黑白时将很难区分它们,这可能是您将线型放在首位的原因)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.