r - How can I make geom_area() leave a gap for missing values? -


when plot using geom_area() expect perform lot geom_bar(), i'm little perplexed behavior missing values.

    require(dplyr)     require(ggplot2)      set.seed(1)      test <- data.frame(x=rep(1:10,3), y=abs(rnorm(30)), z=rep(letters[1:3],10)) %>% arrange(x,z)   # have no idea why geom_area needs data.frame sorted first.      test[test$x==4,"y"] <- na      ggplot(test, aes(x, y, fill=z)) + geom_bar(stat="identity", position="stack")  

produces stacked bar chart. graph using stack_bar()

however, if change stack_area() interpolates across missing values.

> ggplot(test, aes(x, y, fill=z)) + geom_area(stat="identity", position="stack") warning message: removed 3 rows containing missing values (position_stack).  

graph using stack_area()

if add in na.rm=false or na.rm=true makes no difference.

ggplot(test, aes(x, y, fill=z)) + geom_area(stat="identity", position="stack", na.rm=true) warning message: removed 3 rows containing missing values (position_stack)

graph na.rm=true

ggplot(test, aes(x, y, fill=z)) + geom_area(stat="identity", position="stack", na.rm=false) warning message: removed 3 rows containing missing values (position_stack).

graph na.rm=false

obviously, whatever i'm trying isn't working. how can show gap in series stack_area()?

it seems problem has how values stacked. error message tells rows containing missing values removed, there no gap present in data plotting.

however, geom_ribbon, of geom_area special case, leaves gaps missing values. geom_ribbon plots area well, have specify maximum , minimum y-values. trick can done calculating these values manually , plotting geom_ribbon(). starting data frame test, create ymin , ymax data follows:

test$ymax <-test$y test$ymin <- 0 zl <- levels(test$z) ( in 2:length(zl) ) {    zi <- test$z==zl[i]    zi_1 <- test$z==zl[i-1]    test$ymin[zi] <- test$ymax[zi_1]    test$ymax[zi] <- test$ymin[zi] + test$ymax[zi] } 

and plot geom_ribbon:

ggplot(test, aes(x=x,ymax=ymax,ymin=ymin, fill=z)) + geom_ribbon() 

this gives following plot:

enter image description here


Comments