【发布时间】:2018-01-23 17:39:24
【问题描述】:
我有以下数据集:
https://app.box.com/s/au58xaw60r1hyeek5cua6q20byumgvmj
我想根据一天中的时间创建一个密度图。这是我到目前为止所做的:
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"))
它给出了以下错误:
Error: Invalid input: time_trans works with objects of class POSIXct only
如果我将其转换为 POSIXct,它会将日期添加到数据中。
更新 1
以下转换数据为'NA'
timestamp_df$timestamp_time <- as.POSIXct(timestamp_df$timestamp_time, format = "%H:%M%:%S", tz = "UTC"
更新 2
【问题讨论】:
-
timestamp_time是一流的...character?您必须使用as.POSIXct或as.POSIXlt强制它。 -
timestamp_df$timestamp_time <- as.POSIXct(timestamp_df$timestamp_time, format = "%H:%M%:%S", tz = "UTC")将数据设为“NA” -
您的
format参数不正确。下次,您可以考虑为您的问题提供一个可重现的示例。
标签: r ggplot2 lubridate density-plot