Search code examples
rggplot2missing-datastacked-area-charttimeserieschart

introducing a gap in continuous x axis using ggplot


This is kinda a build-on on my previous post creating an stacked area/bar plot with missing values (all the script I run can be found there). In this post, however, Im asking if its possible to leave a gap in an continuous x axis? I have a time-serie (month-by-month) over a year, but for one sample one month is missing and I would like to show this month as a complete gap in the plot. Almost like plotting a graph for Jan-Aug (Sep is missing) and one for Oct-Dec and merging these with a gap for Sep.

The only things I have come up trying are treating the missing month as zero or NA, creating a hugh drop in the area chart for Sep or excluding it but with an x axis ranging from 1-11, respectively (see plots in dropbox folder).

The data set Im working on can be found in my dropbox folder and it's named r_class.txt and you can also see the two different plots (Rplots1 and 2).

Any ideas would really be appreciated!


Solution

  • Plot the series as two separate data frames:

    #Load libraries
    require(ggplot2)
    require(reshape)
    
    #Code copied from your linked post:
    wa=read.table('wa_class.txt', sep="", header=F, na.string="0")
    names(wa)=c("Class","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
    wam=melt(wa)
    wam$variablen=as.numeric(wam$variable)
    
    #For readability, split the melted data frame into two separate data frames
    wam1 <- wam[wam$variablen %in% 1:6,]
    wam2 <- wam[wam$variablen %in% 8:12, ]
    
    ggplot() +
      geom_area(data=wam1, aes(x=variablen, y=value, fill=Class)) +
      geom_area(data=wam2, aes(x=variablen, y=value, fill=Class))
      #and add lineranges, etc., accordingly