Joanna OK
Joanna OK

Reputation: 103

ggplot breaks as date month and day without year, show labels for chosen dates in r

I'm looking for a way to add labels on the x-axis of a line graph for specified dates that are the same each year.

I have a nested list with dates and river streamflow data (Flow) in different river reaches RCH (here for simplicity just reach no. 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)))

I've created a function that creates a graph for each year and plot on it Flows coming from various reaches (here for simplicity I just used a single reach 910). I want to add on the x-axis labels for several specified dates (month and day) that are the same each year. I didn't know how to make it work for every year so instead, I've only added it for year 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))
}

And I've applied it to my list:

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

In the resulting graph date labels onx-axis and matching major grid lines only appear in 2015:

enter image description here

Where I was hoping to achieve something like the one below, where every year has the same date labels and major grid lines marking them:

enter image description here

How can I change the code in the function so it doesnt just apply to 2015 but also any other years in the dataset?

Also I don't understand why the graph for 2015 is smaller then the other graphs...

I'll be thankful for help

Upvotes: 2

Views: 1683

Answers (1)

Vin&#237;cius F&#233;lix
Vin&#237;cius F&#233;lix

Reputation: 8811

I recommend that you use a single data.frame to use facet for each year.

    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))

enter image description here

Upvotes: 1

Related Questions