简体   繁体   English

R中的时间密度图

[英]Temporal density plot in R

I have irregularly measured observations of a phenomenon with a timestamp each: 我对一个现象的观察结果进行了不规则的测量,每个观察结果都带有时间戳:

2013-01-03 00:04:23
2013-01-03 00:02:04
2013-01-02 23:45:16
2013-01-02 23:35:16
2013-01-02 23:31:56
2013-01-02 23:31:30
2013-01-02 23:29:18
2013-01-02 23:28:43
...

Now I would like to plot these points on the x axis and apply a kernel density function to them, so I can visually explore temporal density using various bandwidths. 现在,我想在x轴上绘制这些点并对其应用核密度函数,以便可以使用各种带宽直观地探索时间密度。 Something like this should turn out, although the example below does not use x axis labeling; 尽管下面的示例未使用x轴标签,但应该会出现类似的情况。 I would like to have labels with, for example, particular days (January 1st, January 5th, etc.): 我想要带有特定日期(例如1月1日,1月5日等)的标签:

核密度图

It is important, however, that the measurement points themselves are visible in the plot, like above. 但是,重要的是,测量点本身在图中是可见的,就像上面一样。

#dput
df <- structure(list(V1 = structure(c(2L, 2L, 1L, 3L, 1L, 4L, 5L, 4L), .Label = c("2013-01-02", "2013-01-03", "2013-01-04", "2013-01-05", "2013-01-11"), class = "factor"), V2 = structure(c(1L, 3L, 8L,  4L, 7L, 6L, 5L, 2L), .Label = c(" 04:04:23", " 06:28:43", " 10:02:04", " 11:35:16", " 14:29:18", " 17:31:30", " 23:31:56", " 23:45:16"), class = "factor")), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, -8L))

Using ggplot since it gives fine-grained control over your plot. 使用ggplot因为它可以对您的绘图进行细粒度的控制。 Use different layers for the measurements and the density itself. 使用不同的层进行测量和密度本身。

df$tcol<- as.POSIXct(paste(df$dte, df$timestmp), format= "%Y-%m-%d %H:%M:%S")
library(ggplot2)
measurements <- geom_point(aes(x=tcol, y=0), shape=15, color='blue', size=5)
kde <- geom_density(aes(x=tcol), bw="nrd0")
ggplot(df) + measurements +  kde

Leads to 导致 在此处输入图片说明

Now, if you want to further adjust the x-axis labels (since you want each separate day marked, you can use the scales package. We are going to use scale_x_date but that only takes in 'Date' 现在,如果您想进一步调整x轴标签(因为您希望每个单独的日期都被标记,则可以使用scales包。我们将使用scale_x_date但这仅包含“ Date”(日期)

library(scales)
df$tcol <- as.Date(df$tcol, format= "%Y-%m-%d %H:%M:%S")
xlabel <- scale_x_date(labels=date_format("%m-%d"), breaks="1 day")
ggplot(df) + xlabel + measurements +  kde

This gives: 这给出: 在此处输入图片说明

Please note that the hours seem to have gotten rounded. 请注意,时间似乎已经四舍五入了。

Hopefully this helps you move forward. 希望这可以帮助您前进。

Convert your values to POSIXct, convert that numeric (ie, seconds in UNIX time) and then apply your kernel density function. 将您的值转换为POSIXct,转换该数字(即UNIX时间中的秒),然后应用内核密度函数。 If z is your vector of timestamps: 如果z是您的时间戳向量:

z2 <- as.POSIXct(z, "%Y-%m-%d %H:%M:%S", tz="GMT")
plot(density(as.numeric(z2)))

It would then be relatively easy to add a labeled x-axis with axis . 这样,相对容易地添加带有axis的标记x axis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM