R Graphics Cookbook

Winston Chang

Mentioned 2

"Practical recipes for visualizing data"--Cover.

More on Amazon.com

Mentioned in questions and answers.

This is not a programming question per se, but seeing as how R and ggplot2 are popular here I thought I'd ask if anyone knows if there is a way to download all of the documentation for ggplot2 at http://docs.ggplot2.org so that they can be accessed offline. I am frequently in a situation where internet access is not available.

What about the R Graphics Cookbook by Winston Chang?

Edit: Or using wget, as Ben Bolker suggests:

 wget --recursive --no-clobber --page-requisites --html-extension --convert-links   --restrict-file-names=windows --domains=docs.ggplot2.org  http://docs.ggplot2.org/current/

The --domains option should prevent to follow links outside of http://docs.ggplot2.org (like those at the bottom of the page). However I did not test that.

I want to plot stacked bar plot with ggplot2 and am having difficulty getting the colour mapping and stack order right. The following example has been developed from this SO answer to achieve a non-zero y-axis origin, but as you can see it creates other problems. The colours don't map properly and the plot order is wrong. Grateful for any pointers on the best way to handle this. The desired output should scale colours as per the factor levels for rating, with colours in the order specified.

require(ggplot2)
d = data.frame(grp = rep(c('A','B'), each = 4), 
               rating = rep(c('bad','poor','ok','good'), 2),
               value = c(15,45,35,5,5,15,55,30), stringsAsFactors = F)

if(require(reshape2)) reshape2::dcast(d, grp ~ rating) # show structure

d$rating = ordered(d$rating, levels=c('bad','poor','ok','good'))
d$grp = ordered(d$grp, levels=c('B','A'))

# split datsets so we can plot 'negative' bars
d1 = subset(d, rating %in% c('ok','good'))
d2 = subset(d, rating %in% c('poor','bad'))

ggplot() + 
  geom_bar(data = d1, aes(x=grp, y=value, fill=rating), stat='identity', position='stack') + 
  geom_bar(data = d2, aes(x=grp, y=-value, fill=rating), stat='identity', position='stack') + 
  scale_fill_manual(values=c('red','pink','lightgreen','green')) +
  geom_line(data=d1, aes(x=c(.5,2.5), y=c(0,0)), size=2, linetype='dashed') +
  coord_flip()

enter image description here

Perhaps a bit of reordering and using limits() will help:

d2 <- d2[order(d2$rating, decreasing =T),]

ggplot() + 
     geom_bar(data = d1, aes(x=grp, y=value, fill=rating), stat='identity',
               position='stack') + 
     geom_bar(data = d2, aes(x=grp, y=-value, fill=rating), stat='identity', 
               position='stack') + 
     scale_fill_manual(values=c('red','pink','lightgreen','green'),
                       limits=c("bad","poor","ok","good"))+
     geom_line(data=d1, aes(x=c(.5,2.5), y=c(0,0)), size=2, linetype='dashed') +
     coord_flip()

enter image description here

For anyone who wishes to learn ggplot2, I strongly recommend getting the Winston Chang's R Graphics Cookbook.

Realated tags

ggplot2r