[R] ggplot2 - facetting

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Tue Mar 25 16:10:30 CET 2008


Hi Pedro,

I think that the code below gives you the plot you want. Notice that you
don't have to add everthing seperatly. But you can if you feel the need
to do so.

ggplot(plotdata2, aes(x = x, y = y)) + geom_point() + facet_grid(group ~
.) 

plot0 <- ggplot(plotdata2, aes(x = x, y = y))
layer1 <- geom_point()
facets <- facet_grid(group ~ .)
plot0 + layer1 + facets

But this doesn't work because the variable 'group' is not defined in
plot0.

plot0 <- ggplot()
layer1 <- geom_point(data = plotdata2, aes(x = x, y = y))
facets <- facet_grid(group ~ .)
plot0 + layer1 + facets

With that information, you can alter your code. Notice that I've moved
data=plotdata2 from layer() to ggplot().

plot0<-ggplot(data=plotdata2)
layer1<-layer(mapping=aes_string(x='x',y='y'),geom='point',
stat='identity')
scaleY<-scale_y_continuous()
scaleX <- scale_x_continuous()
Facets<-facet_grid(group ~ .)
plot0+layer1 +scaleY + scaleX + facet_grid(group~.)

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 

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 Pedro de Barros
Verzonden: dinsdag 25 maart 2008 15:50
Aan: r-help op stat.math.ethz.ch
Onderwerp: [R] ggplot2 - facetting
Urgentie: Hoog

Dear All,

After having overcome the issue of legends (thanks, Thierry, once 
more), I am trying to use facetting, but here also I can not find how 
to do this. I do not want to use qplot, but rather the more flexible 
options. However, it seems I am doing still something pretty stupid, 
because I always get an error, even if it seems I am doing everything 
like the examples.

My code is below.
set.seed(123)
plotdata2<-data.frame(x=rep((1:4),4), y=rep(0.1*(1:4),4), 
group=sample(factor(rep(1:4,rep(4,4)), labels=c('Class1', 'Class2', 
'Class3', 'Class4'))))
plotdata2 <- plotdata2[order(plotdata2[,'group']),]
plot0<-ggplot()
layer1<-layer(data=plotdata2, 
mapping=aes_string(x='x',y='y'),geom='point', stat='identity')
scaleY<-scale_y_continuous()
scaleX <- scale_x_continuous()
Facets<-facet_grid(group ~ .)
plot1<-plot0+layer1 +scaleY + scaleX + facet_grid(group~.)

plot1

I always get the error message:
Error in check_formula(formula, varnames) :
   Formula contains variables not in list of known variables

Thanks for any help you can provide.

Best,
Pedro

______________________________________________
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