[R] ggplot2 boxplot confusion

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Wed Feb 27 10:54:08 CET 2008


Chris,

1.

This code will give you the boxplot that you want. 

library(ggplot2)
series <- c('C2','C4','C8','C10','C15','C20')
ids <- c('ID1','ID2','ID3')
mydata <-
data.frame(SERIES=rep(series,30),ID=rep(ids,60),VALUE=rnorm(180))
ggplot(mydata, aes(y = VALUE, x = factor(1))) + geom_boxplot() +
scale_x_discrete("")

But the real power of ggplot2 is when you want a boxplot for each
category:

ggplot(mydata, aes(y = VALUE, x = series)) + geom_boxplot()


2.
Overlaying boxplots and density plots seems a bad idea to me as both
plots are likey to have a different scale.

HTH,

Thierry


------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium 
tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be 
www.inbo.be 

Do not put your faith in what statistics say until you have carefully
considered what they do not say.  ~William W. Watt
A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Chris Friedl
Verzonden: woensdag 27 februari 2008 5:58
Aan: r-help op r-project.org
Onderwerp: [R] ggplot2 boxplot confusion


Ultimately my aim is to get a plot of density faceted by 2 factors with
a
horizontal boxplot overlaid on each density plot in the grid to indicate
summary stats. So I've been experimenting with creating boxplots and
density
plots. Here's some representative data.

series = c('C2','C4','C8','C10','C15','C20')
ids = c('ID1','ID2','ID3')
mydata <-
data.frame(SERIES=rep(cases,30),ID=rep(ids,60),VALUE=rnorm(180))

1. Using R default graphics I can create a boxplot of data independent
of
factors as follows:
boxplot(mydata$VALUE)

But I can't see how to do this with ggplot2. All the examples in the
help
show x and y aesthetics. How to boxplot a single vector? (I saw a
reference
to a "group" parameter in R-help somewhere but can't find it in the
ggplot2
help pages. Is this a case of group = identity ?)

2. I've read the density plot help and noticed the reference to
..density..
as a means to pass density data instead of original data. But I can't
seem
to get a boxplot to overlay a density plot. This is what I've got so far
with consequent error message:

m <- ggplot(mydata, aes(x=VALUE))
m + geom_density()+ geom_boxplot(aes(x=..density..))

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 0, 180

I've tried y=..density.., both x= and y =  ..density.. and neither and
all
fail somehow. Problem is I don't really understand what I'm doing at
this
point.

So can anyone help me out with this? thanks







-- 
View this message in context:
http://www.nabble.com/ggplot2-boxplot-confusion-tp15706116p15706116.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help op 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.



More information about the R-help mailing list