【问题标题】:ggplot breaks as date month and day without year, show labels for chosen dates in rggplot 中断为没有年份的日期月份和日期,在 r 中显示所选日期的标签
【发布时间】:2021-09-02 01:48:13
【问题描述】:

我正在寻找一种方法,可以在折线图的 x 轴上为每年相同的指定日期添加标签。

我有一个嵌套列表,其中包含不同河流到达 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 年:

我希望实现如下所示的目标,其中每年都有相同的日期标签和标记它们的主要网格线:

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

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

感谢您的帮助

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我建议您使用单个 data.frame 来每年使用 facet。

        library(tidyverse)
        library(lubridate)
        
        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")+
          theme (panel.grid.minor = element_blank()) +
          coord_cartesian( ylim = c(0, 1000))
    

    【讨论】:

    • 谢谢你,不幸的是,你建议的改进不允许我指定我想在每年每个图表的 x 轴上标记的日期(例如,仅 4 日和 6 日)八月)。在我的实际数据集中,这些日期发生在一年中的不同时间。如何将其添加到代码中(或更改为目前我的代码仅适用于 2015 年)?
    • 你可以在scale_x_date@JoannaOK 中使用breaks
    • 如果您不想手动列出日期,可以使用:breaks &lt;- c("0804", "0806"); years &lt;- 2015:2018; breaks &lt;- tibble(crossing(years,breaks), date = ymd(paste0(years,breaks))) %&gt;% pull(date) 然后scale_x_date(date_labels = "%b %d", breaks = breaks) +
    猜你喜欢
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 2013-06-07
    相关资源
    最近更新 更多