Sunday, December 30, 2012

ggplot2 trick

Here's some code from Hadley's book that assumes you've created ggplot objects a, b, and c. It puts a in the top row, with b and c in the bottom row.
grid.newpage()
pushViewport(viewport(layout=grid.layout(2,2)))
vplayout<-function(x,y){
    viewport(layout.pos.row=x,layout.pos.col=y) 
}
print(a,vp=vplayout(1,1:2))
print(b,vp=vplayout(2,1))
print(c,vp=vplayout(2,2))
Created by Pretty R at inside-R.org

  • different line style from geo_smooth in different facet_wrap
p1 <- ggplot(ds2, aes(x=visit, y=change))+ geom_point(shape=1, 
aes(color=treat), position=position_jitter(width=0.4,height=0)) 
+ facet_grid(snp~race)+ opts(title = 'rsxxxx')
 
p1 <- p1 + geom_smooth(data=subset(ds2, race == 'CA'), method='lm', 
se=F, color='blue', linetype = ifelse(pValues[pValues$'dbsnprsid' 
== 'rsxxxx', 'slope1_p'] < 0.05 ,"longdash", "solid" ))
 
Created by Pretty R at inside-R.org
  •  background color in the plotting area
+ theme_grey() - the default theme, with a grey background
+ theme_bw() - a theme with a white background 
Created by Pretty R at inside-R.org


No comments: