【发布时间】:2018-07-02 16:33:38
【问题描述】:
这与this question 非常相似,但希望不同之处足以值得提出一个新问题。
我在 R 中的年度时间序列数据中最多有 5 个观察值(“小时”)。因此,每个观察都包含一个年份组 (grp_id) 和它自己的唯一 ID (short_text)。
我想在 ggplot2 的单个条形图中显示所有观测值(小时),每年,在 ggplot2 中(根据唯一 ID 短文本设置闪避),但 也 喜欢除了年度组名 (grp_id) 之外,沿 x 轴运行的每个观测值的唯一 ID (short_text)。
有点像这样
示例数据:
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")
【问题讨论】:
-
是什么使问题与您链接的问题不同?您是否尝试过使用my answer there 中显示的分面技巧?
-
与下方 x 轴标签对应的变量在哪里?你提到了年,但没有这样的专栏。我也同意@aosmith,我看不出与其他帖子有什么明显不同,除了你的第二个分组变量不在这里
-
年份由grp_id列中的id表示,在原题的括号中显示
-
原题中,他们的两组标签分别是ID和年份。在你的,你的两组标签是
grp_id,什么?variable?如果是这种情况,您应该扩展示例数据以包含该列的多个值 - 否则,我们不会向您展示太多 -
我的分组变量是 grp_id 和 short_text - 我已经改写了 q 以使其更清晰