Pascal
Pascal

Reputation: 395

stacking geom_ribbon

I am trying to use geom_ribbon to mimic the behavior of geom_area but i am not successful. would you have any hint on why the following does not work ? I used Hadley's statement from ggplot2 geom_area web pages : "An area plot is a special case of geom_ribbon, where the minimum of the range is fixed to 0, and the position adjustment defaults to position_stacked."

test <- expand.grid(Param = LETTERS[1:3], x = 1:5)
test$y <- test$x

# Ok
p <- ggplot(test)
p <- p + geom_area(aes(x = x, y = y, group = Param, fill = Param), alpha = 0.3)
p

# not ok - initial idea
p <- ggplot(test)
p <- p + geom_ribbon(aes(x = x, ymin = 0, ymax = y, group = Param, fill = Param), alpha = 0.3, position = position_stack())
p

further, how can I look in the code of functions coded the way geom_XXX are? my traditional way gives the following, which is not very usefull:

> geom_ribbon
function (mapping = NULL, data = NULL, stat = "identity", position = "identity", 
    na.rm = FALSE, ...) 
GeomRibbon$new(mapping = mapping, data = data, stat = stat, position = position, 
    na.rm = na.rm, ...)

Thanks for your help Regards Pascal

Upvotes: 1

Views: 2448

Answers (1)

joran
joran

Reputation: 173727

You just didn't map a variable to y in your geom_ribbon call. Adding y = y causes it to work for me. In general, geom_ribbon doesn't require a y aesthetic, but I believe it does in the case of stacking. I presume there's a well-thought out reasoning for why that is, but you never know...

Also, all the source code for ggplot2 is on github.

Upvotes: 2

Related Questions