inverting y-axis for only one of a faceted group

813 views
Skip to first unread message

ashish

unread,
Nov 16, 2010, 4:44:58 PM11/16/10
to ggplot2

Hello,

One of the example from Section 7.2 of the ggplot2 book is:

data(mpg)
mpg2 <- subset(mpg, cyl != 5 & drv %in% c("4", "f"))
p <- qplot(displ, hwy, data = mpg2) + geom_smooth(method = "lm", se =
F)
p + facet_grid(cyl ~ drv)
p + facet_grid(cyl ~ drv, margins = T)

This produces the figure 7.2a in the book.

If I wanted to invert the middle y-axis only, keeping the other two as
they are, how do I do it?

I need to do this for an astronomy dataset where the optical flux is
expressed as magnitudes whereas the gamma-ray values are as flux. So I
would want to invert the mag scale and not the flux scale so that both
have brighter points towards the top.

Cheers,
ashish

James McCreight

unread,
Nov 17, 2010, 10:16:37 AM11/17/10
to ashish, ggplot2
Hi Ashish-

Unfortunately, facet is not designed to do such things. If I'm wrong, some one please correct me.

(side note: I, too, wish it were! It makes great looking, compact graphs, but it would clearly be a ton of work to give it every desirable functionality. Some argue that it's really designed to do small multiples, ie slices of the same data. In such a case, you'll not be flipping axes on just some plots. I like to use facet more generally, but that's my opinion. :)

What I would do is make two versions, the original and one where all y-axes are flipped, eg:
p + facet_grid(cyl ~ drv) + scale_y_reverse()
then merge them with inkscape or illustrator. 

You could also use viewports as on page 154.

If you really got fancy with factors, you might be able to pull it off but it would be alot of work, more work than either of these approaches. You'd probably loose the desired  smoothing. It also might not be possible, basically you'd have to ensure every y ordinate in the middle panels was not in the above or below panels, then you could define their order via factor levels as reversed... not practical.

HTH,

James






--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2




Giovanni Marco Dall'Olio

unread,
Nov 17, 2010, 10:37:39 AM11/17/10
to James McCreight, ashish, ggplot2
I think that it is not implemented in ggplot2 because it is against
good practices to produce plots with facets on different scales.

I can think of two solutions:
- use a different viewport for each plot (it is explained in the
ggplot2 book... I thought the ggplot2 book was available for free
online)
- well... plot the inverse values of the second dataset (and remove
the scales which won't mean anything then)

Have a look at this discussion:
- http://stackoverflow.com/questions/3099219/how-to-use-ggplot2-make-plot-with-2-y-axes-one-y-axis-on-the-left-and-another-y
- http://gettinggeneticsdone.blogspot.com/2010/03/arrange-multiple-ggplot2-plots-in-same.html

--
Giovanni Dall'Olio, phd student
Department of Biologia Evolutiva at CEXS-UPF (Barcelona, Spain)

My blog on bioinformatics: http://bioinfoblog.it

ashish

unread,
Nov 19, 2010, 1:56:12 AM11/19/10
to ggplot2
Thank you both!
I think using viewports will be the simplest at this point.

Cheers,
ashish

On Nov 17, 7:37 am, "Giovanni Marco Dall'Olio" <dalloli...@gmail.com>
wrote:
> I think that it is not implemented in ggplot2 because it is against
> good practices to produce plots with facets on different scales.
>
> I can think of two solutions:
> - use a different viewport for each plot (it is explained in the
> ggplot2 book... I thought the ggplot2 book was available for free
> online)
> - well... plot the inverse values of the second dataset (and remove
> the scales which won't mean anything then)
>
> Have a look at this discussion:
> -http://stackoverflow.com/questions/3099219/how-to-use-ggplot2-make-pl...
> -http://gettinggeneticsdone.blogspot.com/2010/03/arrange-multiple-ggpl...
>
>
>
> On Wed, Nov 17, 2010 at 4:16 PM, James McCreight <mccre...@colorado.edu> wrote:
> > Hi Ashish-
> > Unfortunately, facet is not designed to do such things. If I'm wrong, some
> > one please correct me.
> > (side note: I, too, wish it were! It makes great looking, compact graphs,
> > but it would clearly be a ton of work to give it every desirable
> > functionality. Some argue that it's really designed to do small multiples,
> > ie slices of the same data. In such a case, you'll not be flipping axes on
> > just some plots. I like to use facet more generally, but that's my opinion.
> > :)
> > What I would do is make two versions, the original and one where all y-axes
> > are flipped, eg:
> > p + facet_grid(cyl ~ drv) + scale_y_reverse()
> > then merge them with inkscape or illustrator.
> > You could also use viewports as on page 154.
> > If you really got fancy with factors, you might be able to pull it off but
> > it would be alot of work, more work than either of these approaches. You'd
> > probably loose the desired  smoothing. It also might not be possible,
> > basically you'd have to ensure every y ordinate in the middle panels was not
> > in the above or below panels, then you could define their order via factor
> > levels as reversed... not practical.
> > HTH,
> > James
>
Reply all
Reply to author
Forward
0 new messages