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.
plotting multiple variables in 1 bar graph
10 messages · Macy Anonuevo, David L Carlson, PIKAL Petr +3 more
Hi
-----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 10:23 AM
To: r-help at r-project.org
Subject: [R] plotting multiple variables in 1 bar graph
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))
Where is aggregate.plot from?
What I need now is to plot all the variables in 1 site as they change over time.
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
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.
______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121023/b5bf9bd9/attachment.pl>
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:
set.seed(42) a <- matrix(round(runif(45)*100, 0), nrow=5, ncol=9) a
[,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
barplot(a) barplot(a, beside=TRUE)
------------------------------------------------- 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:
Hi
-----Original Message----- From: [hidden
email]<http://user/SendEmail.jtp?type=node&node=4647131&i=0>[mailto:
r-help-bounces at r-
project.org] On Behalf Of Macy Anonuevo Sent: Tuesday, October 23, 2012 10:23 AM To: [hidden
email]<http://user/SendEmail.jtp?type=node&node=4647131&i=1>
Subject: [R] plotting multiple variables in 1 bar graph 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))
Where is aggregate.plot from?
from the epicalc package.
What I need now is to plot all the variables in 1 site as they
change
over time.
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)
If I understand the code correctly, this means that only the variable
"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.
or maybe you could try ggplot2
I tried looking through ggplot2 and I don't understand the code. I
don't even know where to start.
Regards Petr
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.
______________________________________________ [hidden email]
<http://user/SendEmail.jtp?type=node&node=4647131&i=2>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.
______________________________________________ [hidden email]
<http://user/SendEmail.jtp?type=node&node=4647131&i=3>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. ------------------------------ If you reply to this email, your message will be added to the
discussion
below:
http://r.789695.n4.nabble.com/plotting-multiple-variables-in-1-bar-graph -tp4647099p4647131.html
To unsubscribe from plotting multiple variables in 1 bar graph, click
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>
Geekerie Shirts: shirts for the pop culture junkie - http://geekerie.multiply.com Sustainability in action: the official website of El Nido Resorts' Environment Department - http://elnidoenvironment.wordpress.com -- View this message in context: http://r.789695.n4.nabble.com/plotting-multiple-variables-in-1-bar-graph -tp4647099p4647134.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] ______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121023/87fde88c/attachment.pl>
Hi see in line
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of David Carlson Sent: Tuesday, October 23, 2012 6:47 PM To: 'Macy Anonuevo'; r-help at r-project.org Subject: Re: [R] plotting multiple variables in 1 bar graph
<snip>
What I need now is to plot all the variables in 1 site as they
change
over time.
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)
If I understand the code correctly, this means that only the
variable "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.
Not exactly. VADeaths is a matrix not a single variable.
str(VADeaths)
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
or maybe you could try ggplot2
I tried looking through ggplot2 and I don't understand the code. I
don't even know where to start.
Regards Petr
What Excel produced:
(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:
multiple-variables-in-1-bar-graph-tp4647099.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ [hidden email]
<http://user/SendEmail.jtp?type=node&node=4647131&i=2>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.
______________________________________________ [hidden email]
<http://user/SendEmail.jtp?type=node&node=4647131&i=3>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. ------------------------------ If you reply to this email, your message will be added to the
discussion
below:
http://r.789695.n4.nabble.com/plotting-multiple-variables-in-1-bar- graph -tp4647099p4647131.html
To unsubscribe from plotting multiple variables in 1 bar graph,
click
here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsub s cribe_by_code&node=4647099&code=cmFkaWFudC5zdGFyc0BnbWFpbC5jb218NDY0NzA 5 OXwtNDIxODQ0NzMx>
.
NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro _ viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespace s .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>
-- Geekerie Shirts: shirts for the pop culture junkie - http://geekerie.multiply.com Sustainability in action: the official website of El Nido Resorts' Environment Department - http://elnidoenvironment.wordpress.com -- View this message in context: http://r.789695.n4.nabble.com/plotting-multiple-variables-in-1-bar- graph -tp4647099p4647134.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
______________________________________________ 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. ______________________________________________ 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.
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
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 ... 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.
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121026/ffec5fb0/attachment.pl>
Macy
The data in the sample data frame are already the means of the 5 samples measured for each time T. Does this mean that I need to 1) calculate the means and standard deviations separately per variable per time, 2) compile those results in a new data frame, then 3) use the ggplot code you provided?
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
On Wed, Oct 24, 2012 at 11:22 PM, S Ellison <S.Ellison at lgcgroup.com> 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
> ...
> > 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. >
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, copying or disclosure other than by the intended recipient is unauthorised. If you have received this message in error, please notify the sender immediately via +44(0)20 8943 7000 <tel:%2B44%280%2920%208943%207000> or notify postmaster at lgcgroup.com and delete this message and any copies from your computer and network. LGC Limited. Registered in England 2991879. Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}