2

Hi I got a data frame weekly.mean.values with the following structure:

week:mean:ci.lower:ci.upper

Where week is a factor; mean, ci.lower and ci.upper are numeric. For each week, there is only one mean, and one ci.lower or ci.upper.

I was trying to plot a shaded area inside of the 95% confidence interval around the mean, with the following code:

ggplot(weekly.mean.values,aes(x=week,y=mean)) + 
    geom_line() + 
    geom_ribbon(aes(ymin=ci.lower,ymax=ci.upper))

The plot, however, came out blank (that is only with x-axis and y-axis present, but no lines, or points, let alone shaded areas).

If I removed the geom_ribbon part, I did get a line. I know that this should be a very simple task but I don't know why I couldn't get geom_ribbon to plot what I wanted. Any hint would be truly appreciated.

joran
  • 169,992
  • 32
  • 429
  • 468
MM Cui
  • 51
  • 6
  • 3
    You should provide a small [reproducible](http://stackoverflow.com/q/5963269/324364) example that illustrates this behavior. It's difficult for us to magically know what's happening on your computer. – joran May 26 '12 at 04:26

3 Answers3

3

I realize this thread is super old, but google still find it.

The answer is that you need to set the ymin and ymax to use a part of the data you are using on the y-axis. It you set them to scalar values then the ribbon covers the entire plot from top to bottom.

You can use

ymin=0 
ymax=mean

to go from 0 to your y-point or even

ymin=mean-1
ymax=mean+1

to have the ribbon cover a strip encompassing your actual data.

Swift Sharp
  • 2,604
  • 3
  • 30
  • 54
1

I may be missing something, but the ribbon will be plotted filled with grey20 by default. You are plotting this layer on top of the data so no wonder it obscures it. Also, it is also possible that the limits for the plot axes derived from the data provided to the initial ggplot() call will not be sufficient to contain the confidence interval ribbon. In that case, I would not be surprised to see a grey/blank plot.

To see if this is the problem, try altering your geom_ribbon() line to:

geom_ribbon(aes(ymin=ci.lower,ymax=ci.upper), alpha = 0.5)

which will plot the ribbon with transparency whic should show the data underneath if the problem is what I think it is.

If so, set the x and y limits to the range of the data +/- the confidence interval you wish to plot and swap the order of the layers (i.e. draw the line on top of the ribbon), and use transparency in the ribbon to show the grid through it.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
1

From ggplot's docs for geom_ribbon (2.1.0):

For each continuous x value, geom_interval displays a y interval. geom_area is a special case of geom_ribbon, where the minimum of the range is fixed to 0.

In this case, x values cannot be factors for geom_ribbon. One solution would be to convert week from a factor to a numeric. e.g.

ggplot(weekly.mean.values,aes(x=as.numeric(week),y=mean)) + 
  geom_line() + 
  geom_ribbon(aes(ymin=ci.lower,ymax=ci.upper))

geom_line should handle the switch from factor to numeric without incident, although the X axis scale may display differently.

Dave
  • 2,396
  • 2
  • 22
  • 25