Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-geom_density()使用什么算法以及如何提取曲线的点/方程?_R_Ggplot2 - Fatal编程技术网

R-geom_density()使用什么算法以及如何提取曲线的点/方程?

R-geom_density()使用什么算法以及如何提取曲线的点/方程?,r,ggplot2,R,Ggplot2,我想知道geom_density()到底在做什么,因此我对图形进行了验证,以及是否有任何方法可以提取为绘制的每条曲线生成的函数或点 谢谢键入get(“计算组”,ggplot2::StatDensity)(或者以前的get(“计算”,ggplot2:::StatDensity))将获得用于计算密度的算法。(在根目录下,它是对density()的调用,默认值为kernel=“gaussian”) 打印中使用的点由print.ggplot()以不可见的方式返回,因此您可以如下方式访问它们: libra

我想知道geom_density()到底在做什么,因此我对图形进行了验证,以及是否有任何方法可以提取为绘制的每条曲线生成的函数或点

谢谢

键入
get(“计算组”,ggplot2::StatDensity)
(或者以前的
get(“计算”,ggplot2:::StatDensity)
)将获得用于计算密度的算法。(在根目录下,它是对
density()
的调用,默认值为
kernel=“gaussian”

打印中使用的点由
print.ggplot()
以不可见的方式返回,因此您可以如下方式访问它们:

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")
库(ggplot2)

m如其他答案所示,您可以使用
print.ggplot()
访问ggplot点。但是,
print()
-ing代码也会打印ggplot对象,这可能是不需要的

您可以使用
ggplot\u build()
,在不打印绘图的情况下提取ggplot对象数据:

库(ggplot2)
图书馆(ggplot2movies)

我发现stat_density()允许您设置参数。所以这可能回答了第一部分。我还想知道方程或点是否能被提取出来。谢谢你,威震天,这正是我想要的!
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)