[R] ggplot2/aesthetic plotting advice

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Apr 23 09:56:44 CEST 2009


Dear Ben,

With a lot of overlapping errorbar things will always look cluttered.
Below you will find a few suggestions.

HTH,

Thierry


library(ggplot2)

nx <- 3
ngrp <- 5
nper <- 4
x <- rep(1:nx,ngrp*nper)
y <- runif(nx*ngrp*nper)
g <- factor(rep(1:ngrp,each=nx*nper))

dat <- data.frame(Year = factor(x), Whatever = y, Group = g)
ds <- ddply(dat, c("Year", "Group"), function(x){c(SD = sd(x$Whatever),
Mean = mean(x$Whatever))})
ds$Max <- ds$Mean + ds$SD
ds$Min <- ds$Mean - ds$SD

ggplot(ds, aes(x = Year, y = Mean, min = Min, max = Max, colour =
Group)) + geom_pointrange(position = position_dodge(width = .4))
ggplot(ds, aes(x = as.numeric(Year), y = Mean, min = Min, max = Max,
colour = Group)) + geom_line() + geom_point() + geom_errorbar()
ggplot(ds, aes(x = as.numeric(Year), y = Mean, min = Min, max = Max,
colour = Group)) + geom_line() + geom_point()
ggplot(ds, aes(x = as.numeric(Year), y = Mean, min = Min, max = Max)) +
geom_line() + geom_point() + geom_errorbar() + facet_wrap(~Group)
ggplot(ds, aes(x = as.numeric(Year), y = Mean, min = Min, max = Max,
colour = Group, fill = Group)) + geom_ribbon(alpha = .2, aes(colour =
NULL)) + geom_line(size = 3)
ggplot(ds, aes(x = as.numeric(Year), y = Mean, min = Min, max = Max,
colour = Group, fill = Group)) + geom_ribbon(alpha = .2, aes(colour =
NULL)) + geom_line(size = 3) + facet_wrap(~Group)

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

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 op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Ben Bolker
Verzonden: donderdag 23 april 2009 6:20
Aan: r-help op r-project.org
Onderwerp: [R] ggplot2/aesthetic plotting advice


  Consider the following situation:

  we have quantified algal concentrations for
a variety of species using many samples at each
of three years.  It seems to make sense to generate
a line plot (matplot-like), with each species plotted
as a separate line, with the points connected to emphasize
the temporal pattern.

  The problem: lots of overlapping error bars.

  The question: from both a procedural and an aesthetic
point of view, what do people recommend?  Below is an
example that hacks (not quite "jitters", because it's
systematic) the x locations a bit, but I find it a bit
ugly.  (I was also hoping that ggplot had some magic
to do this automatically, but position_dodge doesn't
seem to work in this context ...)

  Ben Bolker

library(ggplot2)

nx <- 3
ngrp <- 5
nper <- 4
x <- rep(1:nx,ngrp*nper)
y <- runif(nx*ngrp*nper)
g <- factor(rep(1:ngrp,each=nx*nper))

dat <- data.frame(x,y,g)

se <- with(dat,tapply(y,list(g,x),sd)/table(g,x))
means <- with(dat,tapply(y,list(g,x),mean))
limits <- aes(ymax=means+se,ymin=means-se)
gg <- factor(rep(1:ngrp,nx))
xx <- rep(1:nx,each=ngrp)
p <- ggplot(dat,aes(y=c(means),x=xx,
                    group=gg,colour=gg))
p + geom_pointrange(aes(ymin=c(means-se),ymax=c(means+se)))
xdodge <- xx + rep(seq(-0.1,0.1,length=ngrp),nx)
p + geom_line() + geom_pointrange(aes(x=xdodge,
                                      ymin=c(means-se),
                                      ymax=c(means+se))) +
  scale_x_continuous(name="Year",breaks=1:3) +
  scale_y_continuous(name="Whatever")


-- 
Ben Bolker
Associate professor, Biology Dep't, Univ. of Florida
bolker op ufl.edu / www.zoology.ufl.edu/bolker
GPG key: www.zoology.ufl.edu/bolker/benbolker-publickey.asc

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

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.



More information about the R-help mailing list