teunbrand / ggnomics

A small project to add ggplot2 extensions
https://teunbrand.github.io/ggnomics/
Other
80 stars 4 forks source link

specifying columns with facet_nested() #27

Closed joshbis closed 4 years ago

joshbis commented 4 years ago

I'm able to get nested rows, but I'm at a loss on how to specify columns for facet_nested. Do you have any examples of what the function expects?

teunbrand commented 4 years ago

Hi there, Just like facet_grid(), you can specify rows or columns by filling in the right hand side (columns) or left hand side (rows) of the formula.

Example of nested rows:

library(ggplot2)
library(ggnomics)

df <- iris
# Adding a fake nesting variable
df$Leaves <- ifelse(df$Species == "setosa", "Short", "Long")

# Nested Rows
ggplot(df, aes(Sepal.Width, Sepal.Length, colour = Species)) +
  geom_point() +
  facet_nested(Leaves + Species ~ .)

image

Example of nested columns:

# Nested columns
ggplot(df, aes(Sepal.Width, Sepal.Length, colour = Species)) +
  geom_point() +
  facet_nested(~ Leaves + Species)

image

Alternatively, you could also use facet_nested(rows = vars(Leaves, Species)) for row-wise nesting or facet_nested(cols = vars(Leaves, Species)) for column-wise nesting.

I suppose I would have to write a vignette sometime to clarify these kinds of things, but so far I haven't yet.

Does this answer your question?

joshbis commented 4 years ago

Thanks. I did try those formulations of facet_nested() a few different ways and it kept doing the faceting row-wise instead of as columns. But I quit R and reloaded the package and it now works. I suspect that maybe I had another library loaded that was somehow interfering, but can't recreate the issue.

teunbrand commented 4 years ago

Good to hear that it all worked out. I'm not sure what packages might have interfered, most don't overwrite ggplot2 functions. I'll be closing this now; if anyone has a similar problem with row/column specification feel free to open a new issue.