r - 具有唯一子组名称的组的条形图
问题描述
这与这个问题非常相似,但希望足够不同,值得提出一个新问题。
我在 R 中的年度时间序列数据中对我的值(“小时”)最多有 5 个观察值。因此,每个观察值都包含一个年份组(grp_id)和它自己的唯一 ID(short_text)。
我想在 ggplot2 的单个条形图中显示所有观察值(小时),每年,在 ggplot2 的单个条形图中(根据唯一 ID,short_text 设置为闪避),但也希望每个观察的唯一 ID(short_text)运行沿 x 轴,除了年度组名称 (grp_id)。
有点像这样
示例数据:
structure(list(short_text = 1:20, grp_id = c(3, 3, 4, 4, 4, 4,
4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7), variable = c("hours",
"hours", "hours", "hours", "hours", "hours", "hours", "hours",
"hours", "hours", "hours", "hours", "hours", "hours", "hours",
"hours", "hours", "hours", "hours", "hours"), value = c("1371.28228277413",
"4117.61650085692", "4462.05401592845", "4563.63839159463", "6617.47698685436",
"10498.3641321107", "20735.9579131878", "14838.8668295674", "14884.5002478823",
"15846.5620639966", "20996.7282642214", "73321.8056031507", "29960.4636692291",
"32475.8922108366", "35534.418796457", "49040.3036856129", "121255.358807715",
"15288.7191802188", "69888.6583792545", "127734.59190842")), .Names = c("short_text",
"grp_id", "variable", "value"), row.names = c(NA, 20L), class = "data.frame")
到目前为止,我最接近的是这个,但唯一的 ID 不会沿着 x 轴显示:
ggplot(allBinsTop5.m, aes(grp_id, value)) +
geom_bar(aes(fill = short_text), position = "dodge", stat="identity") +
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.x = element_text(angle = 45, vjust = 1, hjust=1),
legend.position="none")
解决方案
可以使用此答案中提供的代码来解决我的问题。
ggplot(data = aa, aes(x = short_text, y = value, fill = short_text)) +
geom_bar(stat = "identity", width = 1) +
facet_wrap(~grp_id, strip.position = "bottom", scales = "free_x") +
theme(panel.spacing = unit(0, "lines"),
strip.background = element_blank(),
strip.placement = "outside") +
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.x = element_text(angle = 45, vjust = 1, hjust=1),
legend.position="none")
推荐阅读
- asp.net - Windows 10、IIS 10 和多个
- angular - Angular - 无法绑定属性,因为它不是“div”的已知属性
- onelogin - 当用户从 onelogin IDP 强制注销时,不调用服务提供者的 SLS 端点
- python - Python DataFrame:如何删除填充多列的行?
- node.js - Windows 上的 node-gyp 问题:配置错误
- c# - 如何使用来自 Yahoo Api 的索引解析 json 数组
- angular - 注释插件中的 Chart.js 行未出现
- angular - Ngfor 绑定问题
- android - Mapbox Android 表达式问题
- nix - 在 Nixpkgs 中覆盖 Firefox makeFlags