[R] Grouped bars in barplot

Peter Alspach Peter.Alspach at plantandfood.co.nz
Thu Jan 13 04:11:20 CET 2011


Tena koe Steve

Convert your data into a matrix:

dataMat <- matrix(c(bline_precip[10,9], bline_runoff[10,9],
                    cccma_precip[10,9], cccma_runoff[10,9],
                    csiro_precip[10,9], csiro_runoff[10,9],
                    ipsl_precip[10,9], ipsl_runoff[10,9],
                    mpi_precip[10,9], mpi_runoff[10,9],
                    ncar_precip[10,9], ncar_runoff[10,9],
                    ukmo_precip[10,9], ukmo_runoff[10,9]), nrow=2)

Since I don't know the nature of your data I will use a simple example:

dataMat <- matrix(1:14, nrow=2)
barplot(dataMat, beside=TRUE, space=c(0,2))
tTicks <- barplot(dataMat, beside=TRUE, space=c(0,2))
tTicks <- tapply(tTicks, rep(1:7, each=2), mean)
axis(1, tTicks, letters[1:7])

Is that what you want?

HTH ....

Peter Alspach

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Steve Murray
> Sent: Thursday, 13 January 2011 12:14 p.m.
> To: r-help at r-project.org
> Subject: [R] Grouped bars in barplot
> 
> 
> Dear all,
> 
> I am trying to make a barplot with clustered pairs of bars, using
> class=numeric data and the following command:
> 
> barplot(c(bline_precip[10,9], bline_runoff[10,9], cccma_precip[10,9],
> cccma_runoff[10,9], csiro_precip[10,9], csiro_runoff[10,9],
> ipsl_precip[10,9], ipsl_runoff[10,9], mpi_precip[10,9],
> mpi_runoff[10,9], ncar_precip[10,9], ncar_runoff[10,9],
> ukmo_precip[10,9], ukmo_runoff[10,9]), beside=TRUE, space=c(0,2))
> 
> This results in all bars being packed tightly together, but with no gap
> between each pair. I suspect the problem is something to do with the
> data not being a matrix, but I've tried using as.matrix for each data
> element and this doesn't seem to work. If any one has any suggestions
> I'd be very grateful to hear them.
> 
> Also, I'm hoping to put a label beneath each pair of bars on the x-
> axis, in the centre. At present I can only get labels to appear
> directly underneath a single bar, as opposed to the centre of the pair
> of bars. Does anyone have any suggestions for solving this?
> 
> Many thanks for any help offered.
> 
> Steve
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

The contents of this e-mail are confidential and may be subject to legal privilege.
 If you are not the intended recipient you must not use, disseminate, distribute or
 reproduce all or any part of this e-mail or attachments.  If you have received this
 e-mail in error, please notify the sender and delete all material pertaining to this
 e-mail.  Any opinion or views expressed in this e-mail are those of the individual
 sender and may not represent those of The New Zealand Institute for Plant and
 Food Research Limited.



More information about the R-help mailing list