2016-10-19 22 views
6

に水平線のような手段を追加し、私はggplot2を使って箱ひげ図を作成しました:Rのggplot2:箱ひげ図

library(ggplot2) 

dat <- data.frame(study = c(rep('a',50),rep('b',50)), 
        FPKM = c(rnorm(1:50),rnorm(1:50))) 

ggplot(dat, aes(x = study, y = FPKM)) + geom_boxplot() 

箱ひげ図は、各ボックス間の水平線として中央値を示しています。

enter image description here

は、どのように私は、そのグループの平均値を表すボックスに破線を追加するには?

ありがとうございます!

答えて

10

stat_summaryを使用してプロットに水平線を追加することができます。geom_errorbarです。 yの最小値と最大値がyと同じに設定されているため、線は水平です。

ggplot(dat, aes(x = study, y = FPKM)) + 
    geom_boxplot() + 
    stat_summary(fun.y = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..), 
       width = .75, linetype = "dashed") 

enter image description here

関連する問題