rggplot2plottimeserieschartgeom-area

How to create a timeseries plot using facet_wrap of ggplot2


This is a follow up question on How to format the x-axis of the hard coded plotting function of SPEI package in R?. in my previous question, I had a single location dataset that needed to be plotted, however, in my current situation, I have dataset for multiple location (11 in total) that in needed to plot in a single figure. I tried to replicate same code with minor adjustment, however, the code do not produce the right plot. also I do not see dates break on the x-axis. Any help would be appreciated.

library(SPEI)
library(tidyverse)
library(zoo)

data("balance")
SPEI_12=spei(balance,12)
SpeiData=SPEI_12$fitted

myDate=as.data.frame(seq(as.Date("1901-01-01"), to=as.Date("2008-12-31"),by="months"))
names(myDate)= "Dates"
myDate$year=as.numeric(format(myDate$Dates, "%Y"))
myDate$month=as.numeric(format(myDate$Dates, "%m"))
myDate=myDate[,-1]
newDates = as.character(paste(month.abb[myDate$month], myDate$year, sep = "_" )) 

DataWithDate = data.frame(newDates,SpeiData) 
df_spei12 = melt(DataWithDate, id.vars = "newDates" )
SPEI12 = df_spei12 %>% 
  na.omit() %>% 
  mutate(sign = ifelse(value >= 0, "pos", "neg")) 
SPEI12 = SPEI12%>% 
  spread(sign,value) %>% 
  replace(is.na(.), 0)

ggplot(SPEI12) + 
  geom_area(aes(x = newDates, y = pos), col = "blue") +
  geom_area(aes(x = newDates, y = neg),  col = "red") +
  facet_wrap(~variable)+
  scale_y_continuous(limits = c(-2.5, 2.5), breaks = -2.5:2.5) +
  scale_x_discrete(breaks=c(1901,1925,1950,1975,2000,2008))+
  ylab("SPEI") + ggtitle("12-Month SPEI") +
  theme_bw() + theme(plot.title = element_text(hjust = 0.5, size = 16, face = "bold"))+
  theme(axis.text = element_text(size=12, colour = "black"), axis.title = element_text(size = 12,face = "bold"))

Here is what the code produces- instead of area plot it is producing bar plots. enter image description here


Solution

  • With geom_area I was returning an error in the fill of the plot (a superposition), so I used geom_bar.

    library(SPEI)
    library(tidyverse)
    library(zoo)
    library(reshape2)
    library(scales)
    
    data("balance")
    SPEI_12=spei(balance,12)
    SpeiData=SPEI_12$fitted
    
    myDate=as.data.frame(seq(as.Date("1901-01-01"), to=as.Date("2008-12-31"),by="months"))
    names(myDate)= "Dates"
    myDate$year=as.numeric(format(myDate$Dates, "%Y"))
    myDate$month=as.numeric(format(myDate$Dates, "%m"))
    myDate=myDate[,-1]
    newDates = as.character(paste(month.abb[myDate$month], myDate$year, sep = "_" )) 
    
    DataWithDate = data.frame(newDates,SpeiData) 
    df_spei12 = melt(DataWithDate, id.vars = "newDates" )
    SPEI12 = df_spei12 %>% 
      na.omit() %>% 
      mutate(sign = ifelse(value >= 0, "pos", "neg")) 
    ###
    SPEI12_md <- SPEI12 %>% 
      dplyr::mutate(Date = lubridate::parse_date_time(newDates, "m_y"),
                    Date = lubridate::ymd(Date),
                    variable = as.factor(variable))
    levels(SPEI12_md$variable) <- c("Indore", "Kimberley", "Albuquerque", "Valencia",
                                    "Viena", " Abashiri", "Tampa", "São Paulo", 
                                    "Lahore", "Punta Arenas", "Helsinki")
    
    v <- 0.1 # 0.1 it is a gap
    v1 <- min(SPEI12_md$value) - v
    v2 <- max(SPEI12_md$value) + v
    
    vv <- signif(max(abs(v1), abs(v2)), 2)
    
    ggplot2::ggplot(SPEI12_md) + 
      geom_bar(aes(x = Date, y = value, col = sign, fill = sign),
               show.legend = F, stat = "identity") +
      scale_color_manual(values = c("pos" = "darkblue", "neg" = "red")) +
      scale_fill_manual(values = c("pos"  = "darkblue", "neg" = "red")) +
      facet_wrap(~variable) +
      scale_x_date(date_breaks = "10 years",
                   labels = scales::date_format("%Y-%m")) + # 
      scale_y_continuous(limits = c(-vv, vv), breaks = c(seq(-vv-v, 0, length.out = 3),
                                                         seq(0, vv+v,  length.out = 3))) +
      ylab("SPEI") + ggtitle("12-Month SPEI") +
      theme_bw() + theme(plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
                         axis.text = element_text(size=12, colour = "black"),
                         axis.title = element_text(size = 12,face = "bold"),
                         axis.text.x = element_text(angle = 90, size = 10))
    

    enter image description here