change axes manually in facet_wrap?

3,121 views
Skip to first unread message

Anna Zakrisson Braeunlich

unread,
Jul 23, 2014, 4:01:33 AM7/23/14
to ggplot2
Hi ggplot2 group,

I have a question regarding manually controlling the axes in facet_wrap, not using scales="free"/"fixed".

# I KNOW THAT THIS PLOT IS UGLY, I AM RUBBISH AT MAKING DUMMY DATA THAT MIMICS MY REAL DATA.
### Question: I would like to change the y-axis ONLY IN FACETS 3-4 manually, but keep the
### axis for facets 1&2 (run the code and see plot).
### My problem is that in my real data facet 1&2 have similar data range (going up to values ~400)
### and 3&4 similar (ranging from ~1-16).
### When I set the scales="free" it all looks quite good, except for that the axes are 
### slightly nudged in facet 3 compared with facet 4. I would therefore, for clarity, like to
### set these axes as identical, but still keep the axes for facets 1&2.
### How can I tweak facet_wrap so as to manually set the y-axes?


### Some dummy data:

mydata<- data.frame(factor1 = factor(rep(LETTERS[1:6], each = 80)), 
                  factor2 = factor(rep(c(1:5), each = 16)), 
                  factor3 = factor(rep(c(1:4), each = 4)), 
                  var1 = rnorm(120, mean = rep(c(0, 3, 5), each = 40), 
                  sd = rep(c(1, 2, 3), each = 20)), 
                  var2 = rnorm(120, mean = rep(c(6, 7, 8), each = 40), 
                  sd = rep(c(1, 2, 3), each = 20))) 

p <- ggplot(mydata,aes(x=factor1,y=var1))+
  geom_point(mapping=aes(shape=factor2))+
  facet_wrap(~factor3, scales = "free")

Many thank's!
best
Anna Zakrisson



Anna Zakrisson Braeunlich
PhD student

Department of Ecology, Environment and Plant Sciences
Stockholm University
Svante Arrheniusv. 21A
SE-106 91 Stockholm
Sweden/Sverige

Lives in Berlin.
For paper mail:
Katzbachstr. 21
D-10965, Berlin
Germany/Deutschland

E-mail: anna.za...@su.se
Tel work: +49-(0)3091541281
Mobile: +49-(0)15777374888
LinkedIn: http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b

><((((º>`•. . • `•. .• `•. . ><((((º>`•. . • `•. .• `•. .><((((º>`•. . • `•. .• `•. .><((((º>

Ben Bond-Lamberty

unread,
Jul 23, 2014, 9:57:47 AM7/23/14
to Anna Zakrisson Braeunlich, ggplot2
Hi Anna, see this stackoverflow answer, which has a clever solution
involving geom_blank():
http://stackoverflow.com/questions/18046051/setting-individual-axis-limits-with-facet-wrap-and-scales-free-in-ggplot2

In your example, if p <- ggplot( ... ) you could do

p+geom_blank(data=data.frame(factor3=3:4,factor1="A",var1=15))

to just change the y axis of the bottom two facets.

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages