根据组调整ggplot中geom_vline的颜色
因此,我正在为两种虹膜数据的sepal.engength创建密度图。图形如下:
如您所见,我在图表中添加了两个中心趋势的度量(中值和均值)。但是,目前它们会根据度量(即中位数=黑色,平均=灰色)进行颜色。
我想更改颜色,以便setosa/versicolor的含义和中位数具有相同的颜色。此外,我想为setosa/versicolor的平均值/中位数添加一个指示颜色和线型的传奇。因此,最后,我将拥有一个四个部分的传奇:中值setosa,平均setosa,中值versicolor,平均versiocolor。
有人知道该怎么做吗?请参阅下面的复制代码:
library(ggplot2)
library(tidyverse)
iris <- iris %>%
filter(Species == c("setosa", "versicolor"))
temp <- iris %>%
group_by(Species) %>%
dplyr::summarize(Mean = mean(Sepal.Length, na.rm=TRUE))
temp_2 <- iris %>%
group_by(Species) %>%
dplyr::summarize(Median = median(Sepal.Length, na.rm=TRUE))
ggplot(iris, aes(x = Sepal.Length, fill = factor(Species))) +
geom_density(alpha = 0.5) +
theme_minimal() +
geom_vline(data=temp, aes(xintercept=Mean, color="Mean"),
linetype="dotted",
lwd=1) +
geom_vline(data=temp_2, aes(xintercept=Median, color="Median"),
linetype="dashed",
lwd=1) +
scale_color_manual(name = "Statistics", values = c(Median = "black", Mean = "grey50")) +
labs(title = "Distribution of Sepal.Length",
x = "Sepal.Length",
y = "Density",
fill = "Species")
So I am creating density plot for Sepal.Length for two species of the iris-dataset. The graph looks like this:
As you can see, I added two measures of central tendency to the graph (Median and Mean). However, right now they are colored depending on the measure (i.e., Median = black, Mean = grey).
I want to change the colors so that mean and median for Setosa/Versicolor have the same color. Furthermore, I want to add a legend indicating color and linetype for mean/median of setosa/versicolor. So in the end I would have a legend with four parts: Median Setosa, Mean Setosa, Median Versicolor, Mean Versiocolor.
Does anyone know how to do this? Please see code for reproduction below:
library(ggplot2)
library(tidyverse)
iris <- iris %>%
filter(Species == c("setosa", "versicolor"))
temp <- iris %>%
group_by(Species) %>%
dplyr::summarize(Mean = mean(Sepal.Length, na.rm=TRUE))
temp_2 <- iris %>%
group_by(Species) %>%
dplyr::summarize(Median = median(Sepal.Length, na.rm=TRUE))
ggplot(iris, aes(x = Sepal.Length, fill = factor(Species))) +
geom_density(alpha = 0.5) +
theme_minimal() +
geom_vline(data=temp, aes(xintercept=Mean, color="Mean"),
linetype="dotted",
lwd=1) +
geom_vline(data=temp_2, aes(xintercept=Median, color="Median"),
linetype="dashed",
lwd=1) +
scale_color_manual(name = "Statistics", values = c(Median = "black", Mean = "grey50")) +
labs(title = "Distribution of Sepal.Length",
x = "Sepal.Length",
y = "Density",
fill = "Species")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个比较复杂。您需要将线条的线型和颜色映射到
Species
和表示中位数或平均值的字符串的交互,而不是适当地指定手动比例:This is relatively complex. You would need to map the linetype and color of the lines to the interaction of
Species
and a string denoting median or mean, than specify manual scales appropriately: