【问题标题】:Bar plot of groups with unique sub groups names具有唯一子组名称的组的条形图
【发布时间】:2018-07-02 16:33:38
【问题描述】:

这与this question 非常相似,但希望不同之处足以值得提出一个新问题。

我在 R 中的年度时间序列数据中最多有 5 个观察值(“小时”)。因此,每个观察都包含一个年份组 (grp_id) 和它自己的唯一 ID (short_text)。

我想在 ggplot2 的单个条形图中显示所有观测值(小时),每年,在 ggplot2 中(根据唯一 ID 短文本设置闪避),但 喜欢除了年度组名 (grp_id) 之外,沿 x 轴运行的每个观测值的唯一 ID (short_text)。

有点像这样

plot

示例数据:

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 以使其更清晰

标签: r ggplot2


【解决方案1】:

这对我有用:

# Create a new label which is concatenation of group and id
allBinsTop5.m$newlabel<-paste0("G",allBinsTop5.m$grp_id ,"-ID",allBinsTop5.m$short_text)

# Add a geom_text command that specifies new label along with dodge 
ggplot(allBinsTop5.m, aes(grp_id, value)) +   
    geom_bar(aes(fill = newlabel), position = "dodge", stat="identity") + 
    geom_text(aes(label=newlabel),position=position_dodge(width=.9),
              vjust=allBinsTop5.m$value)+
    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")

【讨论】:

  • 这将组和 ID 的组合放在每个条形上方,而 OP 想要每个条形上方的值和 ID 作为 x 轴标签,下面嵌套年份标签
【解决方案2】:

可以使用this answer 中提供的代码来解决我的问题。

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")

plot with multiple groups and unique sub-groups labels

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多