Tiffany
Tiffany

Reputation: 301

R side by side barplot, scale bar by bin size

I am trying to scale the barplot bar width by my bin size (colnames) to show skewness of the data (e.g., the 11-10 bin will be narrower than the 251-500 bin). I am using a side by side barplot, using rownames as my groups (A, B). It seems that the width= argument is only taking the first 2 width values, and assigning one to each group, as opposed to assigning the width values to each bar individually. Can anyone suggest a way to do this?

df <- rbind(runif(10,0,1), runif(10,0,1))
rownames(df) <- c("A", "B")
colnames(df) <- c("0", "1-10", "11-20", "21-30", "31-40", "41-50", "51-75",    "76-125", "126-250", "251-500") 
bar_width = rep(c(1, rep(10,5), 25, 50, 125, 250), each=2)

barplot(df, width=bar_width, beside=T)

Upvotes: 3

Views: 485

Answers (1)

thelatemail
thelatemail

Reputation: 93813

Here's an attempt in base R plotting:

bp <- barplot(c(df), width=bar_width, 
        space=rep(c(40,0),ncol(df))/mean(bar_width), col=gray.colors(nrow(df)))
axis(1, at=colMeans(matrix(bp,nrow=2)), labels=colnames(df),
     lwd=0, lwd.tick=1, cex.axis=0.7, las=2)

enter image description here

Upvotes: 1

Related Questions