【发布时间】:2016-01-21 09:33:40
【问题描述】:
考虑这个数据框:
df <- data.frame(vars=c(rnorm(3),rnorm(3,2,1), rnorm(3,1,1)),
names=c("a","b","c","a","d","b","c","a","f"),
groups=c(rep("x",3),rep("y",3),rep("z",3)))
我正在用 ggplot 绘制这个:
ggplot(df, aes(reorder(names, vars), names)) + geom_bar(stat="identity") +
theme_bw() + facet_grid(groups~., scales="free_x") + coord_flip() + ylab(NULL) + xlab(NULL)
看起来像这样
我现在想要以下内容:
- 每个网格项目都应该删除未使用的项目,例如在“x”网格中不应有“d”和“f”
- x 轴应该是“vars”列的值。每个网格中的比例应相同,应删除整体 x 比例。我只希望每个网格中的条形比例保持不变
- 每个网格中的条形应按降序排列(顶部较长的条形)
更新:
使用here 的建议进行编辑我收到此错误:
ggplot(df, aes(names,vars)) + geom_bar(stat="identity") + coord_flip() +
theme_bw() + facet_wrap(~groups,nrow = 3,scales = "free_x")
Error in facet_render.wrap(plot$facet, panel, plot$coordinates, theme, :
ggplot2 does not currently support free scales with a non-cartesian coord or coord_flip.
In addition: Warning message:
Stacking not well defined when ymin != 0
当我删除 coord_flip() 时它可以工作,但我仍然收到警告,结果不是我想要的。
【问题讨论】:
-
可以查看here
-
@akrun 我添加了其他信息