根据组调整ggplot中geom_vline的颜色

发布于 2025-01-19 18:27:05 字数 1406 浏览 6 评论 0原文

因此,我正在为两种虹膜数据的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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放低过去 2025-01-26 18:27:05

这个比较复杂。您需要将线条的线型和颜色映射到 Species 和表示中位数或平均值的字符串的交互,而不是适当地指定手动比例:

ggplot(iris, aes(x = Sepal.Length, fill = factor(Species))) +
  geom_density(alpha = 0.5) +
  theme_minimal() +
  geom_vline(data=temp, aes(xintercept=Mean, 
                            color = interaction(Species, 'Mean'),
                            linetype = interaction(Species, 'Mean')),
             lwd = 1, key_glyph = draw_key_path) +
  geom_vline(data=temp_2, aes(xintercept=Median, 
                              color = interaction(Species, 'Median'),
                              linetype = interaction(Species, 'Median')),
             lwd=1,  key_glyph = draw_key_path) +
  scale_linetype_manual(values = c(setosa.Mean = 'dotted', 
                                   setosa.Median = 'dashed',
                                   versicolor.Mean = 'dotted',
                                   versicolor.Median = 'dashed'),
                        name = 'Averages') +
  scale_color_manual(values = c(setosa.Mean = "#F8766D", 
                                   setosa.Median = "#F8766D",
                                   versicolor.Mean = "#00BFC4",
                                   versicolor.Median = "#00BFC4"),
                     name = 'Averages') +
  labs(title = "Distribution of Sepal.Length",
       x = "Sepal.Length",
       y = "Density",
       fill = "Species") +
  theme(legend.key.width = unit(15, 'mm'))

在此处输入图像描述

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:

ggplot(iris, aes(x = Sepal.Length, fill = factor(Species))) +
  geom_density(alpha = 0.5) +
  theme_minimal() +
  geom_vline(data=temp, aes(xintercept=Mean, 
                            color = interaction(Species, 'Mean'),
                            linetype = interaction(Species, 'Mean')),
             lwd = 1, key_glyph = draw_key_path) +
  geom_vline(data=temp_2, aes(xintercept=Median, 
                              color = interaction(Species, 'Median'),
                              linetype = interaction(Species, 'Median')),
             lwd=1,  key_glyph = draw_key_path) +
  scale_linetype_manual(values = c(setosa.Mean = 'dotted', 
                                   setosa.Median = 'dashed',
                                   versicolor.Mean = 'dotted',
                                   versicolor.Median = 'dashed'),
                        name = 'Averages') +
  scale_color_manual(values = c(setosa.Mean = "#F8766D", 
                                   setosa.Median = "#F8766D",
                                   versicolor.Mean = "#00BFC4",
                                   versicolor.Median = "#00BFC4"),
                     name = 'Averages') +
  labs(title = "Distribution of Sepal.Length",
       x = "Sepal.Length",
       y = "Density",
       fill = "Species") +
  theme(legend.key.width = unit(15, 'mm'))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文