我具有以下格式的数据,并希望使用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()
现在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()
是否可以为这两个构面组的val
获取单独的轴?还是一般认为这是不好的做法,我应该首先将该图分为两个单独的图?