Skip to content

plotting multiple variables in 1 bar graph

10 messages · Macy Anonuevo, David L Carlson, PIKAL Petr +3 more

#
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.




--
View this message in context: http://r.789695.n4.nabble.com/plotting-multiple-variables-in-1-bar-graph-tp4647099.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi
Where is aggregate.plot from?
Something like

barplot(VADeaths, beside = TRUE,
        col = c("lightblue", "mistyrose", "lightcyan",
                "lavender", "cornsilk"),
        legend = rownames(VADeaths), ylim = c(0, 100))
title(main = "Death Rates in Virginia", font.main = 4)


or maybe you could try ggplot2

Regards
Petr
#
If you have your data organized in a matrix like the one printed at the
bottom of your jpg, barplot will produce the same graph:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]   91   52   46   94   90   51   74   83   38
[2,]   94   74   72   98   14   39   81    1   44
[3,]   29   13   93   12   99   91   39   21    4
[4,]   83   66   26   47   95   45   69   91   97
[5,]   64   71   46   56    8   84    0   61   43
-------------------------------------------------
David L. Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Macy Anonuevo
Sent: Tuesday, October 23, 2012 8:32 AM
To: r-help at r-project.org
Subject: Re: [R] plotting multiple variables in 1 bar graph

Thank you for replying.

On Tue, Oct 23, 2012 at 9:11 PM, PIKAL Petr [via R] <
ml-node+s789695n4647131h99 at n4.nabble.com> wrote:

            
email]<http://user/SendEmail.jtp?type=node&node=4647131&i=0>[mailto:
email]<http://user/SendEmail.jtp?type=node&node=4647131&i=1>
plot
each
rubble
10
(coral,
FUN=c("mean"),
change
"VADeaths" is plotted over time? I'm hoping to have multiple variables
plotted over time, with the bars clustered by variable so that you can
see
how each variable changes over time.
don't
even know where to start.
<http://r.789695.n4.nabble.com/file/n4647099/abdeens_benthic_cover.jpg>
the
or
http://r.789695.n4.nabble.com/plotting-
<http://user/SendEmail.jtp?type=node&node=4647131&i=2>mailing list
<http://user/SendEmail.jtp?type=node&node=4647131&i=3>mailing list
discussion
http://r.789695.n4.nabble.com/plotting-multiple-variables-in-1-bar-graph
-tp4647099p4647131.html
here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubs
cribe_by_code&node=4647099&code=cmFkaWFudC5zdGFyc0BnbWFpbC5jb218NDY0NzA5
OXwtNDIxODQ0NzMx>
NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_
viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces
.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web
.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.
naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3A
email.naml>

  
    
#
Hi

see in line
<snip>
Not exactly. VADeaths is a matrix not a single variable.
num [1:5, 1:4] 11.7 18.1 26.9 41 66 8.7 11.7 20.3 30.9 54.3 ...
 - attr(*, "dimnames")=List of 2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ..$ : chr [1:5] "50-54" "55-59" "60-64" "65-69" ...
  ..$ : chr [1:4] "Rural Male" "Rural Female" "Urban Male" "Urban Female"
See ?barplot help page for details.

Regards
Petr
#
On 10/23/2012 07:22 PM, Macy Anonuevo wrote:
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
#
Perhaps something like lattice or ggplot would serve better?

Here's something using ggplot (which has prettier colours than lattice)


#Something like your data - with considerable licence on unreadable names!
abst <-structure(list(Count = c(17.03, 22.94, 28.38, 29.72, 28.37, 14.45, 
1.51, 0.54, 0.62, 1.52, 62.3, 70.6, 68.82, 64.75, 63.77, 3.17, 
2.78, 2.22, 2.03, 1.94, 0.61, 0.33, 0.74, 0.74, 0.58, 0.44, 0.12, 
0.37, 0.08, 0.41, 0.04, 0, 0.08, 0, 0.08, 1.96, 1.68, 2.84, 2.06, 
3.32, 0, 0, 0, 0, 0), Benthic = structure(c(2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 9L, 9L, 9L, 9L, 9L, 4L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 8L, 8L, 
8L, 8L, 8L, 7L, 7L, 7L, 7L, 7L), .Label = c("Algae", "Coral", 
"Deadcoral", "Ind", "other", "softcoral", "something", "sponges", 
"xBiotic"), class = "factor"), Time = structure(c(1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("T 1", "T 2", 
"T 3", "T 4", "T 5"), class = "factor")), .Names = c("Count", 
"Benthic", "Time"), row.names = c(NA, -45L), class = "data.frame")

head(abst)

#Add an arbitrary 'std error'

abst$stderr <- 0.05*abst$Count

 library(ggplot2)
 b <- ggplot(subset(abst, Benthic!="something"), aes(x = Time, y = Count, fill=Time)) 
 bptot<-b + geom_bar(stat = "identity", position = "dodge")
 bptot + facet_grid(. ~ Benthic , scales="free_y", margins=T)
 
#or, for unequal scale heights - much easier to see individual trends
bwrap <- bptot + facet_wrap( ~ Benthic , scales="free", nrow=2)
bwrap

#Now add error bars
bwrap+geom_errorbar(aes(ymin=Count-stderr, ymax=Count+stderr), width=.3)

______________________________________________
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.

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
1 day later
#
Macy
That seems to be the easiest thing to do.

You said you already used aggregate() to get the means; you can extend that a little to get the sd's or (assuming a simple stderr) the standard errors in the same data frame; for example

aggregate(count~site+time+benthic.component, data=yourdataframe, FUN=function(x) c(mean=mean(x), sd=sd(x), stderr=sqrt(var(x)/length(x))))
	#assuming i) that you have a column-format data frame with one row for each individual count identified by site, time and component
                #ii) that's how you want the stderr calculated!

Then you'll have the means, standard deviations and standard errors in the right form for ggplot to work with stat="identity".

Do check the variable names in the resulting aggregated data frame; aggregate would give them names like count.mean, count.sd etc so you will need to use those names in ggplot.

Steve E
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}