rggplot2

Facet Wrap/Facet Grid Plot in ggplot: Multiple date problem


I have the time series data containing ten stations in CSV formart. Each station consist of three parameters (SPEI3, SPEI6 and SPEI12). Each SPEI category has its own start date. The sample data for two stations is as follows;

date,SPEI,Bulongwa,Igembe
3/1/1990,SPEI3,-0.633666276,-1.091342548
4/1/1990,SPEI3,-1.154270937,-1.260999014
5/1/1990,SPEI3,-1.730380247,-1.918347545
6/1/1990,SPEI3,-1.554194439,-1.669196002
7/1/1990,SPEI3,-0.585667705,-0.530214816
8/1/1990,SPEI3,-0.174442879,0.058721878
9/1/1990,SPEI3,1.177516783,0.488334655
10/1/1990,SPEI3,0.337872023,0.321737439
11/1/1990,SPEI3,-0.862164636,-0.237407495
12/1/1990,SPEI3,-1.022895024,-0.928489143
6/1/1990,SPEI6,-1.397632538,-0.722279406
7/1/1990,SPEI6,-1.338037747,-0.663393368
8/1/1990,SPEI6,-1.773644884,-0.835592437
9/1/1990,SPEI6,-1.469993994,-0.731273761
10/1/1990,SPEI6,-0.640571906,-0.237666955
11/1/1990,SPEI6,-0.963494314,-0.222447386
12/1/1990,SPEI6,-0.963266132,-0.50631387
12/1/1990,SPEI12,-1.743275704,-0.704998147
1/1/1991,SPEI12,-1.398853355,-0.668119127
2/1/1991,SPEI12,-1.630408685,-0.781252647
3/1/1991,SPEI12,-1.451362535,-0.716740124
4/1/1991,SPEI12,-1.162635355,-0.554554904
5/1/1991,SPEI12,-0.723093595,-0.513940083
6/1/1991,SPEI12,-0.710841553,-0.511117277
7/1/1991,SPEI12,-0.682616494,-0.517770976
8/1/1991,SPEI12,-0.70090681,-0.5207414
9/1/1991,SPEI12,-0.748674903,-0.532204739
10/1/1991,SPEI12,-0.595583026,-0.502605309
11/1/1991,SPEI12,-0.383636489,-0.429787626
12/1/1991,SPEI12,0.146175789,-0.197058223

The full dataset can be found in the following link. SPEI Data

Using the following code, plotting with a single station works just fine.

Code:

# Libraries
library(ggplot2)
library(ggpubr)
#Loading Data
df <- read.csv("SPEI.csv")

#Create Rdate format
dates <- as.Date(df$date, format = '%m/%d/%Y')
df$date <- dates


# Plot
ggplot(df, aes(x = date, y = Bulongwa, fill=SPEI))+
  geom_area(position = 'identity')+
  theme_pubclean()+
  scale_fill_manual(values = c("#003333","#00CCFF", "#996600"), name = NULL)+
  scale_x_date(date_breaks = "2 years",date_labels = "%Y", expand = c(0,0)) +
  theme(axis.text.x = element_text(angle=90, vjust=.5))+
  labs(x = "Date: [Years]", y = 'SPEI-Drought / Wetness Index')+
  theme(plot.title = element_text(hjust = 0.5, size=14, face= "bold", colour= "black"))+
  theme(plot.title = element_text(hjust = 0.5), legend.position = 'bottom')+
  theme(strip.text = element_text(size =11.5, face="bold"),
        legend.title = element_text(colour = "black", size =12, face="bold"),
        legend.text = element_text(colour = "black", size =12,  face="bold"),
        axis.title.y = element_text(colour = "black", size =14, face="bold"),
        axis.text.y = element_text(colour = "black", size =14),
        axis.title.x = element_text(colour = "black", size =12, face="bold"),
        axis.text.x = element_text(colour = "black", size =15))+
  theme(panel.spacing.x = unit(1, "lines"),
        panel.border = element_rect(fill = 'transparent',  colour = "#BFD5E3"))

Results: enter image description here

However, if I reshape my data and plot both station using facet_wrap(~variable) the output is a mess. The modified code and resuts are as follows.

Code: 
# Libraries
library(ggplot2)
library(reshape)
library(ggpubr)

#Loading Data
df <- read.csv("SPEI.csv")

#Create Rdate format
dates <- as.Date(df$date, format = '%m/%d/%Y')
dt = melt(df, id.vars = c("SPEI","date"))
dt$date <- dates
head(dt)

# Plot
ggplot(dt, aes(x = date, y = value, fill=SPEI))+  
  geom_area(position = 'identity')+
  theme_pubclean()+
  scale_fill_manual(values = c("#003333","#00CCFF", "#996600"), name = NULL)+
  scale_x_date(date_breaks = "2 years",date_labels = "%Y", expand = c(0,0)) +
  theme(axis.text.x = element_text(angle=90, vjust=.5))+
  facet_wrap(~variable)+
  labs(x = "Date: [Years]", y = 'SPEI-Drought / Wetness Index')+
  theme(plot.title = element_text(hjust = 0.5, size=14, face= "bold", colour= "black"))+
  theme(plot.title = element_text(hjust = 0.5), legend.position = 'bottom')+
  theme(strip.text = element_text(size =11.5, face="bold"),
        legend.title = element_text(colour = "black", size =12, face="bold"),
        legend.text = element_text(colour = "black", size =12,  face="bold"),
        axis.title.y = element_text(colour = "black", size =14, face="bold"),
        axis.text.y = element_text(colour = "black", size =14),
        axis.title.x = element_text(colour = "black", size =12, face="bold"),
        axis.text.x = element_text(colour = "black", size =15))+
  theme(panel.spacing.x = unit(1, "lines"),
        panel.border = element_rect(fill = 'transparent',  colour = "#BFD5E3"))

Results: enter image description here

My desired output is as follows: enter image description here

Is there a workaround for this?


Solution

  • There are 5 NAs (I assume) written as "#NAME?" within the Igembe column. This causes the Igembe column to be read in as class Character. After calling melt(), the resulting value column is of class Character as well, which is why your plot looks a bit odd. If you're alright with these observations being treated as NAs and not being included within your plot, you can cast the Igembe column to numeric with as.numeric() before calling melt(). The "#NAME?" observations will be coded as NA.

    Code:

    library(ggplot2)
    library(reshape)
    library(ggpubr)
    
    #Loading Data
    df <- read.csv("SPEI.csv")
    
    # Cast column to numeric
    df$Igembe <- as.numeric(df$Igembe)
    
    #Create Rdate format
    dates <- as.Date(df$date, format = '%m/%d/%Y')
    dt = melt(df, id.vars = c("SPEI","date"))
    dt$date <- dates
    head(dt)
    
    # Plot
    ggplot(dt, aes(x = date, y = value, fill=SPEI))+  
      geom_area(position = 'identity')+
      theme_pubclean()+
      scale_fill_manual(values = c("#003333","#00CCFF", "#996600"), name = NULL)+
      scale_x_date(date_breaks = "2 years",date_labels = "%Y", expand = c(0,0)) +
      theme(axis.text.x = element_text(angle=90, vjust=.5))+
      facet_wrap(~variable)+
      labs(x = "Date: [Years]", y = 'SPEI-Drought / Wetness Index')+
      theme(plot.title = element_text(hjust = 0.5, size=14, face= "bold", colour= "black"))+
      theme(plot.title = element_text(hjust = 0.5), legend.position = 'bottom')+
      theme(strip.text = element_text(size =11.5, face="bold"),
            legend.title = element_text(colour = "black", size =12, face="bold"),
            legend.text = element_text(colour = "black", size =12,  face="bold"),
            axis.title.y = element_text(colour = "black", size =14, face="bold"),
            axis.text.y = element_text(colour = "black", size =14),
            axis.title.x = element_text(colour = "black", size =12, face="bold"),
            axis.text.x = element_text(colour = "black", size =15))+
      theme(panel.spacing.x = unit(1, "lines"),
            panel.border = element_rect(fill = 'transparent',  colour = "#BFD5E3"))
    
    

    output: enter image description here