一起使用facet_grid和facet_wrap [英] Using facet_grid and facet_wrap Together

查看:68
本文介绍了一起使用facet_grid和facet_wrap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 facet_wrap 和每个包装的构面内的 facet_grid 创建图表,但无法这样做.有什么建议吗?

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个方面,每个数量一个,
  • 5个数量方面中的每个方面在每个月的内部都有12个方面
  • 每年每个月的面都有两个面

我能找到的最接近的是这个

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)

虽然,我理想地希望是这样的东西(在我看来,类似于 ggplot()... + facet_grid(年〜月)+ facet_wrap(Facet2〜.))-

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:我认为后者的各个方面更容易区分和整理.注释?有其他选择吗?

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)

如果要更改构面标题以匹配您的示例,请查看 facet_grid() labeller 参数.

If you want to change the facet titles to match your example have a look at the labeller argument of facet_grid().

这篇关于一起使用facet_grid和facet_wrap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆