I'm trying to create a chart using facet_wrap
with a facet_grid
inside each of the wrapped facets but I'm unable to. Any suggestions?
For example, if I'm doing year-on-year comparisons for monthly averages of 2 quantities, I would like to have -
- 2 facets, one for each quantity,
- Each of the5 quantity facets has 12 facets inside of it for each of the months
- Each month facet has two facets inside of it for each of the year
The closest I can come is this,
library(ggplot2)
# create dataset
df <- data.frame(
Facet1 = rep(c(1,2,3),24),
Facet2 = c(rep(1,24),rep(2,24)),
Year = rep(c(rep(2012,12),rep(2013,12)),2),
Month = rep(rep(1:12,2),2),
ValueX = sample(0:5,144,replace = TRUE),
ValueY = sample(0:5,144,replace = TRUE)
)
df <- df[!(df$Facet1 == 2 & df$Facet2 == 2),]
ggplot(df, aes(ValueX, ValueY)) + geom_point() +
facet_grid(Facet2 + Year ~ Month)
While, what I would ideally like, is something along the lines of this (In my mind, analogous to ggplot() ... + facet_grid(Year ~ Month) + facet_wrap(Facet2~.)
) -
PS: I think the facets in the latter are much more distinguishable and neater to go through. Comments? Any alternatives?
facet_grid
gives you the plot you want, but you are not happy with the distances between facets and with the facet strips on the right and want to change them? – Rolandfacet_grid(Year ~ Month) + facet_wrap(Facet2~.)
is what I'm trying to achieve, so there are 2 facets forFacet2
, and then each of those facets are then broken down into the 24Year¬Month
facets. The current methodology offacet_grid(Facet2 + Year ~ Month)
accomplishes that as far as the eventual 48 facets are concerned, but lookwise, I think the second picture is a clearer faceting than the first one and allows for better readability. – TheComeOnMan