Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying geom_ribbon borders

Tags:

plot

r

ggplot2

I am plotting a series of means and standard deviations over time with code below, and am trying to use geom_ribbon to display the sd's, see below.

Due to the significant overlap I'd like to add a border to the ribbons that is the same color as the corresponding variable but is a dashed line, but I can't figure out where in the code this would go. I know "colour" and "linetype" commands are involved somehow...

Thanks!

graph.msd <- ggplot(data=g.data, aes(x=quarter,y=mean,group=number)) 
graph.msd <- graph.msd + geom_line(aes(colour = number),size=1)+geom_ribbon(aes(ymin=mean-sd,ymax=mean+sd,fill=number),linetype=2,alpha=0.1)
like image 483
km5041 Avatar asked Mar 20 '13 00:03

km5041


Video Answer


1 Answers

You need pass a value for colour to geom_ribbon something like

graph.msd <- graph.msd + 
 geom_line(aes(colour = number),size=1)+
 geom_ribbon(aes(ymin = mean-sd, ymax = mean+sd, 
                 fill = number,colour = number), linetype=2, alpha=0.1)

with a reproducible example (using a variant on the examples in ?geom_ribbon

huron <- data.frame(year = 1875:1972, level = as.vector(LakeHuron))
library(plyr) # to access round_any
huron$decade <- round_any(huron$year, 10, floor)

ggplot(huron, aes(x =year, group = decade)) + 
  geom_ribbon(aes(ymin = level-1, ymax = level+1, 
      colour = factor(decade), fill = factor(decade)), 
    linetype = 2, alpha= 0.1)

enter image description here

like image 171
mnel Avatar answered Oct 31 '22 19:10

mnel