将boxplot的geom_segment和Median添加到图例中

时间:2017-09-18 11:54:05

标签: r ggplot2 legend boxplot

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

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,它运行良好,因为默认情况下线型是正确的。

此外,我想在传奇中有第三行。第三行应为中线,这是一条坚固的粗黑线。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分: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"))

enter image description here

答案 1 :(得分:0)

我增加了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"))

enter image description here