R - What algorithm does geom_density() use and how to extract points/equation of curves?

前端 未结 2 937
死守一世寂寞
死守一世寂寞 2020-12-03 00:11

I would like to know what is geom_density() exactly doing, so I justify the graph and if there is any way of extracting the function or points that generates for each of the

2条回答
  •  有刺的猬
    2020-12-03 00:31

    As suggested in other answers, you can access the ggplot points using print.ggplot(). However, print()-ing code also prints the ggplot object, which may not be desired.

    You can get extract the ggplot object data, without printing the plot, using ggplot_build():

    library(ggplot2)
    library(ggplot2movies)
    
    m <- ggplot(movies, aes(x = rating))
    m <- m + geom_density()
    p <- ggplot_build(m)  # <---- INSTEAD OF `p <- print(m)`
    head(p$data[[1]], 3)
    #             y        x     density     scaled    count     n PANEL group ymin
    # 1 0.007376115 1.000000 0.007376115 0.02591684 433.6271 58788     1    -1    0
    # 2 0.007652653 1.017613 0.007652653 0.02688849 449.8842 58788     1    -1    0
    # 3 0.007872571 1.035225 0.007872571 0.02766120 462.8127 58788     1    -1    0
    
    
    # Just to show that those are the points you are after, extract and use them 
    # to create a lattice xyplot 
    library(lattice)
    m2 <- xyplot(y ~x, data=p$data[[1]], type="l")
    
    library(gridExtra)
    grid.arrange(m, m2, nrow=1)
    

提交回复
热议问题