我试图绘制堆积的条形图,并用我计算出的值标记每个条形。 我有eah酒吧的价值向量。如果按原样使用矢量,则会收到警告“错误:美学的长度必须为1或与数据相同(156):标签”,因此我将矢量添加到数据中,并对每个行使用rep条,但是当我使用geom_text时,我为每个条获得了许多标签(每个条中的堆栈数)。
我的代码:
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)
谢谢!
答案 0 :(得分: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-05-16创建