在 ggplot2 中自定义图例文本和颜色
我正在尝试绘制一个图(使用ggplot)来绘制geom_point
和geom_line
,其中不同的变量以不同的颜色绘制(根据scale_colour_manual(value = color) )
,其中 color
是自定义颜色数组)以及两条水平黑线 (geom_hline
)。
当我尝试自定义水平线的图例文本时,我的麻烦出现了。看来我只能选择两者之一:
- 水平线的颜色为黑色,但该行的图例文本不正确
- 该行的图例文本是正确的,但水平线的颜色由上述 < 确定代码>scale_colour_manual颜色。
plot <- ggplot(data, aes(x = factor(Month), y = avgLoss, colour = type, order = -as.numeric(type)))
+ geom_line() + geom_point()
meanplus2sd <- mean(data$avgLoss) + 2*sd(data$avgLoss)
plot <- plot + geom_hline(aes(yintercept = meanplus2sd), colour = "black")
生成图例中显示“黑色”的黑线
plot <- plot + geom_hline(aes(yintercept = meanplus2sd, colour = "Mean + 2 Stdev."))
生成一条线,该线是我定义的 scale_colour_manual
数组中的下一个颜色,但图例文本是“Mean + 2 Stdev”。
除了标准的 geom_point + geom_line
绘图之外,任何有关获取水平线的自定义颜色和图例文本的帮助都会非常好。谢谢。
I am trying to get a plot (using ggplot) that will plot geom_point
and geom_line
where different variables are plotted in different colors (according to scale_colour_manual(value = color)
where color
is a custom array of colors) in addition to two horizontal black lines (geom_hline
).
My trouble arises when I attempt to get the legend text for the horizontal lines customized. It appears that I can have only one of the two:
- the color of the horizontal line as black but the legend text for this line is incorrect
- the legend text for this line is correct but the color of the horizontal line is determined by the aforementioned
scale_colour_manual
color.
plot <- ggplot(data, aes(x = factor(Month), y = avgLoss, colour = type, order = -as.numeric(type)))
+ geom_line() + geom_point()
meanplus2sd <- mean(data$avgLoss) + 2*sd(data$avgLoss)
plot <- plot + geom_hline(aes(yintercept = meanplus2sd), colour = "black")
produces black line that says "black" in the legend
plot <- plot + geom_hline(aes(yintercept = meanplus2sd, colour = "Mean + 2 Stdev."))
produces a line that is the next color in my defined scale_colour_manual
array, but the legend text is "Mean + 2 Stdev."
Any help in getting both custom color and legend text for a horizontal line in addition to the standard geom_point + geom_line
plotting would be excellent. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获得所需内容的一种方法是将水平黑线的数据包含在原始数据框中。您的示例并不完全可重现,因此这可能不完全是您想要的代码,但这是总体思路:
将行添加到“数据”,每个“月”级别一个,其中“avgLoss”的值在每行等于“meanplus2sd”。您可能希望所有其他列都包含 NA。所以,
这应该可以帮助你开始,你只需要确保颜色“黑色”位于scale_colour_manual中的正确位置,这可能需要一些调整。
One way to get what you want would be to include the data for your horizontal black line in the original data frame. You example isn't exactly reproducible, so this may not be exactly the code you want, but this is the general idea:
Add rows to 'data', one for each level of 'Month', where the value of 'avgLoss' in each row is equal to 'meanplus2sd'. You'll probably want all the other columns to contain NAs. So,
That should get you started, you'll just have to make sure that the color "black" is in the right position in scale_colour_manual, which may take some fiddling.
注释可能是一个不错的选择
Annotate might be a good option