[R] plotting multiple variables in 1 bar graph

Jim Lemon jim at bitwrit.com.au
Wed Oct 24 11:26:37 CEST 2012


On 10/23/2012 07:22 PM, Macy Anonuevo wrote:
> I'd greatly appreciate your help in making a bar graph with multiple
> variables plotted on it. All the help sites I've seen so far only plot 1
> variable on the y-axis
>
> Data set:
> I have 6 sites, each measured 5 times over the past year. During each
> sampling time, I counted the occurrences of different benthic components
> (coral, dead coral, sand, etc.) over 5 transects in each site
>
> site     time     transect     coral     deadcoral     sand     rubble .....
> S1       time1   trans1        10           15                  10         4
> S1       time1   trans2         5             4                    10
> 6
> S1      time1   trans3         10           2                     5
> 7
> .
> .
> .
> S5      time5    trans5     6            3                       1
> 6
>
> I used aggregate to get the means of the individual variables (coral, dead
> coral, etc.) using the site and time as grouping factors.
>
> aggregate.plot(deadcoral, by=list(SITE=site, TIME=time), FUN=c("mean"),
> error=c("sd"), legend.site="topright", bar.col=rainbow(6))
>
> What I need now is to plot all the variables in 1 site as they change over
> time.
>
> What Excel produced:
> <http://r.789695.n4.nabble.com/file/n4647099/abdeens_benthic_cover.jpg>
> (The image has mean %cover as the y-value instead of mean count but the
> example still applies)
>
> I've spent several hours looking for code to do this but didn't find
> anything. I'd use the Excel graph except that it doesn't have the sd or se
> bars.
>
>
Hi Macy,
This isn't exactly the same as the Excel plot, but it might help you 
out. If your data is in a data frame named "abs":

library(plotrix)
barpos<-barp(abs,names.arg=names(abs),
  do.first=grid(nx=NA,lty=1,ny=9),col=rainbow(5),
  ylim=c(0,100),staxx=TRUE,ylab="Percent",
  main="Abdeen Benthic Cover Through Time",
  height.at=seq(0,100,by=10))
dispersion(barpos$x,barpos$y,ulim=barpos$y/10)
legend(5,80,paste("T",1:5,sep=""),fill=rainbow(5),
  bg="white")

Jim




More information about the R-help mailing list