简体   繁体   English

一起使用 facet_grid 和 facet_wrap

[英]Using facet_grid and facet_wrap Together

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.我正在尝试使用facet_wrap创建一个图表,在每个包装的 facet 内都有一个facet_grid ,但我无法创建。 Any suggestions?有什么建议?

For example, if I'm doing year-on-year comparisons for monthly averages of 2 quantities, I would like to have -例如,如果我要对 2 个数量的月平均值进行同比比较,我希望 -

  • 2 facets, one for each quantity, 2个面,每个数量一个,
  • Each of the5 quantity facets has 12 facets inside of it for each of the months每个月的 5 个数量面中的每一个面都有 12 个面
  • 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~.) ) -虽然,我理想中想要的是这样的东西(在我看来,类似于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. PS:我认为后者的方面更容易区分,也更整洁。 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 facet_grid() .如果你想改变小标题以匹配您的例子来看看在labeller的参数facet_grid()

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)

在此处输入图片说明

This can easily be done using cowplot :这可以使用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)

在此处输入图片说明

For this kind of task, the best solution - IMOHO - is to use the ggpubr package - see here for examples:对于此类任务,最好的解决方案 - IMOHO - 是使用 ggpubr 包 - 请参见此处的示例:

http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/81-ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-page/ http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/81-ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-page/

You will not be able to use nested facets like you mention in your question (I don't think the support for this kind of facets exists at the moment), but could come up with something pretty close with some customization.您将无法使用您在问题中提到的嵌套方面(我认为目前不存在对此类方面的支持),但可以通过一些自定义来提出一些非常接近的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM