【问题标题】:Label stacked bar plot with ggplot2使用 ggplot2 标记堆积条形图
【发布时间】:2019-05-16 21:30:00
【问题描述】:

我想绘制堆积条形图,并用我计算的值标记每个条形图。 我有每个条的值向量。如果我按原样使用向量,则会收到警告“错误:美学必须长度为 1 或与数据 (156): 标签相同”,因此我将向量添加到数据中,对每个行的所有行使用 rep bar,但是当我使用 geom_text 时,我会为每个 bar 获得许多标签(每个 bar 中的堆栈数)。

我的代码:

g <- ggplot(df, aes(x = RNA, y = Value))
g + geom_bar(aes(fill=fct_reorder(Cancer, Value, sum, desc=TRUE)), width = 0.5, stat="identity") + 
   geom_text(aes(label = lbls), vjust = -0.5, position = position_dodge(0.9))+ 
  theme(axis.text.x = element_text(angle=90, vjust=0.6),
        axis.text.y = element_blank()) +
  scale_fill_manual(values=c("#df4a7a",
                            "#c97b7a",
                            "#de5137",
                            "#d08935",
                            "#a78d57",
                            "#d2d23e",
                            "#cfd88d",
                            "#67993f",
                            "#76d854",
                            "#66db9f",
                            "#529477",
                            "#81dacf",
                            "#6bb2d5",
                            "#6387d7",
                            "#777ba7",)) +
  labs(title="Cancer Types", 
       subtitle="")

我的数据框,其中包含要标记的值列:

                              Cancer                  RNA       Value  Tabs
mp.117                        Breast                  snoRNA     3 0.268
tmp.118   Digestive/Gastrointestinal                  snoRNA     0 0.268
tmp.119 Endocrine and Neuroendocrine                  snoRNA     1 0.268
tmp.120                          Eye                  snoRNA     0 0.268
tmp.121                Genitourinary                  snoRNA     0 0.268
tmp.122                    Germ Cell                  snoRNA     0 0.268
tmp.123                  Gynecologic                  snoRNA     0 0.268
tmp.124                Head and Neck                  snoRNA     0 0.268
tmp.125            Hematologic/Blood                  snoRNA     0 0.268
tmp.126              Musculoskeletal                  snoRNA     1 0.268
tmp.127                   Neurologic                  snoRNA     0 0.268
tmp.128         Respiratory/Thoracic                  snoRNA     0 0.268
tmp.129                         Skin                  snoRNA     0 0.268
tmp.143                       Breast circRNA | Circular RNA      3 0.005
tmp.144   Digestive/Gastrointestinal circRNA | Circular RNA      1 0.005
tmp.145 Endocrine and Neuroendocrine circRNA | Circular RNA      1 0.005
tmp.146                          Eye circRNA | Circular RNA      0 0.005
tmp.147                Genitourinary circRNA | Circular RNA      1 0.005
tmp.148                    Germ Cell circRNA | Circular RNA      0 0.005
tmp.149                  Gynecologic circRNA | Circular RNA      4 0.005
tmp.150                Head and Neck circRNA | Circular RNA      3 0.005
tmp.151            Hematologic/Blood circRNA | Circular RNA      0 0.005
tmp.152              Musculoskeletal circRNA | Circular RNA      0 0.005
tmp.153                   Neurologic circRNA | Circular RNA      0 0.005
tmp.154         Respiratory/Thoracic circRNA | Circular RNA      1 0.005
tmp.155                         Skin circRNA | Circular RNA      0 0.005

带有标签的向量:

lbl = c(0.821, 0.899, 0.410, 0.028, 0.257, 0.217, 0.474, 0.220, 0.210, 0.268,   NaN, 0.005)

谢谢!!

【问题讨论】:

  • 你打算在哪里使用lbl?也许您可以以更容易复制的格式发布数据样本,例如dput
  • 我相信这篇文章提供了您正在寻找的答案。您无需手动计算标签。 stackoverflow.com/questions/6644997/…

标签: r ggplot2 geom-text


【解决方案1】:
library(ggplot2)
library(dplyr)
library(forcats)


#your plot without geom_text
g <- ggplot(df, aes(x = RNA, y = Value)) + 
  geom_bar(aes(fill=fct_reorder(Cancer, Value, sum, desc=TRUE)), 
           width = 0.5, stat="identity") + 
  theme(axis.text.x = element_text(angle=90, vjust=0.6),
        axis.text.y = element_blank()) +
  scale_fill_manual(values=c("#df4a7a", "#c97b7a","#de5137","#d08935",
                             "#a78d57","#d2d23e","#cfd88d","#67993f",
                             "#76d854","#66db9f","#529477","#81dacf",
                             "#6bb2d5","#6387d7","#777ba7")) +
  labs(title="Cancer Types", subtitle="")



tabdf <- df %>% group_by(RNA) %>% summarise_at(vars(Tabs,Value), list(~mean(.)))

g +  geom_text(data = tabdf ,aes(y=Value, label = Tabs, fill = NULL))

reprex package (v0.2.1) 于 2019 年 5 月 16 日创建

【讨论】:

    猜你喜欢
    • 2013-11-07
    • 2019-01-14
    • 1970-01-01
    • 2021-10-16
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多