我正在尝试绘制一段时间内特定日期发生的“中断”计数。但是,由于“时间”变量采用date:time格式,并且图形无法生成,因此出现问题。
ggplot(df, aes(y = `Breaks`, x = `Date`)) +
geom_histogram(bins = 100, binwidth = 1, colour = "white", fill = "#1380A1")
示例数据:
structure(list(Date = structure(c(1544107050, 1544100120, 1540557866,
1540558168, 1544100123, 1544100135, 1545299546, 1545299518, 1545822865,
1545822864, 1545822866, 1545822875, 1546016246, 1546016252, 1546016263
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Breaks = c(NA,
NA, 2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA)), row.names = c(NA,
15L), class = "data.frame")
答案 0 :(得分:2)
library(tidyverse)
df %>%
mutate(Date = as.Date(Date)) %>%
count(Date, wt = Breaks) %>%
ggplot(aes(Date, n)) +
geom_col(colour = "white", fill = "#1380A1")
(不确定我是否理解有关“但是我需要图形中本质上表示(o)的缺失值。”的注释,是否应该以某种方式直观地表示零?)顺便说一句,通过count(Date = ...
行的部分会产生这个-是您通过捕获缺失值意味着什么?)
# A tibble: 5 x 2
Date n
<date> <dbl>
1 2018-10-26 2
2 2018-12-06 0
3 2018-12-20 0
4 2018-12-26 0
5 2018-12-28 1