问题描述
我知道,这个问题已经被问过很多次了,但是我仍然找不到一个好的答案.我想得到一个漂亮的图例,该图例在绘图中的单独图例中显示手动添加的线条.到目前为止,这是我已经弄清楚的:
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指南图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!