使用R中的geom_smooth()在ggplot2图例中混合填充颜色 [英] Mixed fill color in ggplot2 legend using geom_smooth() in R

查看:167
本文介绍了使用R中的geom_smooth()在ggplot2图例中混合填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 ggplot2 中的 geom_smooth()绘制两条回归曲线时,对于 fill 颜色,图例会选择置信区间为1的图例.相交.我确实认为这种行为是在重叠区域成比例地大于另一个区域时发生的,但是我发现这是非常不希望的,因为读者能够推断出变暗"的区域.区域是CI相交的区域.恕我直言,为两条曲线分配相同的颜色有点困难或不直观.

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.

我该如何纠正?

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)

输出:

推荐答案

您可以将填充添加为美观映射,以确保将其命名为与颜色映射相同的名称,以使图例得以合并:

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

这篇关于使用R中的geom_smooth()在ggplot2图例中混合填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆