Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 仅使用geom_区域更改ggplot2中的图例颜色_R_Ggplot2 - Fatal编程技术网

R 仅使用geom_区域更改ggplot2中的图例颜色

R 仅使用geom_区域更改ggplot2中的图例颜色,r,ggplot2,R,Ggplot2,我有以下ggplot对象: 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 = "fu

我有以下ggplot对象:

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

我试图通过添加比例\颜色\手动添加图例。但是,图例填充颜色与geom_区域图层的颜色不匹配。如何手动配置此填充颜色?

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

aes
-调用中还指定
fill
-颜色。然后,您可以使用
scale\u fill\u manual
指定填充颜色。由于填充和外部边界的颜色相同,您可以指定
美学=c(“颜色”、“填充”)
将其应用于两种美学

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

谢谢您的回复。这很有效。是否有一些方法也可以将alpha添加到图例填充中?是的,您可以例如添加
指南(fill=guide\u legend(override.aes=list(alpha=0.25))