colin
colin

Reputation: 2666

Adding multiple categorical labels to a bar chart in R

Say I have some data on some experiment I conducted on Earth and on Wayne's World. There are control and treatment means:

means1<-c(1,2)
means2<-c(1.5,2.5)
data<-cbind(means1,means2)
rownames(data)=c('ctrl','treatment')
colnames(data)=c('Earth','Waynes World')

I would like to plot this data, so I do.

barplot(data,beside=T) 

This generates paired control and treatment bars, separated by planet. Each pair of bars has an x axis label specifying what planet they are from. What I would like is a second set of x-axis labels underneath each bar that specifies ctrl or treatment. Bonus if you tilt this second set of labels, they don't overlap the first labels, and everything looks pretty.

Upvotes: 0

Views: 316

Answers (1)

MrFlick
MrFlick

Reputation: 206197

I think something like this describes what you're after

bp<-barplot(data,beside=T, xaxt="n") 
mtext(text=rownames(data)[row(bp)], at=bp, line=1, side=1)
mtext(text=colnames(data), at=colMeans(bp), line=2.2, side=1)

enter image description here

Upvotes: 3

Related Questions