add vertical line of the mean to density plot

前端 未结 1 1658
情话喂你
情话喂你 2021-01-07 14:31

Following the example from this website, we can generate a density plot for several factors within a data.frame

library(sm)
attach(mtcars)
sm.de         


        
相关标签:
1条回答
  • 2021-01-07 15:10

    This is an example for the mean. To calculate the median, simply replace "FUN = mean" with "FUN = median" in the aggregate function.

    library(sm)
    attach(mtcars)
    sm.density.compare(mpg, cyl, xlab="Miles Per Gallon")
    
    means <- aggregate(mpg ~ cyl, FUN = mean)
    abline(v = means[1,2], col = 2)
    abline(v = means[2,2], col = 3, lty = 2)
    abline(v = means[3,2], col = 4, lty = 3)
    

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