27

I'm trying to produce a barplot with bar widths determined by an integer variable (sample size). Adding "width = variableName" doesn't seem to work. Is there an established way of doing this? Here's some dummy data and code. I want the bar widths to be a function of variable d in this example.

dat <- data.frame(a=c("A", "B", "C"), b=c(0.71, 0.94, 0.85), d=c(32, 99, 18))

ggplot(dat, aes(x=a, y=b, fill=a)) +  
geom_bar(colour="black", size=.3) + 
theme_bw()  

1 Answer 1

43

How about using width= after rescaling your d vector, say by a constant amount?

ggplot(dat, aes(x=a, y=b, width=d/100)) + 
  geom_bar(aes(fill=a), stat="identity", position="identity")

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

@chi: that's great, thanks a lot. I'd already tried this approach, but without the 'stat' and 'position' arguments it didn't affect the bar widths.
Is it possible to add the width of the bars in the legend ?
THANK YOU! I was missing the position argument :)
how would you apply this to percent charts such the answer for stackoverflow.com/questions/46984296/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.