[英]The difference between geom_density in ggplot2 and density in base R
I have a data in R like the following:我在 R 中有如下数据:
bag_id location_type event_ts
2 155 sorter 2012-01-02 17:06:05
3 305 arrival 2012-01-01 07:20:16
1 155 transfer 2012-01-02 15:57:54
4 692 arrival 2012-03-29 09:47:52
10 748 transfer 2012-01-08 17:26:02
11 748 sorter 2012-01-08 17:30:02
12 993 arrival 2012-01-23 08:58:54
13 1019 arrival 2012-01-09 07:17:02
14 1019 sorter 2012-01-09 07:33:15
15 1154 transfer 2012-01-12 21:07:50
where class(event_ts) is POSIXct
.其中 class(event_ts) 是POSIXct
。
I wanted to find the density of bags at each location in different times.我想找到不同时间每个位置的袋子密度。
I used the command geom_density(ggplot2)
and I could plot it very nice.我使用命令geom_density(ggplot2)
并且我可以很好地绘制它。 I wonder if there is any difference between density(base)
and this command.我想知道density(base)
和这个命令之间是否有任何区别。 I mean any difference about the methods that they are using or the default bandwith that they are using and the like.我的意思是他们使用的方法或他们使用的默认带宽等方面的任何差异。
I need to add the densities to my data frame.我需要将密度添加到我的数据框中。 If I had used the function density(base)
, I knew how I can use the function approxfun
to add these values to my data frame, but I wonder if it is the same when I use geom_density(ggplot2)
.如果我使用了函数density(base)
,我知道如何使用函数approxfun
将这些值添加到我的数据框中,但我想知道当我使用geom_density(ggplot2)
时它是否相同。
A quick perusal of the ggplot2 documentation for geom_density()
reveals that it wraps up the functionality in stat_density()
.快速阅读geom_density()
的ggplot2 文档会发现它包含了stat_density()
的功能。 The first argument there references that the adjust
parameter coming from the base function density()
.那里的第一个参数引用了来自基础函数density()
的adjust
参数。 So, to your direct question - they are built off of the same function, though the exact parameters used may be different.因此,对于您的直接问题 - 它们是基于相同的功能构建的,尽管使用的确切参数可能不同。 You have some control over setting those parameters, but you may not be able to have the amount of flexibility you want.您可以对设置这些参数进行一些控制,但您可能无法获得所需的灵活性。
One alternative to using geom_density()
is to calculate the density that you want outside of ggplot()
and then plot it with geom_line()
.使用geom_density()
一种替代方法是在ggplot()
之外计算您想要的密度,然后使用geom_line()
绘制它。 For example:例如:
library(ggplot2)
#100 random variables
x <- data.frame(x = rnorm(100))
#Calculate own density, set parameters as you desire
d <- density(x$x)
x2 <- data.frame(x = d$x, y = d$y)
#Using geom_density()
ggplot(x, aes(x)) + geom_density()
#Using home grown density
ggplot(x2, aes(x,y)) + geom_line(colour = "red")
Here, they give nearly identical plots, though they may vary more significantly with your data and your settings.在这里,它们给出了几乎相同的图,尽管它们可能因您的数据和设置而异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.