1

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!

5
  • A better format for the question would be to avoid the undersign and greetings. Commented Oct 12, 2012 at 13:40
  • I don't completely get your question: a) your x-axis should really stay continuous as it is. Just your layer has a gap: this you can achieve by putting a Jan - Aug layer and a Oct - Dec layer I think. b) you want to have a gap in the axis (the guide in ggplot terms). That IMHO is easiest achieved by facetting into Jan - Aug ./. Oct - Dec. Commented Oct 12, 2012 at 14:24
  • Just use a missing value. It will break the line. Commented Oct 13, 2012 at 14:45
  • @hadley, can you explain? It seems that missing values are interpolated with geom_area (but not, for instance, geom_line). So using the OP's code on the linked post, if we set all "Sep" values to NA, there's no break in the areas at Sep. Commented Oct 13, 2012 at 21:15
  • If geom_area doesn't break with NA that's an error. If someone provides a minimal self-contained example, I can explore further. Commented Oct 14, 2012 at 23:13

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! Worked perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.