-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add facet variants called subplots #2786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Facets are meant to display different subsets of the same data. However, with data in the "long" format (i.e. several variables stacked in the same column and an additional column indicating which is which), they can also be used to create the same plot for several variables (e.g. a histogram). In that case, the scales are often free (the variables are different and hence often do not have the same unit). The problem with that approach is that the axis title is often a generic, uninformative word (e.g. "values") while the real labels for the axis are the facet labels, which are on the wrong side of the plot. These functions use `facet_wrap` and `facet_grid` to create subplots and adjust the position and aspect of the facet labels to make them look like axis titles.
Please see the examples in the documentation for use cases, but here is a quick demo: cars <- stack(mtcars, select=c(disp, drat, qsec))
# NB: did not want to depend on tidyr::gather for the examples
p <- ggplot(cars) + geom_histogram(aes(values), bins=10)
p + facet_wrap(~ind, scales="free_x") p + subplot_wrap(~ind) |
It seems to me this would fit better into an extension package (e.g., GGally or ggforce). Regardless of where this could live, I'd like to mention that your TODO in the theme code could be a use case for this PR: #2784 |
I thought this was a common enough use case and a simple enough change to make it into the main repo. I can submit this to ggforce though, if you think it would be more appropriate. Actually, the best course of action would probably be an overhaul of facets because I suppose that all the various hacks that accumulated over the years ( This would at least get the ball rolling showing use cases for such a change. |
Recently facets formula interface was augmented to accept rows and columns similarly we might think over the other direction by allowing a formula interface to the x / y interface? xyplot(uempmed+ psavert+unemploy~ date, data=economics) # you can make nicer
in ggplot2 you need the data to be in the long format first the question is whether we want to allow this based on the initial non stacked dataset ? ggplot(economics_long, aes(date, value)) +
geom_line()+
facet_grid( variable~. ,scales="free_y")
ggplot(economics, aes(formula= uempmed+ psavert+unemploy~ date)) +
geom_line()+
facet_grid_stack( ) the yvalue and yvars would be then an internal computer variable that we can access later on |
FWIW I'll accept this functionality in ggforce |
I'm going to close this as this should be pursued in an extension package |
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
Facets are meant to display different subsets of the same data.
However, with data in the "long" format (i.e. several variables stacked
in the same column and an additional column indicating which is which),
they can also be used to create the same plot for several variables
(e.g. a histogram). In that case, the scales are often free (the
variables are different and hence often do not have the same unit).
The problem with that approach is that the axis title is often a generic,
uninformative word (e.g. "values") while the real labels for the axis
are the facet labels, which are on the wrong side of the plot.
These functions use
facet_wrap
andfacet_grid
to create subplots andadjust the position and aspect of the facet labels to make them look like
axis titles.