简体   繁体   中英

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. 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. It is IMHO a bit harder or unintuitive to assign the same color for both the curves.

How can I correct this ?

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 = "")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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