【问题标题】:Adding geom_segment and Median of the Boxplot to legend将箱线图的 geom_segment 和 Median 添加到图例
【发布时间】:2017-09-18 11:54:04
【问题描述】:

我正在寻找一种方法,在图例中拥有两个或更多段以及箱线图的中位数。 我想出了以下示例:

y = data.frame(runif(500))
library(ggplot2)
ggplot(data = y )+
  aes(0, y)+
  geom_boxplot(outlier.shape = 1)+ 
  scale_y_continuous(name = "",limits = c(0,1))+
  scale_x_discrete(name = "")+
  geom_segment( aes(x=-0.362, y=0.6, xend=0.363,yend=0.6, linetype = "R 
  fans"), linetype = "dashed", colour = "black")+
  geom_segment( aes(x=-0.35, y=0.8, xend=0.35,yend=0.8, linetype = 
  "frustated R users"), col = "red" )+
  theme_bw()+
  theme(legend.title = element_blank())+
  theme(legend.background = element_rect(fill="white",
                                     size=0.1, linetype="solid", 
                                     colour ="black"))

y=0.6 的 geom_segment 应在图例中以黑色虚线表示。目前我选择了两次线型,这没有意义,但是如果我擦除第二个线型,图例中的颜色会变为红色,或者线型会变为另一种不需要的线型。对于情节和图例,它应该是黑色和虚线。对于 y = 0.8,它工作得很好,因为默认情况下线型是正确的。

另外,我想在图例中添加第三行。第三条线应该是中线,是一条实心粗黑线。

提前感谢您的帮助。

【问题讨论】:

    标签: r ggplot2 legend boxplot


    【解决方案1】:

    解决方案,将行作为单独的data.frame 传递,通过color = Group 设置单独的行颜色并使用scale_color_manual 指定这些颜色。

    library(ggplot2)
    
    # Generate data
    dBox <- data.frame(y = rnorm(10))
    dLines <- data.frame(X =c(-0.362, -0.35),
                         Y = c(0.6, 0.8),
                         Xend = c(0.363, 0.35),
                         Yend=c(0.6, 0.8),
                         Group = c("typeA", "typeB"),
                         color = c("black", "red"))
    
    
    ggplot(dBox, aes(0, y)) +
        geom_boxplot(outlier.shape = 1)+ 
        scale_y_continuous(name = "",limits = c(0,1))+
        scale_x_discrete(name = "") +
        geom_segment(data = dLines, 
                     aes(x = X, xend = Xend, 
                         y = Y, yend = Yend, 
                         color = Group)) +
        scale_color_manual(values = dLines$color) +
        theme_bw() +
        theme(legend.title = element_blank()) +
        theme(legend.background = element_rect(fill = "white",
                                               size = 0.1, 
                                               linetype = "solid", 
                                               colour = "black"))
    

    【讨论】:

    • 非常感谢您的帮助。我稍微增加了你的答案,所以我也有两种不同的线型(见下面的答案)。但我仍然不知道如何将 Boxplot 的中线添加到图例中。一种方法是在中线的顶部添加第三条线 (geom_segment)。这是唯一的方法吗?
    【解决方案2】:

    我还为两种不同的线型增强了 PoGibas 非常有用的答案:

    y = data.frame(runif(500))
    
    
    dLines <- data.frame(X =c(-0.362, -0.35),
                     Y = c(0.6, 0.8),
                     Xend = c(0.363, 0.35),
                     Yend=c(0.6, 0.8),
                     Group = c("TypeA", "TypeB"),
                     color = c("black", "red"),
                     linetype = c( "solid", "dashed"))
    
    ggplot(data = y )+
      aes(0, y)+
      geom_boxplot(outlier.shape = 1)+ 
      scale_y_continuous(name = "",limits = c(0,1))+
      scale_x_discrete(name = "")+
      geom_segment(data = dLines, 
               aes(x = X, xend = Xend, 
                   y = Y, yend = Yend, 
                   color = Group,
                   linetype = Group))+
      scale_color_manual(values = dLines$color) +
      scale_linetype_manual(values = dLines$linetype) +
      theme_bw()+
      theme(legend.title = element_blank())+
      theme(legend.background = element_rect(fill="white",
                                         size=0.1, linetype="solid", 
                                         colour ="black"))
    

    【讨论】:

    • 但图例中仍然缺少箱线图的中位数。
    猜你喜欢
    • 2021-09-12
    • 2018-04-08
    • 1970-01-01
    • 2018-05-11
    • 2023-04-11
    相关资源
    最近更新 更多