ge中的每周日期的geom_density()

时间:2015-09-03 14:13:22

标签: r ggplot2

我正在尝试按星期几创建一个密度图。

我有以下数据:

a = 5;
if(a) {
// some code
}

但是,使用以下ggplot代码,我得到以下输出:

         dateDelivered       day
5  2015-07-13 14:57:34    Monday
11 2015-07-22 16:50:11 Wednesday
12 2015-07-27 12:08:13    Monday
13 2015-07-28 10:30:44   Tuesday
14 2015-07-28 12:42:58   Tuesday

输出图像可在此处找到:

enter image description here

理想情况下,我想要一行显示每天的密度。

2 个答案:

答案 0 :(得分:2)

你的问题是geom_density(...)想要一个连续的x轴,而你正在提供一个因子。这是一种或多或少地做你要求的方式。这有点类似于其他答案。

# create sample dataset - you have this already
set.seed(1)   # for reproducibility
dow <- c("M","T","W","Th","F")
df <- data.frame(x=sample(dow,100, replace=TRUE,p=c(.2,.4,.2,.1,.1)))
df$x <- with(df,factor(x, levels=dow))   # need this to get days in the correct order

# you start here...
ggplot(df, aes(x=as.numeric(x))) + 
  geom_histogram(aes(y=..density..),binwidth=1, fill="lightgreen", color="grey70", origin=0.5)+
  stat_density(color="red", geom="line")+
  scale_x_continuous(name="",labels=dow, breaks=seq_along(dow))

答案 1 :(得分:1)

可以通过一点点争吵来做到这一点。

首先,您需要将时间转换为POSIXct,如果还没有:

df_delivered$date2 <- as.POSIXct(df_delivered$dateDelivered, format="%Y-%m-%d %H:%M:%S")

接下来,将日期列转换为因子,这样我们就可以获得每周通过的天数:

df_delivered$day <- factor(df_delivered$day, levels = c("Sunday", "Monday", 
                                   "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"))

现在我们需要获得每天的时间:

df_delivered$date3 <- as.numeric(df_delivered$date2 - trunc(df_delivered$date2, "days"))/60 

现在我们可以使用我们的轴作为24 *天数,再加上日期列:

ggplot(df_delivered, aes(x = date3+as.numeric(day)*24)) +
    geom_density()

编辑:来自模拟数据集,时间已经为POSIXct。 数据,命名为df_delivered:

structure(list(dateDelivered = structure(c(1325420712.07391, 
1325473818.87468, 1325944988.29881, 1326644837.14753, 1327078496.72151, 
1327173445.32841, 1328170080.73442, 1330948998.35264, 1332327962.56186, 
1332768069.2447, 1333181987.1911, 1335463412.38292, 1336150093.37186, 
1336729162.94389, 1336794193.70351, 1336815576.06732, 1336890600.1805, 
1336933569.03092, 1337348143.61397, 1337616505.99632, 1337639497.61884, 
1338600812.52401, 1338605366.09276, 1338723437.09295, 1339189548.67689, 
1339517147.2495, 1339932397.01648, 1340107202.65251, 1340330642.50151, 
1341405354.71406, 1341418497.24939, 1344275327.79584, 1344286095.22275, 
1344613247.17759, 1348160598.15293, 1348759049.56039, 1348961725.19995, 
1349065123.77902, 1349191950.54645, 1349200558.14843, 1350922752.47161, 
1351430185.1209, 1351531759.2165, 1351534610.79749, 1351964975.69713, 
1352534323.88355, 1353403252.48848, 1353737961.90685, 1354897718.11477, 
1355562841.50268), class = c("POSIXct", "POSIXt")), day = structure(c(4L, 
4L, 3L, 4L, 1L, 3L, 5L, 2L, 7L, 2L, 3L, 5L, 1L, 1L, 1L, 3L, 4L, 
4L, 1L, 2L, 2L, 1L, 1L, 4L, 1L, 6L, 4L, 6L, 5L, 7L, 7L, 2L, 2L, 
1L, 5L, 5L, 3L, 2L, 6L, 6L, 2L, 4L, 2L, 2L, 3L, 3L, 6L, 3L, 1L, 
3L), .Label = c("Friday", "Monday", "Saturday", "Sunday", "Thursday", 
"Tuesday", "Wednesday"), class = "factor")), .Names = c("dateDelivered", 
"day"), row.names = c(NA, -50L), class = "data.frame")

流程和情节:

df_delivered$day <- factor(df_delivered$day, levels = c("Sunday", "Monday", 
                                                        "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"))
df_delivered$date3 <- as.numeric(df_delivered$dateDelivered - trunc(df_delivered$dateDelivered, "days"))/60
ggplot(df_delivered, aes(x=date3+as.numeric(day)*24)) +geom_density()

输出:

enter image description here