Using facet_grid and facet_wrap Together

I am trying to create a chart using facet_wrap with facet_grid inside each of the wrapped faces, but I cannot. Any suggestions?

For example, if I make comparisons on an annualized basis for monthly average values ​​of 2 quantities, I would like to have -

  • 2 faces, one for each value,
  • Each of the 5-phase faces has 12 faces inside it for each month.
  • Each month, a facet has two aspects within it each year.

Closest I can come

 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) 

enter image description here

While what I would ideally like is something like this (in my opinion, similar to ggplot() ... + facet_grid(Year ~ Month) + facet_wrap(Facet2~.) ) -

enter image description here

PS: I think that the edges in the latter are much more distinguishable and more accurate. Comments? Any alternatives?

+6
source share
3 answers

Perhaps I do not understand what you are trying to do, but is not achieving what you want?

 ggplot(df, aes(ValueX, ValueY)) + geom_point() + facet_grid(Facet2 ~ Facet1) 

If you want to change the facet headers according to your example, look at the argument labeller facet_grid() .

0
source

I'm not sure I understand what you want to do, but it’s easier for me to get what you want using the grid here:

 library(latticeExtra) xyplot(ValueY~ValueX|Facet1+Facet2,data=df, between=list(x=2,y=0), par.settings = ggplot2like(),axis=axis.grid) 

enter image description here

0
source

This solution is not very pretty, but it can be implemented using cowplot :

 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) 

enter image description here

0
source

Source: https://habr.com/ru/post/958657/


All Articles