Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: No free axis scales when using geom_dotplot with facet_grid

Tags:

r

ggplot2

I've been attempting to generate a series of dot plots together using facet_grid. In so doing, I've noticed that geom_dotplot does not appear to respond to facet_grid's scales = "free_y" argument.

Here is some example code:

require(ggplot2)

#Example data
set.seed(3)
df = data.frame(Gene = rep(c("a", "b", "c", "d"), each=20),
                ToD = rep(c("Morning", "Evening"), times = 40),
                Expression = c(runif(20, min=0, max=10),
                               runif(20, min=0, max=1),
                               runif(20, min=0, max=1000),
                               runif(20, min=0, max=100)))

#Box plots of example data
ggplot(df, aes(x = ToD, y = Expression)) +
    geom_boxplot() +
    facet_grid(Gene ~ ., scales = "free_y")

#Dot plots of example data
ggplot(df, aes(x = ToD, y = Expression)) +
    geom_dotplot(binaxis = "y", stackdir = "centerwhole") +
    facet_grid(Gene ~ ., scales = "free_y")

And here are the versions of R and ggplot2 I'm currently using:

  • R version 3.2.2 (2015-08-14)
  • ggplot2_1.0.1.9003

So when I generate the box plots, everything works as expected with the y-axes scaling appropriately for each facet row:

Box plots of test data

However, the dot plots maintain the same y-axis scales for every facet row:

Dot plots of test data

I've read about a known bug in ggplot2 where coord_flip and facet_grid do not work together when specifying free scales. Is this related to that same problem?

While I could generate each of the plots individually and then combine them with grid.arrange, this is cumbersome for my purposes. I'm trying to line up these dot plots with other faceted plots, and I'd like to avoid needing to regenerate all of those using grid.arrange too. Any thoughts?

Thanks for any help you can offer and please let me know if I can provide any further clarification.

like image 862
Brain_Food Avatar asked Jan 06 '16 16:01

Brain_Food


1 Answers

I have been reading the doc at ?geom_dotplot. Apparently, the binpositions-option can be set to "all" (all data taken together) or "bygroup"; the default. Thus, using Gene both as group and as facets, binpositions can vary and at least the free y-axis returned:

ggplot(df, aes(x = ToD, y = Expression, group=Gene)) +
  geom_dotplot(binaxis = "y", stackdir = "centerwhole", binpositions="bygroup") +
  facet_grid(Gene ~ ., scales = "free_y")

enter image description here

But now the grouping on the x-axis disappeared. There's probably a better solution, but grouping by the interaction between Gene and ToD seemed to solve it:

 ggplot(df, aes(x = ToD, y = Expression, group=interaction(Gene,ToD))) +
  geom_dotplot(binaxis = "y", stackdir = "centerwhole", binpositions="bygroup") +
  facet_grid(Gene ~ ., scales = "free_y")

enter image description here

like image 172
Heroka Avatar answered Sep 18 '22 13:09

Heroka