简体   繁体   English

在 R 中使用 geom_smooth() 在 ggplot2 图例中混合填充颜色

[英]Mixed fill color in ggplot2 legend using geom_smooth() in R

When plotting two regression curves using geom_smooth() in ggplot2 , for the fill color, the legend picks the one where the confidence intervals intersect.ggplot2使用geom_smooth()绘制两条回归曲线时,对于fill颜色,图例会选择置信区间相交的曲线。 I do think this behaviour arises when the overlapping area is proportionally bigger that the other, however I find this quite undesired because the reader is capable of deducing that the "darkened" area is the one where the CI intersect.我确实认为当重叠区域按比例大于另一个时会出现这种行为,但是我发现这很不受欢迎,因为读者能够推断出“变暗”区域是 CI 相交的区域。 It is IMHO a bit harder or unintuitive to assign the same color for both the curves.恕我直言,为两条曲线指定相同的颜色有点困难或不直观。

How can I correct this ?我该如何纠正?

MWE: MWE:

library(ggplot2)

p <- ggplot(data=iris, aes(x=Sepal.Width, y=Sepal.Length)) + geom_point()
p <- p + geom_smooth(method=loess, aes(colour="Loess"), fill="yellow")
p <- p + geom_smooth(method=lm, aes(colour="LM"))

print(p)

Output:输出:

此处使用的数据和颜色仅用于说明目的

You can add the fill as an aesthetic mapping, ensuring you name it the same as the color mapping to get legends to merge:您可以将填充添加为美学映射,确保将其命名为与颜色映射相同的名称以合并图例:

library(ggplot2)

ggplot(data=iris, aes(x=Sepal.Width, y=Sepal.Length)) +
  geom_point(aes(shape = "data")) +
  geom_smooth(method=loess, aes(colour="Loess", fill="Loess")) +
  geom_smooth(method=lm, aes(colour="LM", fill = "LM")) +
  scale_fill_manual(values = c("yellow", "gray"), name = "model")  +
  scale_colour_manual(values = c("red", "blue"), name = "model") +
  labs(shape = "")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM