ggplot中facet_grid()的独立轴

时间:2020-05-05 11:08:15

标签: ggplot2 facet-grid

我具有以下格式的数据,并希望使用facet_grid()中的ggplot2创建分组的条形图。

plot.df = data.frame(m   = factor(rep(1:3, 9)),
                     s   = factor(rep(rep(1:3, each=3),3)),
                     var = factor(rep(1:3, each = 9)),
                     val = rnorm(27))


ggplot(plot.df, aes(x=var, y=val, fill=m)) +
  facet_grid(.~s,scales="free") +
  geom_bar(position="dodge", stat="identity") +
  coord_flip() 

facet plot 1

现在var因素之一的规模与其他因素不同。假设在var == 3的所有情况下我们都需要不同的比例。我们可以将其与其他方面分开,如下所示:

plot.df$select = c(rep(0,18),rep(1,9))

ggplot(plot.df, aes(x=var, y=val, fill=m)) +
  facet_grid(select~s,scales="free") +
  geom_bar(position="dodge", stat="identity") +
  coord_flip() 

facet plot 2

是否可以为这两个构面组的val获取单独的轴?还是一般认为这是不好的做法,我应该首先将该图分为两个单独的图?

0 个答案:

没有答案
相关问题