包括手动添加的行到ggplot2指南图例

编程入门 行业动态 更新时间:2024-10-28 12:16:46
本文介绍了包括手动添加的行到ggplot2指南图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道,这个问题已经被问过很多次了,但是我仍然找不到一个好的答案.我想得到一个漂亮的图例,该图例在绘图中的单独图例中显示手动添加的线条.到目前为止,这是我已经弄清楚的:

This is question that's been asked many times, I know, but I still can't figure out a good answer. I would like to get a pretty legend that shows manually-added lines in a separate legend on the plot. Here's what I've figured out so far:

library(ggplot2) data(mtcars) ggplot(mtcars, aes(x=disp, y=mpg, color=factor(am))) + theme_bw() + geom_point() + geom_smooth(method = 'lm', se=FALSE) + geom_abline(aes(intercept=40, slope = (-1/10))) + geom_abline(aes(intercept=25, slope = (-1/30)))

给予:

具有手动添加的行,但没有图例条目.

which has manually-added lines, but no legend entry for them.

仅添加show.legend=TRUE并不是很有帮助:

Just adding show.legend=TRUE isn't very helpful:

ggplot(mtcars, aes(x=disp, y=mpg, color=factor(am))) + theme_bw() + geom_point() + geom_smooth(method = 'lm', se=FALSE) + geom_abline(aes(intercept=40, slope = (-1/10)), show.legend = TRUE) + geom_abline(aes(intercept=25, slope = (-1/30)), show.legend = TRUE)

为每行添加一个人工fill也不是很有帮助,

Adding an artificial fill for each additional line isn't very helpful, either:

ggplot(mtcars, aes(x=disp, y=mpg, color=factor(am))) + theme_bw() + geom_point() + geom_smooth(method = 'lm', se=FALSE) + geom_abline(aes(intercept=40, slope = (-1/10), fill='Comparison Line 1')) + geom_abline(aes(intercept=25, slope = (-1/30), fill='Comparison Line 2'))

它只是发出警告并返回原始图:

it just gives a warning and returns the original plot:

Warning: Ignoring unknown aesthetics: fill Warning: Ignoring unknown aesthetics: fill

同时添加show.legend=TRUE和假aes fill的操作很接近,但是结果非常难看:

Adding both show.legend=TRUE and a fake aes fill gets close, but the result is very ugly:

ggplot(mtcars, aes(x=disp, y=mpg, color=factor(am))) + theme_bw() + geom_point() + geom_smooth(method = 'lm', se=FALSE) + geom_abline(aes(intercept=40, slope = (-1/10), fill='Comparison Line 1'), show.legend = TRUE) + geom_abline(aes(intercept=25, slope = (-1/30), fill='Comparison Line 2'), show.legend = TRUE)

最后,我的问题: 如何消除颜色图例(标题为"factor(am)")中的对角线,以及如何使填充图例中的项旁边的外观看起来正常(标题为"fill")?

Finally, my question: How do I get rid of the diagonal lines in the color legend (titled "factor(am)"), and how do I get normal-looking lines next to the items in the fill legend (titled "fill")?

推荐答案

您非常亲密! 将与geom_abline相关的伪变量(例如size)添加到aes().然后使用scale_size_manual缩放size.

You were very close! Add dummy variable that is relevant to geom_abline for example size to aes(). And scale size back using scale_size_manual.

library(ggplot2) ggplot(mtcars, aes(x=disp, y=mpg, color=factor(am))) + theme_bw() + geom_point() + geom_smooth(method = 'lm', se=FALSE) + geom_abline(aes(intercept=40, slope = (-1/10), size='Comparison Line 1')) + geom_abline(aes(intercept=25, slope = (-1/30), size='Comparison Line 2')) + scale_size_manual(values = c(0.3, 0.3))

PS .:您正在使用的填充物对abline的美学效果未知(如ggplot2警告您:Warning: Ignoring unknown aesthetics: fill).

PS.: Fill that you were using is unknown aesthetics for the abline (as ggplot2 warns you: Warning: Ignoring unknown aesthetics: fill).

更多推荐

包括手动添加的行到ggplot2指南图例

本文发布于:2023-10-31 00:44:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544491.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图例   指南

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!