首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅使用geom_areas更改ggplot2中的图例颜色

仅使用geom_areas更改ggplot2中的图例颜色
EN

Stack Overflow用户
提问于 2019-03-16 17:46:50
回答 1查看 171关注 0票数 2

我有下面的ggplot对象:

代码语言:javascript
复制
ggplot(data.frame(x = c(3, 15)), aes(x)) + 
  geom_area(aes(color = "Gruppe A"), stat = "function", fun = dnorm, args = list(mean = 10, sd = 2), fill = "blue", alpha=.5, xlim = c(3, 15)) +
  geom_area(aes(color = "Gruppe B"), stat = "function", fun = dnorm, args = list(mean = 9, sd = 2), fill = "red", alpha=.5, xlim = c(3, 15)) +
  scale_fill_discrete(name = "Name", labels = c("Gruppe A", "Gruppe B")) +
  scale_color_manual(values=c("Gruppe A"="blue", "Gruppe B"="red")) +
  scale_x_continuous(name = "Note", breaks = c(3:15), limits = c(3, 15)) + 
  scale_y_continuous(name = "Dichte")

我尝试通过添加scale_color_manual手动添加图例。但是,图例填充颜色与geom_area图层的颜色不匹配。如何手动配置此填充颜色?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-16 18:58:43

您已经很接近了,但是您的代码需要进行一些额外的调整:

还要指定fill-color中的aes-call。然后可以使用scale_fill_manual指定要填充的颜色。由于填充和外部边界具有相同的颜色,因此可以指定aesthetics = c("color", "fill")将其应用于这两种美学。

代码语言:javascript
复制
ggplot(data.frame(x = c(3, 15)), aes(x)) + 
  geom_area(aes(color = "Gruppe A", fill = "Gruppe A"), stat = "function", fun = dnorm, args = list(mean = 10, sd = 2), alpha=.5, xlim = c(3, 15)) +
  geom_area(aes(color = "Gruppe B", fill = "Gruppe B"), stat = "function", fun = dnorm, args = list(mean = 9, sd = 2), alpha=.5, xlim = c(3, 15)) +
  scale_fill_manual(name = "Name", values = c("Gruppe A" = "blue", "Gruppe B" = "red"), aesthetics = c("color", "fill")) +
  scale_x_continuous(name = "Note", breaks = c(3:15), limits = c(3, 15)) + 
  scale_y_continuous(name = "Dichte")

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55195404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档