2017-09-18 4 views
1

私は、凡例とBoxplotの中央値に2つ以上のセグメントを持つ方法を模索しています。 Iは、以下の例が出ている:Yと凡例にboxplotのgeom_segmentとMedianを追加

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")) 

注意geom_segment = 0.6黒い破線と凡例でなければなりません。現時点では、線種を2回選択しましたが、これは意味をなさないですが、2番目の線種を消去すると、凡例の色が赤に変わり、線種が別のものに変わります。それはプロットだけでなく、伝説のために黒と破線でなければなりません。 y = 0.8の場合、線種はデフォルトで正しいので、うまくいきます。

さらに、私は凡例に3行目を入れたいと思います。 3行目は中央線で、太くて太い黒線です。

ご協力いただきありがとうございます。

答えて

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

+0

をあなたの助けのためにありがとうございました。私はあなたの答えを少し増やしました。私は2つの異なる線種も持っています(以下の答えを見てください)。しかし、私はBoxplotの中央線を伝説に追加する方法はまだ分かりません。 1つの方法は、3行目(geom_segment)を正中線の上に正確に追加することです。これが唯一の方法ですか? –

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

+0

しかし、Boxplotの中央値はまだ伝説にはありません。 –

関連する問題