user1471980
user1471980

Reputation: 10626

is it possible to to have a fixed width on geom_bar

I am working with bunch of different data frames which could have multiple variables or a singel variable:

my data frame looks like this: x

Team         Grade
Football      10

When I do this, bar size is huge. I would like to have a fix size so that it looks good when there is one or 2 rows of data:

ggplot(x, aes(Team, Grade, group=Team)) + geom_bar(aes(fill=Team))

Upvotes: 1

Views: 2548

Answers (1)

Drew Steen
Drew Steen

Reputation: 16607

If your data look like this:

df <- data.frame(Team=gl(4, 1), Grade=sample(1:4))

You can specify the width of the bar as follows:

ggplot(df, aes(x=Team, y=Grade, fill=Team)) + geom_bar(width=0.2)

Which gives this plot:

enter image description here

A similar data set, but with only one Team, is given by:

ggplot(subset(df, Team==1), aes(x=Team, y=Grade, fill=Team)) + geom_bar(width=0.2)

enter image description here

Which looks OK, I think.

If you are more concerned about the situation where you only have one Team (or a few), and the bars all go to the top of the plot, you can set y limits:

+ scale_y_continuous(limits=c(0, 4))

enter image description here

Upvotes: 4

Related Questions