How can I get geom_area () to leave a space for missing values?

When I use geom_area() , I expect it to be very similar to geom_bar() , but I am a bit perplexed by this behavior for 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) # I also have no idea why geom_area needs the data.frame to be sorted first. test[test$x==4,"y"] <- NA ggplot(test, aes(x, y, fill=z)) + geom_bar(stat="identity", position="stack") 

Produces this glass histogram. Graph using stack_bar ()

However, if I go to stack_area (), it interpolates for 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 I add na.rm=FALSE or na.rm=TRUE , it does not matter.

ggplot (test, aes (x, y, fill = z)) + geom_area (stat = "identity", position = "stack", na.rm = TRUE) Warning message: 3 lines containing missing values ​​were deleted (position_stack)

Graph with na.rm = TRUE

ggplot (test, aes (x, y, fill = z)) + geom_area (stat = "identity", position = "stack", na.rm = FALSE) Warning message: 3 lines containing missing values ​​(position_stack) were deleted.

Graph with na.rm = FALSE

Obviously, everything I'm trying does not work. How can I show space in series with stack_area() ?

+5
source share
1 answer

The problem seems to be related to how the values ​​add up. The error message reports that the lines containing the missing values ​​have been deleted, so there is simply no space in the data you draw.

However, geom_ribbon , of which geom_area is a special case, leaves spaces for missing values. geom_ribbon also displays the area, but you must specify the maximum and minimum y values. Thus, the trick can be done by calculating these values ​​manually, and then building with geom_ribbon() . Starting with your test data frame, I create the ymax and ymax as follows:

 test$ymax <-test$y test$ymin <- 0 zl <- levels(test$z) for ( i 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 then geom_ribbon using geom_ribbon :

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

This gives the following graph:

enter image description here

+4
source

All Articles