如何在x轴gg_vistime上添加年份详细信息

问题描述 投票:0回答:1

我对 R 相当陌生,我对如何在 x 轴上添加年份详细信息有疑问? (下图),默认设置仅显示 2002-2012 年,间隔为 2 年。同时,我想添加 2003、2005、2007.. 等来每年显示

timeline

我的数据集以 dput() 的形式:

structure(list(start_year = structure(c(978307200, 1009843200, 
1041379200, 1072915200, 1104537600, 1136073600, 1167609600, 1199145600, 
1230768000, 1262304000, 1293840000, 1325376000, 1356998400), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), end_year = structure(c(1356998400, 
1357084800, 1357171200, 1357257600, 1357344000, 1357430400, 1357516800, 
1357603200, 1357689600, 1357776000, 1357862400, 1357948800, 1358035200
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), gbrs = c("LEED", 
"LEED", "LEED", "BREEAM", "LEED", "LEED", "LEED", "BREEAM", "LEED", 
"LEED", "LEED", "BREEAM", "LEED"), indicator = c("Energy & Atmosphere", 
"Energy & Atmosphere", "Energy & Atmosphere", "Health and Wellbeing", 
"Indoor Environmental Quality", "Indoor Environmental Quality", 
"Indoor Environmental Quality", "Management", "Material & Resources", 
"Material & Resources", "Sustainable Sites", "Transport", "Water Efficiency"
), abb = c("EaA", "EaA", "EaA", "HaW", "IEQ", "IEQ", "IEQ", "M", 
"MaR", "Apr", "SS", "T", "WE")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -13L))

这就是我所做的

vistime(test, col.event = "abb", col.group = "gbrs", 
        col.start = "start_year", col.end = "end_year", col.tooltip = "tooltip",
        optimize_y = TRUE, show_labels = TRUE)

当我尝试添加scale_x_date(如下)时,出现错误并显示NULL

vistime(test, col.event = "abb", col.group = "gbrs", 
        col.start = "start_year", col.end = "end_year", col.tooltip = "tooltip",
        optimize_y = TRUE, show_labels = TRUE) +
  scale_x_continuous(breaks = function(x) exec(seq, !!!x), 
                     labels = function(x) x, 
                     limits = c(2004, 2020))

我做错了什么吗?如有帮助,将不胜感激! :)

r vistime
1个回答
0
投票

您可以使用

gg_vistime
来使用
ggplot2
函数。为此,我们可以使用
scale_x_datetime
函数,其中
date_breaks
为 1 年,如下所示:

library(vistime)
library(ggplot2)
gg_vistime(test, col.event = "abb", col.group = "gbrs", 
        col.start = "start_year", col.end = "end_year", col.tooltip = "tooltip",
        optimize_y = TRUE, show_labels = TRUE) +
  scale_x_datetime(date_breaks = "1 year", date_labels = "%Y")

创建于 2024-05-06,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.