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()
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()
For anyone who wishes to learn ggplot2, I strongly recommend getting the Winston Chang's R Graphics Cookbook.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments