R - geom_density()使用什么算法以及如何提取曲线的点/方程? [英] R - What algorithm does geom_density() use and how to extract points/equation of curves?

查看:255
本文介绍了R - geom_density()使用什么算法以及如何提取曲线的点/方程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是geom_density()正好在做什么,所以我证明了图的正确性,以及是否有任何方式来提取函数或为绘制的每条曲线生成的点。

感谢

解决方案

输入 compute_group,ggplot2 :: StatDensity)(或者,以前, get(calculate,ggplot2 ::: StatDensity))会让你用于计算密度的算法。 (在根目录下,这是对 density()的调用,并且 kernel =gaussian默认值)。



图中使用的点无形性地由 print.ggplot()返回,因此您可以像这样访问它们:

  library(ggplot2)
m < - ggplot(aes(x = rating))
m< ; - m + geom_density()
p < - print(m)
头(p $ data [[1]],3)
#yx密度缩放计数PANEL组ymin ymax
#1 0.0073761 1.0000 0.0073761 0.025917 433.63 1 1 0 0.0073761
#2 0.0076527 1.0176 0.0076527 0.026888 449.88 1 1 0 0.0076527
#3 0.0078726 1.0352 0.0078726 0.027661 462.81 1 1 0 0.0078726


##为了表明这些是你之后的点,
##提取并使用它们来创建一个格xyplot
库(gridExtra)
库(格)
mm < - xyplot(y〜x,data = p $ data [[1]],type =l)


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 curves being plotted.

Thanks

解决方案

Typing get("compute_group", ggplot2::StatDensity) (or, formerly, get("calculate", ggplot2:::StatDensity)) will get you the algorithm used to calculate the density. (At root, it's a call to density() with kernel="gaussian" the default.)

The points used in the plot are invisibly returned by print.ggplot(), so you can access them like this:

library(ggplot2)
m <- ggplot(movies, aes(x = rating))
m <- m + geom_density()
p <- print(m)
head(p$data[[1]], 3)
#           y      x   density   scaled  count PANEL group ymin      ymax
# 1 0.0073761 1.0000 0.0073761 0.025917 433.63     1     1    0 0.0073761
# 2 0.0076527 1.0176 0.0076527 0.026888 449.88     1     1    0 0.0076527
# 3 0.0078726 1.0352 0.0078726 0.027661 462.81     1     1    0 0.0078726


## Just to show that those are the points you are after, 
## extract and use them to create a lattice xyplot 
library(gridExtra)
library(lattice)
mm <- xyplot(y ~x, data=p$data[[1]], type="l")

这篇关于R - geom_density()使用什么算法以及如何提取曲线的点/方程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆