[R] ggplot2 facets as rows and columns

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Nov 8 15:12:37 CET 2007


You could work around it like this.

n <- length(levels(TDBU$system))
rows <- ceiling(sqrt(n))
TDBU$rows <- ceiling(as.numeric(TDBU$system) / rows)
TDBU$cols <- (as.numeric(TDBU$system) - 1)  %% rows

ggplot(TDBU,aes(x=x))+geom_histogram(aes(y=..density..))+ geom_density()
+ facet_grid(rows ~ cols)

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 Ben Bolker
Verzonden: donderdag 8 november 2007 14:53
Aan: r-help op r-project.org
Onderwerp: [R] ggplot2 facets as rows and columns


   Does anyone (Hadley??) know if there's a straightforward way in
ggplot2 to get data divided by a single factor to
plot as a rectangular grid of subplots?   So far I've only
been able to get such data plotted as a single row or single column of
skinny subplots.  The code below gives an example implemented with
lattice, and my best attempt in ggplot2

 cheers
   Ben Bolker

------------
g= rep(1:9,each=50)
means = rnorm(9)
vars = runif(9)
x = rnorm(450,means[g],vars[g])
TDBU = data.frame(system=factor(g),x)

require(lattice)

histogram( ~ x | system, data = TDBU,
          xlab = "LRR Effect Size",type="density",
          panel = function(x, ...) {
            panel.histogram(x, ...)
            panel.densityplot(x,...)
          } ) 

library(ggplot2)
ggplot(TDBU,aes(x=x))+geom_histogram(aes(y=..density..))+
  geom_density()+facet_grid(system~.)


--
View this message in context:
http://www.nabble.com/ggplot2-facets-as-rows-and-columns-tf4771000.html#
a13647294
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