我有下面的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")我尝试通过添加scale_color_manual手动添加图例。但是,图例填充颜色与geom_area图层的颜色不匹配。如何手动配置此填充颜色?
发布于 2019-03-16 18:58:43
您已经很接近了,但是您的代码需要进行一些额外的调整:
还要指定fill-color中的aes-call。然后可以使用scale_fill_manual指定要填充的颜色。由于填充和外部边界具有相同的颜色,因此可以指定aesthetics = c("color", "fill")将其应用于这两种美学。
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")

https://stackoverflow.com/questions/55195404
复制相似问题