3

I am using ggplot2 package to plot a stacked area of species abundance.

My code:

#Aggragate 2 column
    x=IBTS[,3:4]
    Datehour=paste(IBTS$Date.,IBTS$Time.)
    Date<-strptime(Datehour,"%d/%m/%Y %H:%M")

#Add a new column with the date
    IBTS$Date
    IBTS<-cbind(IBTS,Date)

#Plotting the data
    p<-ggplot(IBTS,aes(x=Date,y=Number.of.Particles.))
    p+geom_area(aes(fill=Selection.set.),position="stack")+  
    theme_bw()+ scale_fill_brewer(palette="Blues")

I obtain this plot:

enter image description here

I have a simply question: On Janv. 30, I have NA values on my dataframe but these values are not considered in my plot.

Does anyone have any idea?

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • 3
    Could you include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610)? – Jaap Feb 16 '16 at 11:49
  • what do you mean "not considered"? you would like to have a discontinuity (a white gap)? – agenis Feb 16 '16 at 12:59
  • 3
    seems duplicate of http://stackoverflow.com/q/30116606/3871924 – agenis Feb 16 '16 at 13:03
  • As Agenis asked, I would have discontinuity on this short period and would have the same result on the previous link. I tried with the geom_ribbon function but I have never built a loop... – Arnaud Louchart Feb 16 '16 at 13:40
  • Could be the OP is complaining about a newish `ggplot2` bug then. Not sure what the problem is here, but if it is that he should have blanks (white space) where there is interpolation, then check this.http://stackoverflow.com/questions/35454277/can-you-make-geom-ribbon-leave-a-gap-for-missing-values/35461050#35461050 – Mike Wise Feb 17 '16 at 19:54
  • I posted an issue and Hadley fixed it, so this might be fixed now. Not sure because it is `geom_area` and not `geom_ribbon`. https://github.com/hadley/ggplot2/commit/a760ff627582da59f938af14b0f01d54a4a97aa7 – Mike Wise Feb 18 '16 at 02:25
  • And I found a one-liner workaround. See the answer below. – Mike Wise Feb 18 '16 at 09:40

2 Answers2

2

The complaint seems to be that there should be a blank white space at "Janv. 30" because there is missing data there. geom_area is based on GeomRibbon and there was a bug there that was causing this to happen which I posted to GitHub. Hadley actually just fixed it now, but the version is not released of course.

There is a workaround for now that I posted yesterday at this link Can you make geom_ribbon leave a gap for missing values?.

Update

Before I had you create a new geom to fix this, now I discovered a one-line that does the same thing:

GeomRibbon$handle_na <- function(data, params) { data }

p<-ggplot(IBTS,aes(x=Date,y=Number.of.Particles.))
p+geom_ribbon_na(aes(fill=Selection.set.),position="stack")+  
theme_bw()+ scale_fill_brewer(palette="Blues")

It is not an obvious dup since you have to know that the two layers geom_area and geom_ribbon use the same ggproto code

Community
  • 1
  • 1
Mike Wise
  • 22,131
  • 8
  • 81
  • 104
0

I fixed the problem by creating 2 lines of null data according to the limits of the interpollation. Now, the new interpollation has made a "blank" a this place. I just want to thank you for your help