【问题标题】:adding chosen dates (day and month) as x-axis labels for each year (the same every year) in ggplot scale_x_date在ggplot scale_x_date中添加选定的日期(日和月)作为每年(每年相同)的x轴标签
【发布时间】:2021-10-31 13:04:32
【问题描述】:

我正在寻找一种方法,可以在折线图的 x 轴上为每年相同的指定日期添加标签。 这里的主要重点是将选定的日期(日和月)添加为每年的 x 轴标签(每年都相同)。最好使用 scale_x_date。

我有一个嵌套列表,其中包含不同河流到达 RCH 的日期和河流流量数据 (Flow)(这里为简单起见仅到达第 910 号):


Flowtest <- list( tibble(date=as.Date(c("2015/08/01","2015/08/02","2015/08/03","2015/08/04",
                                       "2015/08/05","2015/08/06","2015/08/07"), format="%Y/%m/%d"),
                                Flow=c(123, 170, 187, 245, 679, 870, 820),
                                RCH=c(910)), 
                 tibble(date=as.Date(c("2016/08/01","2016/08/02","2016/08/03","2016/08/04",
                                       "2016/08/05","2016/08/06","2016/08/07"), format="%Y/%m/%d"),
                                Flow=c(570, 450, 780, 650, 230, 470, 340),
                                RCH=c(910)),
                 tibble(date=as.Date(c("2017/08/01","2017/08/02","2017/08/03","2017/08/04",
                                        "2017/08/05","2017/08/06","2017/08/07"), format="%Y/%m/%d"),
                                 Flow=c(160, 170, 670, 780, 350, 840, 850),
                                 RCH=c(910)),
                  tibble(date=as.Date(c("2018/08/01","2018/08/02","2018/08/03","2018/08/04",
                                        "2018/08/05","2018/08/06","2018/08/07"), format="%Y/%m/%d"),
                                 Flow=c(120, 780, 820, 580, 870, 870, 840),
                                RCH=c(910)))

我创建了一个函数,它为每年创建一个图表,并在其上绘制来自各个河段的流量(这里为简单起见,我只使用了一个河段 910)。我想为每年相同的几个指定日期(月和日)添加 x 轴标签。我不知道如何让它适用于每一年,所以我只在 2015 年添加了它:

test_line_plot <- function (x) {
  ggplot(x, aes(date, Flow, group = RCH)) +
    geom_line() + 
    facet_wrap( ~ format(date, "%Y"))+
   scale_x_date(date_labels = "%b %d", 
         breaks = (as.Date(c("2015/08/04", "2015/08/06"))))+
    theme (panel.grid.minor = element_blank()) +
    coord_cartesian( ylim = c(0, 1000))
}

我已经将它应用到我的列表中:

test_plot_list <- lapply(Flowtest , test_line_plot)
library("ggpubr")
test_plot <- ggarrange(plotlist = test_plot_list, nrow = 4, ncol = 1)
plot(test_plot)

在生成的图表中,x 轴上的日期标签和匹配的主要网格线仅出现在 2015 年:

我希望实现如下所示的目标,每年都有相同的日期标签和主要的网格线标记它们。 这里的主要重点是将选定的日期(日和月)添加为每年的 x 轴标签(每年都相同)。最好使用 scale_x_date。

如何更改函数 (scale_x_date) 中的代码,使其不仅适用于 2015 年,还适用于数据集中的任何其他年份?

另外我不明白为什么 2015 年的图表比其他图表小...

感谢您的帮助

【问题讨论】:

标签: r ggplot2


【解决方案1】:

您可以使用您的日期创建一个向量并将它们作为breaksscale_x_date 中传递

library(tidyverse)
library(lubridate)

#Example of dates to use as breaks
x_dates <- dmy(c("04/08/15","06/08/15","02/08/17","05/08/18"))
    
Flowtest %>%
  # Make a single data.frame from the list of data.frames
  bind_rows() %>% 
  # Create variable year
  mutate(year = year(date)) %>% 
  ggplot(aes(date, Flow, group = RCH)) +
  geom_line() + 
  # Using year for facet, with free scales and a single column
  facet_wrap(~year,scales = "free", ncol = 1)+
  scale_x_date(date_labels = "%b %d",breaks = x_dates)+
  theme (panel.grid.minor = element_blank()) +
  coord_cartesian( ylim = c(0, 1000))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多