R ggplot2: Add tool as a horizontal line in the drawer

I created boxplot using 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() 

The box shows the median horizontal line on each box.

enter image description here

How to add a dashed line to the field representing the average of this group?

Thanks!

+6
source share
1 answer

You can add horizontal lines to graphs using stat_summary with geom_errorbar . The row is horizontal because the minimum and maximum values โ€‹โ€‹of y are equal to 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

+10
source

Source: https://habr.com/ru/post/1011484/


All Articles