为事件绘制简单的时间轴
Plot simple time axis for events
我正在寻找一种解决方案来绘制事件标记为红色垂直线的时间轴。我没有找到类似的东西:
(红线高度应该相等)
数据只是 unix 时间戳的列表,应在时间跨度之间绘制为红色字符串。
如何使用ggplot绘图?
首先,这里有一些示例日期:
sampleDates <-
sample(seq(as.Date("2016-01-01")
, as.Date("2016-12-31")
, 1)
, 60)
那么,这里是对@zx8754评论的起点的扩展(注:mtcars
是内置数据集)。在这里,我添加了一个箭头(而不仅仅是一条线),然后使用 geom_linerange
因为您可以在图例中获得一条垂直线,例如,如果您按事件类型为线着色。 theme_minimal
以下的一切都只是改变显示选项以使其更漂亮。
ggplot() +
geom_segment(
aes(x = min(sampleDates) - 10
, xend = max(sampleDates) + 20
, y = 0
, yend = 0)
, arrow = arrow()
) +
geom_linerange(
aes(x = sampleDates
, ymin = -1
, ymax = 1)
, col = "red") +
xlab("Sample Date") +
theme_minimal() +
theme(axis.text.y = element_blank()
, axis.title.y = element_blank()
, panel.grid.major.y = element_blank()
, panel.grid.minor.y = element_blank()
) +
scale_x_date(date_breaks = "1 month"
, date_labels = "%Y\n%b-%d")
我正在寻找一种解决方案来绘制事件标记为红色垂直线的时间轴。我没有找到类似的东西:
数据只是 unix 时间戳的列表,应在时间跨度之间绘制为红色字符串。
如何使用ggplot绘图?
首先,这里有一些示例日期:
sampleDates <-
sample(seq(as.Date("2016-01-01")
, as.Date("2016-12-31")
, 1)
, 60)
那么,这里是对@zx8754评论的起点的扩展(注:mtcars
是内置数据集)。在这里,我添加了一个箭头(而不仅仅是一条线),然后使用 geom_linerange
因为您可以在图例中获得一条垂直线,例如,如果您按事件类型为线着色。 theme_minimal
以下的一切都只是改变显示选项以使其更漂亮。
ggplot() +
geom_segment(
aes(x = min(sampleDates) - 10
, xend = max(sampleDates) + 20
, y = 0
, yend = 0)
, arrow = arrow()
) +
geom_linerange(
aes(x = sampleDates
, ymin = -1
, ymax = 1)
, col = "red") +
xlab("Sample Date") +
theme_minimal() +
theme(axis.text.y = element_blank()
, axis.title.y = element_blank()
, panel.grid.major.y = element_blank()
, panel.grid.minor.y = element_blank()
) +
scale_x_date(date_breaks = "1 month"
, date_labels = "%Y\n%b-%d")