【问题标题】:Mean and median boxplot legend for geom_boxplot in the ggplot2 functionggplot2 函数中 geom_boxplot 的平均值和中值箱线图图例
【发布时间】:2021-11-25 08:34:32
【问题描述】:

我正在尝试将黑色实线识别为“中位数”,将红色虚线识别为“平均值”。

但是,即使我包含show.legend = TRUElegend.position = "right",图例也不会显示在图表上。我将在我的箱线图下方添加一张图片,因为它目前没有图例。

图片说明:

我的代码如下:

box %>% 
  pivot_longer(cols = c(Total, EX, MD, CI, EI)) %>% 
  ggplot(aes(x = name, y = value)) +
  geom_boxplot(fill="gray", show.legend = TRUE) +
  labs(x = "Burnout Dimension", y = "Likert Response Count") +
  stat_summary(fun.y = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
               width = .75, linetype = "dashed", color="red") +
  scale_colour_manual("Values", values=c("blue", "red")) +
  theme_minimal() +
  theme(text = element_text(family="Times New Roman",
                            color="#595959",
                            size=14),
        legend.position = "right",
        panel.grid.major.y = element_line(c(0,25,50,75,100),
                                          color="#D9D9D9",
                                          size=0.2),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        panel.background = element_blank())

【问题讨论】:

    标签: r ggplot2 boxplot


    【解决方案1】:

    如果你想拥有一个传奇,你必须在美学上进行映射:

    1. color = ... 移动到aes() 中,而不是使用颜色名称(在这种情况下没有意义),而是使用一些有意义的标签,例如meanmedian
    2. 要获得中位数的图例条目,您还必须为中位数添加第二个stat_summary 层,并摆脱geom_boxplot 绘制的默认中线,例如使用fatten = 0
    3. 通过使用命名向量来设置所需的颜色,在该向量中,您使用在步骤 1 中设置的有意义的标签作为名称。

    由于您没有提供数据,我使用了一些随机的假数据:

    library(tidyr)
    library(ggplot2)
    
    box %>%
      pivot_longer(cols = c(Total, EX, MD, CI, EI)) %>%
      ggplot(aes(x = name, y = value)) +
      geom_boxplot(fill = "gray", show.legend = FALSE, fatten = 0) +
      labs(x = "Burnout Dimension", y = "Likert Response Count") +
      stat_summary(
        fun = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y.., color = "Mean"),
        width = .75, linetype = "dashed"
      ) +
      stat_summary(
        fun = median, geom = "errorbar", aes(ymax = ..y.., ymin = ..y.., color = "Median"),
        width = .75, linetype = "solid"
      ) +
      scale_colour_manual("Values", values = c(Median = "blue", Mean = "red")) +
      theme_minimal() +
      theme(
        text = element_text(
          family = "Times New Roman",
          color = "#595959",
          size = 14
        ),
        legend.position = "right",
        panel.grid.major.y = element_line(c(0, 25, 50, 75, 100),
          color = "#D9D9D9",
          size = 0.2
        ),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        panel.background = element_blank()
      )
    

    数据

    set.seed(42)
    
    box <- data.frame(
      EX = sample(1:5, 10, replace = TRUE),
      MD = sample(1:5, 10, replace = TRUE),
      CI = sample(1:5, 10, replace = TRUE),
      EI = sample(1:5, 10, replace = TRUE),
      Total = sample(1:5, 10, replace = TRUE)
    )
    

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 2013-09-03
      • 2020-10-07
      • 2011-01-30
      • 2012-08-31
      • 2023-04-03
      • 2021-07-01
      • 2021-11-02
      • 1970-01-01
      相关资源
      最近更新 更多