Using facet_grid and facet_wrap Together
facet_wrap multiple pages
facet_wrap vs facet_grid
facet_wrap multiple variables
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?
Maybe I'm misunderstanding what you're trying to do, but does this not achieve what you want?
ggplot(df, aes(ValueX, ValueY)) + geom_point() + facet_grid(Facet2 ~ Facet1)
If you want to change the facet titles to match your example have a look at the
labeller argument of
Easy multi-panel plots in R using facet_wrap() and facet_grid() from , ggplot2 makes it easy to use facet_wrap() with two variables by simply stringing them together with a + . Although it's easy, and we show an example here, we would generally choose facet_grid() to facet by more than one variable in order to give us more layout control. ggplot2 has a two primary techniques for creating small multiple charts: facet_wrap and facet_grid. The primary difference between facet_wrap and facet_grid is in how they lay out the panels of the small multiple chart. Essentially, facet_wrap places the first panel in the upper right hand corner of the small multiple chart.
I am not sure to understand what you want to do , But I think it easier to get what you want using lattice` here:
library(latticeExtra) xyplot(ValueY~ValueX|Facet1+Facet2,data=df, between=list(x=2,y=0), par.settings = ggplot2like(),axis=axis.grid)
15 Facetting, The differences between facet_wrap() and facet_grid() are illustrated in You can use multiple variables in the rows or columns, by “adding” them together, e.g. � ggplot2 makes it easy to use facet_wrap () with two variables by simply stringing them together with a +. Although it’s easy, and we show an example here, we would generally choose facet_grid () to facet by more than one variable in order to give us more layout control. Compute the counts for the plot so we have two variables to use in faceting:
This can easily be done using
plot1<-ggplot(df[df$Facet2==1,], aes(ValueX, ValueY)) + geom_point() + facet_grid(Year ~ Month)+ theme_bw() plot2<-ggplot(df[df$Facet2==2,], aes(ValueX, ValueY)) + geom_point() + facet_grid(Year ~ Month)+ theme_bw() plot_grid(plot1, plot2, labels = c("1", "2"), nrow = 2)
Plotting multiple groups with facets in ggplot2, Facet wrap. Here, a single categorical variable defines subsets of the data. The panels are calculated in a 1 dimensional ribbon that can be wrapped to multiple� Lay out panels in a grid facet_grid () forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data. If you have only one variable with many levels, try facet_wrap ().
Lay out panels in a grid — facet_grid • ggplot2, If you have only one variable with many levels, try facet_wrap() . facet_grid( rows = NULL, cols = NULL, scales = "fixed", space = "� Wrap a 1d ribbon of panels into 2d. facet_wrap () wraps a 1d sequence of panels into 2d. This is generally a better use of screen space than facet_grid () because most displays are roughly rectangular.
ggplot2 facet : split a plot into a matrix of panels - Easy Guides, Facet with one variable. The graph is partitioned in multiple panels by levels of the group “supp”: # Split in vertical direction bp + facet_grid(supp� facet_grid; facet_wrap; Modifying facet label appearance; Modifying facet label text; Free scales; Problem. You want to do split up your data by one or more variables and plot the subsets of data together. Solution Sample data. We will use the tips dataset from the reshape2 package.
Facets (ggplot2), Sample data; facet_grid; facet_wrap; Modifying facet label appearance to do split up your data by one or more variables and plot the subsets of data together. Solution. Sample data. We will use the tips dataset from the reshape2 package. In order to do so, you simply modify your code to add +facet_wrap() and specify that ~measure, our key variable, should be used for facetting. ggplot(econdatalong, aes(x=Country, y=value))+ geom_bar(stat='identity', fill="forest green")+ facet_wrap(~measure)
- ggplot2 does not support this directly but someone with fancy grid skills could do this. You could also export the graphic and the the same thing in < minute with inkscape.
facet_gridgives 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?
- @Roland -
facet_grid(Year ~ Month) + facet_wrap(Facet2~.)is what I'm trying to achieve, so there are 2 facets for
Facet2, and then each of those facets are then broken down into the 24
Year¬Monthfacets. The current methodology of
facet_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.
- I can only reiterate what Tyler said: ggplot2 doesn't support this. You can only change distances and facet strips of the plot with grid functionality.
- Thanks. Do you guys agree that this is cleaner though? Would it be useful to the general community if this were filed as a feature request?
- Thanks but nope. I'm trying to implement multiple levels of faceting, not edit the one level of faceting itself.
- If this doesn't achieve what you want, you need to improve your question. This exactly produces what you show in your mock-up.
- Thanks a lot for your input guys, I have edited the question and shown something I hacked together something that looks like what I'm trying to get to. I'm sorry if the question wasn't clear earlier, hopefully it is clear now.
- Thanks a lot for your input @agstudy , I have edited the question and shown something I hacked together something that looks like what I'm trying to get to. I haven't used
latticebefore so I couldn't manipulate your suggestion too much, but I tried playing with the
Facet1+Facet2part, that didn't get my anywhere.