barplot - fixed width of bars

1,585 views
Skip to first unread message

Ellen Pape

unread,
Jan 27, 2015, 8:20:34 AM1/27/15
to ggp...@googlegroups.com
Hi all,

I have the following (imaginary) dataset

area<-c(rep("A",2),rep("B",5),rep("C",3))
area
station<-c(1:2,1:5,1:3)
station
var<-c(5.4,4.8,4.4,5.2,3.3,4.2,4.1,6.2,5.1,5.0)
dataset<-data.frame(area,station,var)

in every area (A,B,C) a variable (var) is measured in several stations. The number of stations varies between these areas.
I want to make a barplot, with a bar per station grouped per area. This is the code I used:

library(ggplot2)
plot<-ggplot(dataset, aes(x=area, y=var,group=station))+geom_bar(width=0.5,colour="black",fill="white",stat="identity",position=position_dodge(width=0.6),drop=F)+scale_y_continuous("Sediment depth averaged TOM (%)\n",limits=c(0,8),expand=c(0,0))+scale_x_discrete("")
plot

This is nice but the width of the bars varies per area since the number of stations investigated per area is different, i.e. the more stations in an area, the narrower the bars.

So, I would like to make the following adjustments:

- I want to have a fixed bar width regardless of the number of stations in each area
- For the labels on the x axis, I would like to have the stations first, and below the area

Can someone help me?

Thanks!
ellen

Ron Crump

unread,
Jan 27, 2015, 8:46:07 AM1/27/15
to ggp...@googlegroups.com
Hi Ellen,

> library(ggplot2)
> plot<-ggplot(dataset, aes(x=area,
> y=var,group=station))+geom_bar(width=0.5,colour="black",fill="white",stat="identity",position=position_dodge(width=0.6),drop=F)+scale_y_continuous("Sediment
> depth averaged TOM (%)\n",limits=c(0,8),expand=c(0,0))+scale_x_discrete("")
> plot
>
> This is nice but the width of the bars varies per area since the number
> of stations investigated per area is different, i.e. the more stations
> in an area, the narrower the bars.
>
> So, I would like to make the following adjustments:
>
> - I want to have a fixed bar width regardless of the number of stations
> in each area
> - For the labels on the x axis, I would like to have the stations first,
> and below the area
>
> Can someone help me?

I don't know how to modify your code to do exactly what you want,
but have you considered variations on:

# facet_wrap on area
ggplot(dataset,
aes(y=var,x=station))+geom_bar(stat="identity")+facet_wrap(~area)+scale_y_continuous("Sediment
depth averaged TOM
(%)\n",limits=c(0,8),expand=c(0,0))+scale_x_discrete("Station")

or

# fill by area
ggplot(dataset,
aes(y=var,x=paste(area,station,sep=""),fill=area))+geom_bar(stat="identity")+scale_y_continuous("Sediment
depth averaged TOM
(%)\n",limits=c(0,8),expand=c(0,0))+scale_x_discrete("Station",labels=station)

This does have fixed width, but does not have the area-over-station
labeling (which is redundant using these graphs).

Ron.

Dennis Murphy

unread,
Jan 27, 2015, 4:21:38 PM1/27/15
to Ellen Pape, ggplot2
Hi:

Here are a couple of suggestions. I think the first is what you were after.

library(ggplot2)
ggplot(dataset, aes(x = factor(station), y = var)) +
geom_bar(stat = "identity", col = "black", fill = "white") +
facet_grid(. ~ area, scales = "free_x", space = "free") +
labs(x= "station")

The "free_x" argument allows for varying numbers of levels per facet;
the space = "free" argument resizes the space proportionally in each
facet to the number of available levels.

As far as the labels are concerned, your request is possible but
requires some surgery with the grid and gtable packages. The
suggestion to facet by area is simpler and accomplishes the same
purpose.

An alternative to this is a Cleveland dot chart. I prefer the way it's
done in base graphics, where spacing is used to separate groups rather
than faceting. A basic version would be

with(dataset, dotchart(var, labels = station, groups = area, pch = 16))

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ellen Pape

unread,
Jan 31, 2015, 6:54:59 AM1/31/15
to Dennis Murphy, ggplot2
Hi Dennis,

Yes, thanks for the solution! Also the dotplot seems nice!

Have a nice weekend
ellen
Reply all
Reply to author
Forward
0 new messages