Hi R Users,
I was struggling to overlay two graphs created from the two different dataset
using ggplot2. Furthermore, I could not join means of the box plots.
I tried this way but did not work. Any suggestions?
dat1<-structure(list(site = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), layer = structure(c(2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L,
1L, 1L), .Label = c("bottom", "top"), class =
"factor"), Present = c(120L,
125L, 123L, 23L, 21L, 19L, 131L, 124L, 127L, 24L, 27L, 25L, 145L,
143L, 184L, 29L, 14L, 17L, 38L)), .Names = c("site",
"layer",
"Present"), row.names = c(NA, 19L), class = "data.frame")
dat1
dat2<-structure(list(site = 1:3, present = c(-3L, 2L, 5L)), .Names =
c("site",
"present"), row.names = c(NA, 3L), class = "data.frame")
dat2
library("plyr")
library(ggplot2)
A<-ggplot(dat1, aes(x = factor(site), y = Present, colour = layer,
fill=layer)) +
geom_boxplot(outlier.shape = 16, outlier.size = 1) + theme_bw()+
ylim(0,185)
# Here I wanted to join the means of the boxplots among the sites, but I could
not join it.
B<-ggplot(dat2, aes(x=factor(site),y= present, colour="blue") +
geom_line() + geom_point()) # wanted to plot it using second y axis.
A+B
Thanks for your help.
KG
[[alternative HTML version deleted]]
On Jan 27, 2014, at 3:13 AM, Kristi Glover wrote:> Hi R Users, > I was struggling to overlay two graphs created from the two different dataset using ggplot2. Furthermore, I could not join means of the box plots. > > I tried this way but did not work. Any suggestions? > dat1<-structure(list(site = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, > 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), layer = structure(c(2L, > 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, > 1L, 1L), .Label = c("bottom", "top"), class = "factor"), Present = c(120L, > 125L, 123L, 23L, 21L, 19L, 131L, 124L, 127L, 24L, 27L, 25L, 145L, > 143L, 184L, 29L, 14L, 17L, 38L)), .Names = c("site", "layer", > "Present"), row.names = c(NA, 19L), class = "data.frame") > dat1 > dat2<-structure(list(site = 1:3, present = c(-3L, 2L, 5L)), .Names = c("site", > "present"), row.names = c(NA, 3L), class = "data.frame") > dat2 > library("plyr") > library(ggplot2) > A<-ggplot(dat1, aes(x = factor(site), y = Present, colour = layer, fill=layer)) + > geom_boxplot(outlier.shape = 16, outlier.size = 1) + theme_bw()+ ylim(0,185) > # Here I wanted to join the means of the boxplots among the sites, but I could not join it. > B<-ggplot(dat2, aes(x=factor(site),y= present, colour="blue") + > geom_line() + geom_point()) # wanted to plot it using second y axis. > A+B > Thanks for your help.I believe you may be able to find 'ggplot2' hacks that deliver double ordinate plots but Hadley Wickham has an intense aversion to such plots, so you won't find them in standard `gglot2` functions. Do a search on StackOverflow and in the archives. I cannot remember the names of the functions or the authors, but I do remember experiencing the heresy of double ordinates within the vaulted arches the Church of GGplot.> > [[alternative HTML version deleted]]And you should learn to post in plain text. -- David Winsemius Alameda, CA, USA
Hi Kristi
Jim has given you 1 non ggplot2 solution here is one from lattice
panel.average is a line so added a line for points
# convert site to a factor
dat1$Site = factor(dat1$Site)
datav <- aggregate(Present ~ Site, dat1,mean)
datav
diff(datav[,2]) # test
bw1 <-
bwplot(Present~Site,data=subset(dat1, layer == "top"),
ylim = c(0,200),
panel = function(x,y,...){
panel.bwplot(x,y,...)
panel.average(x=x,y=y,...)
})
bw2<-
bwplot(Present~Site,data=subset(dat1, layer == "bottom"),
ylim = c(0,200),
panel = function(x,y,...){
panel.bwplot(x,y,...)
# overall mean if needed
panel.average(dat1[,"Site"],
dat1[,"Present"], ...)
})
print(bw1, more = T)
print(bw2, more = F)
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
Behalf Of Kristi Glover
Sent: Monday, 27 January 2014 21:13
To: R-help
Subject: [R] Overlaying two graphs using ggplot2 in R
Hi R Users,
I was struggling to overlay two graphs created from the two different
dataset using ggplot2. Furthermore, I could not join means of the box plots.
I tried this way but did not work. Any suggestions?
dat1<-structure(list(site = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), layer = structure(c(2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L,
1L, 1L), .Label = c("bottom", "top"), class =
"factor"), Present = c(120L,
125L, 123L, 23L, 21L, 19L, 131L, 124L, 127L, 24L, 27L, 25L, 145L,
143L, 184L, 29L, 14L, 17L, 38L)), .Names = c("site",
"layer",
"Present"), row.names = c(NA, 19L), class = "data.frame")
dat1
dat2<-structure(list(site = 1:3, present = c(-3L, 2L, 5L)), .Names
c("site",
"present"), row.names = c(NA, 3L), class = "data.frame")
dat2
library("plyr")
library(ggplot2)
A<-ggplot(dat1, aes(x = factor(site), y = Present, colour = layer,
fill=layer)) +
geom_boxplot(outlier.shape = 16, outlier.size = 1) + theme_bw()+
ylim(0,185)
# Here I wanted to join the means of the boxplots among the sites, but I
could not join it.
B<-ggplot(dat2, aes(x=factor(site),y= present, colour="blue") +
geom_line() + geom_point()) # wanted to plot it using second y axis.
A+B
Thanks for your help.
KG
[[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.
Dear Kristi,
You could do something like this.
ggplot(dat1, aes(x = factor(site), y = Present)) +
geom_boxplot(aes(colour = layer)) +
geom_line(data = dat2, aes(group = 1, y = present)) +
geom_point(data = dat2, aes(y = present))
Note that
- ggplot provides no second axis
- the boxplots display the median, not the mean
Best regards,
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx at inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more than
asking him to perform a post-mortem examination: he may be able to say what the
experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not ensure
that a reasonable answer can be extracted from a given body of data.
~ John Tukey
-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
Namens Kristi Glover
Verzonden: maandag 27 januari 2014 12:13
Aan: R-help
Onderwerp: [R] Overlaying two graphs using ggplot2 in R
Hi R Users,
I was struggling to overlay two graphs created from the two different dataset
using ggplot2. Furthermore, I could not join means of the box plots.
I tried this way but did not work. Any suggestions?
dat1<-structure(list(site = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L), layer = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("bottom",
"top"), class = "factor"), Present = c(120L, 125L, 123L,
23L, 21L, 19L, 131L, 124L, 127L, 24L, 27L, 25L, 145L, 143L, 184L, 29L, 14L, 17L,
38L)), .Names = c("site", "layer", "Present"),
row.names = c(NA, 19L), class = "data.frame")
dat1
dat2<-structure(list(site = 1:3, present = c(-3L, 2L, 5L)), .Names =
c("site", "present"), row.names = c(NA, 3L), class =
"data.frame")
dat2
library("plyr")
library(ggplot2)
A<-ggplot(dat1, aes(x = factor(site), y = Present, colour = layer,
fill=layer)) +
geom_boxplot(outlier.shape = 16, outlier.size = 1) + theme_bw()+
ylim(0,185) # Here I wanted to join the means of the boxplots among the sites,
but I could not join it.
B<-ggplot(dat2, aes(x=factor(site),y= present, colour="blue") +
geom_line() + geom_point()) # wanted to plot it using second y axis.
A+B
Thanks for your help.
KG
[[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.
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en
binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer
and may not be regarded as stating an official position of INBO, as long as the
message is not confirmed by a duly signed document.