How to display the median value in a boxplot in ggplot?

后端 未结 1 854
情书的邮戳
情书的邮戳 2020-12-06 01:46

I am trying to show the median value(i.e the horizontal bar) in the a box plot by using ggplot(). R keeps asking to specify y axis. I am a bit stuck.

p <-         


        
相关标签:
1条回答
  • 2020-12-06 02:11

    I don't think that stat_bin is the way to go:

    library(plyr)
    library(ggplot2)
    
    p_meds <- ddply(p, .(TYPE), summarise, med = median(TOTALREV))
    
    ggplot(p,aes(x = TYPE, y = TOTALREV)) + 
        geom_boxplot() + 
        geom_text(data = p_meds, aes(x = TYPE, y = med, label = med), 
                  size = 3, vjust = -1.5)
    

    enter image description here

    As a side note, I generally find that people are less confused with ggplot once they learn to stop using qplot.

    0 讨论(0)
提交回复
热议问题