根据因子值 (1,0) 绘制时间序列数据的条形图
plot bar chart for time series data based on factor value (1,0)
我有一个 dataframe/tibble 看起来像:
z <- tibble(Time = as.POSIXct(c(
'2020-01-06 00:22:15',
'2020-01-06 00:45:16',
'2020-01-06 00:46:37',
'2020-01-06 01:29:55')),
Value = c(0,1,0,1))
我想绘制类似 bar chart using time series and value data 的东西
请查看附件,因为我还不能添加内嵌图片。
您可以使用 ggplot2
中的 geom_rect
。要为绘图准备小标题,您可以创建一个用于设置与连续时间相对应的 xmax 的列。
library(dplyr)
library(ggplot2)
z %>% mutate(xmax = lead(Time), y = 0) %>%
ggplot(aes(xmin = Time, xmax = xmax, ymin = y, ymax =y+1))+
geom_rect(aes(fill = as.factor(Value)))+
theme(axis.text.y = element_blank(),
legend.title = element_blank())
它能回答您的问题吗?
我有一个 dataframe/tibble 看起来像:
z <- tibble(Time = as.POSIXct(c(
'2020-01-06 00:22:15',
'2020-01-06 00:45:16',
'2020-01-06 00:46:37',
'2020-01-06 01:29:55')),
Value = c(0,1,0,1))
我想绘制类似 bar chart using time series and value data 的东西 请查看附件,因为我还不能添加内嵌图片。
您可以使用 ggplot2
中的 geom_rect
。要为绘图准备小标题,您可以创建一个用于设置与连续时间相对应的 xmax 的列。
library(dplyr)
library(ggplot2)
z %>% mutate(xmax = lead(Time), y = 0) %>%
ggplot(aes(xmin = Time, xmax = xmax, ymin = y, ymax =y+1))+
geom_rect(aes(fill = as.factor(Value)))+
theme(axis.text.y = element_blank(),
legend.title = element_blank())
它能回答您的问题吗?