Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable width bars in ggplot2 barplot in R

Tags:

r

ggplot2

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()   
like image 867
Steve Avatar asked May 10 '11 10:05

Steve


People also ask

How do I change the width of a barplot bar in R?

To Increase or Decrease width of Bars of BarPlot, we simply assign one more width parameter to geom_bar() function. We can give values from 0.00 to 1.00 as per our requirements.

How do I make the bars wider in R?

To make the bars narrower or wider, set the width of each bar with the width argument. Larger values make the bars wider, and smaller values make the bars narrower. To add space between bars, specify the space argument. The default value is 0.2.

How do I increase the space between grouped bars in ggplot2?

For grouped bars, there is no space between bars within each group by default. However, you can add some space between bars within a group, by making the width smaller and setting the value for position_dodge to be larger than width.

What does Geom_bar do in R?

geom_bar() makes the height of the bar proportional to the number of cases in each group (or if the weight aesthetic is supplied, the sum of the weights). If you want the heights of the bars to represent values in the data, use geom_col() instead.


Video Answer


1 Answers

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

like image 129
chl Avatar answered Sep 26 '22 04:09

chl