r - ggplot2中的分组条形图
问题描述
我想复制这个图(在组之间进行分面) (分面如中完成)在 此处输入图像描述 这是我的尝试:
library (ggplot2)
data<- data.frame(
d = rep(LETTERS[21:26], 10),
val = rnorm (60),
c = rep(LETTERS[1:10], each = 6)
)
ggplot(data, aes(c, val)) +
geom_bar(stat = 'identity', aes(fill = val), position = "dodge") +
facet_grid(data[, 1] ~ .)
并且图表是垂直排列的,而不是躲避的位置。我应该怎么办?
提前谢谢你。
解决方案
我觉得你不需要facet_grid()
,随便填'd'
。
ggplot(data, aes(c, val)) +
geom_bar(stat = 'identity', aes(fill = d), position = "dodge")
根据 OP 的评论,我们也可以使用facet_grid(cols = vars(d))
.
ggplot(data, aes(c, val)) +
geom_bar(stat = 'identity', aes(fill = val), position = "dodge") +
facet_grid(cols = vars(d)) # or facet_grid(. ~ d)
推荐阅读
- html - 将“小部件”样式化为内联并在较小的屏幕下彼此下方
- javascript - JavaScript 输入按钮未按预期工作
- java - 尝试在 JPanel 中添加更多形状时出错
- html - 尝试使用角度可编辑 div 中的文本内容更新变量时,可编辑 div 上的奇怪行为。有什么解释吗?
- python - Python SyntaxError:非 ASCII 字符
- mysql - Mysql:以时间为条件限制插入、更新、删除动作
- inno-setup - Inno Setup - 显示 MsgBox 并单击“确定”返回上一页
- android - 如何初始化 exoplayer 以播放破折号(.mpd)视频
- c++ - 在多个对象中找到一个相同的元素
- javascript - 过滤掉无效电子邮件,使其不会从警报提示中显示到文档中