Combine coord_flip() and facet_grid()?

1,103 views
Skip to first unread message

Ben Harrison

unread,
May 22, 2013, 8:55:48 PM5/22/13
to ggp...@googlegroups.com
I am having trouble with coord_flip() again, and I don't understand why.

# example:
mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point()  + coord_flip()

mt + facet_grid(. ~ cyl, scales="free")  # ignores y axis
mt + facet_grid(. ~ cyl, scales="free_y")  # proof for above

# this is not what I want but illustrates problem in a different way
mt + facet_grid(cyl ~ ., scales="free")  # only frees one axis

Ben.

Ben Harrison

unread,
May 22, 2013, 9:18:33 PM5/22/13
to ggp...@googlegroups.com
Decided to post my data and code to hopefully get some suggestions as to how to workaround this problem. The data are at the link below,

http://ubuntuone.com/6oFKXT9dhj6YXpXNIug74q

require(ggplot2)
logs <- read.csv("logs.csv", na.strings="?")
head(logs)

   DEPTH CALI      DEN       GR
1 0.4572  NaN 1464.196      NaN
2 0.6096  NaN 1471.228      NaN
3 0.7620  NaN 1490.986      NaN
4 0.9144  NaN 1556.038 166.4195
5 1.0668  NaN 1594.225 164.2164
6 1.2192  NaN 1574.266 165.7328

# a simple plot to get started, showing correct form.
# The variable label (GR) would be better at the top, but don't know how to change that.
ggplot(data=logs, aes(y=GR, x=DEPTH)) + geom_line() +
    scale_x_reverse()  + coord_flip() + theme(aspect.ratio = 3)

# trying to plot 3 variables versus depth in a facet to look similar to above
require(reshape2)
melted <- melt(logs, id.vars='DEPTH')
sp <- ggplot(melted, aes(x=DEPTH, y=value)) + geom_line() + scale_x_reverse() + coord_flip()
sp + facet_grid(". ~ variable", scales="free")  # need to free up the variable value scales.

# it should look a bit like this one, but rotated
sp <- ggplot(melted, aes(x=DEPTH, y=value)) + geom_line() + scale_x_reverse()
sp + facet_grid(variable ~ ., scales="free_y")  # , variable.name='curve', value.name='measurement')


Cheers,
Ben

Ito, Kaori (Groton)

unread,
May 22, 2013, 10:01:47 PM5/22/13
to Ben Harrison, ggp...@googlegroups.com

I think it is expected behavior with facet_grid. You can use facet_wrap instead if you want to scale=“free” for both x and y.  However, it seems facet_wrap doesn’t work well with coord_flip (I got error message “Error in facet_render.wrap(plot$facet, panel, plot$coordinates, plot_theme(plot),  :   ggplot2 does not currently support free scales with a non-cartesian coord or coord_flip.”), therefore,  I swap x and y in the ggplot call, remove coord_flip, as below:

 

mt <- ggplot(mtcars, aes(y=mpg, x=wt, colour = factor(cyl))) + geom_point()

mt + facet_wrap(~cyl, scales="free") 

 

….then both x- and y- are free.

I hope this helps,…

Kaori

--
--
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/groups/opt_out.
 
 

Ben Harrison

unread,
May 23, 2013, 12:27:16 AM5/23/13
to ggp...@googlegroups.com, Ben Harrison
OK, thanks Kaori. That's got me on the right path.

I am new to ggplot, so don't know all the functions. From my second post (above) I need to draw lines. geom_line connects x observations, so setting DEPTH as y messes up the plot, but I read up on geom_path instead, which connects observations in original order. So my code now is:


require(reshape2)
melted <- melt(logs, id.vars='DEPTH')
sp <- ggplot(melted, aes(x=value, y=DEPTH)) + geom_path() + scale_y_reverse()

sp + facet_grid(". ~ variable", scales="free") 

and this produces what I wanted!

Dennis Murphy

unread,
May 23, 2013, 4:48:07 AM5/23/13
to Ben Harrison, ggplot2
In general, free scales, coord_flip() and faceting do not play well
together. Any two of the three are fine, but not all three in concert.
Kaori's suggestion is about the best one can hope for in this
circumstance.

Dennis
Reply all
Reply to author
Forward
0 new messages