ggplot2中的geom_density与基准R中的密度之间的差异 [英] The difference between geom_density in ggplot2 and density in base R

查看:398
本文介绍了ggplot2中的geom_density与基准R中的密度之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有一个数据,如下所示:

I have a data in R like the following:

  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

其中class(event_ts)是POSIXct.

where class(event_ts) is POSIXct.

我想找到不同时间在每个位置的行李密度.

I wanted to find the density of bags at each location in different times.

我使用了命令geom_density(ggplot2),我可以将其绘制得非常漂亮.我想知道density(base)和此命令之间是否有任何区别.我的意思是关于他们使用的方法或他们使用的默认带宽等的任何区别.

I used the command geom_density(ggplot2) and I could plot it very nice. I wonder if there is any difference between density(base) and this command. I mean any difference about the methods that they are using or the default bandwith that they are using and the like.

我需要将密度添加到我的数据框中.如果我使用过函数density(base),我知道如何使用函数approxfun将这些值添加到数据框中,但是我想知道使用geom_density(ggplot2)时是否相同.

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) .

推荐答案

快速阅读geom_density() 的ggplot2文档rel ="nofollow noreferrer"> ggplot2文档显示,它包装了stat_density()中的功能.那里的第一个参数引用adjust参数来自基本函数density().因此,直接的问题是-它们是基于相同的函数构建的,尽管所使用的确切参数可能有所不同.您可以控制设置这些参数,但可能无法获得所需的灵活性.

A quick perusal of the ggplot2 documentation for geom_density() reveals that it wraps up the functionality in stat_density(). The first argument there references that the adjust parameter coming from the base function density(). 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.

使用geom_density()的一种替代方法是计算所需的ggplot()以外的密度,然后使用geom_line()进行绘制.例如:

One alternative to using geom_density() is to calculate the density that you want outside of ggplot() and then plot it with 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.

这篇关于ggplot2中的geom_density与基准R中的密度之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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