R 基于一天中的时间的密度图

R 基于一天中的时间的密度图,r,ggplot2,lubridate,density-plot,R,Ggplot2,Lubridate,Density Plot,我有以下数据集: 我想根据一天中的时间创建一个密度图。以下是我迄今为止所做的工作: library("ggplot2") library("scales") library("lubridate") timestamp_df$timestamp_time <- format(ymd_hms(hn_tweets$timestamp), "%H:%M:%S") ggplot(timestamp_df, aes(timestamp_time)) + geom_density(

我有以下数据集:

我想根据一天中的时间创建一个密度图。以下是我迄今为止所做的工作:

library("ggplot2")
library("scales")
library("lubridate")

timestamp_df$timestamp_time <- format(ymd_hms(hn_tweets$timestamp), "%H:%M:%S")

ggplot(timestamp_df, aes(timestamp_time)) + 
       geom_density(aes(fill = ..count..)) +
       scale_x_datetime(breaks = date_breaks("2 hours"),labels=date_format("%H:%M"))
更新2

以下是我想要实现的目标: 这里有一种方法:

library(ggplot2)
library(lubridate)
library(scales)

df <- read.csv("data.csv") #given in OP
提取小时和分钟:

df$time <- hms::hms(second(df$timestamp), minute(df$timestamp), hour(df$timestamp))  
要将其缩放为1,请执行以下操作:

ggplot(df) + 
  geom_density( aes(x = time, y = ..scaled..), fill = "red", alpha = 0.5) +
  scale_x_datetime(breaks = date_breaks("2 hours"), labels=date_format("%H:%M"))
其中..缩放。。是在绘图创建期间生成的统计密度的计算变量


时间戳\u时间属于类。。。性格您必须使用as.POSIXct或as.POSIXlt.timestamp\u df$timestamp\u time强制它。您的format参数不正确。下一次,你可以考虑为你的问题提供一个可重复的例子。非常感谢!我试着用2小时休息的形式来绘制这个图,而不是日期。基本上,我想看看在24小时内什么时候计数较高。这是社交帖子的时间戳。对不起;也许,我并不完全清楚自己想要什么。我更新了原来的帖子。太好了!我可以根据你的解决方案重新创建这个。但有一个问题——为什么它在y轴上显示科学数字?我通过使用选项SCIPEN=10000改变了这一点,因此它现在显示十进制数字。我应该从小数点解释什么?密度曲线必须处处都是非负的,整个曲线的积分必须等于一。如果愿意,也可以按比例打印:选中“编辑”。
df$time <- hms::hms(second(df$timestamp), minute(df$timestamp), hour(df$timestamp))  
df$time <- as.POSIXct(df$time)


ggplot(df, aes(time)) + 
  geom_density(fill = "red", alpha = 0.5) + #also play with adjust such as adjust = 0.5
  scale_x_datetime(breaks = date_breaks("2 hours"), labels=date_format("%H:%M"))
ggplot(df) + 
  geom_density( aes(x = time, y = ..scaled..), fill = "red", alpha = 0.5) +
  scale_x_datetime(breaks = date_breaks("2 hours"), labels=date_format("%H:%M"))