繁体   English   中英

如何将标签添加到堆叠条形图

[英]How to add labels to a stacked bar graph

我希望将数值添加到我生成的堆叠条形图中每个堆栈的中间(代码如下)。 我发现的大多数示例都是针对一列中的信息的,每当我尝试修改它时,都会遇到有关长度要求的错误。

DA <- data.frame(
  Imp=c("2015","2019"),
  "mismatch"=c(220,209),
  "match"=c(3465,3347),
  "NA"=c(501,630),
  check.names = FALSE)

DA %>%
  pivot_longer(-Imp) %>%
  ggplot(aes(x = Imp, y = value, fill = name)) + geom_col(position = "stack") + 
  scale_fill_manual(name=" ", values=c("aquamarine4", "orange", "coral")) +
  theme_light() +
  theme(legend.position = "bottom") +
  scale_y_continuous(expand = c(0,0)) +
  geom_text(aes(x=1, y=4300, label="Stretch it"), vjust=-1) +
  labs(y="Count", x="Imputed Genotypes") +
  geom_bar(stat = "identity", color="white", width = 1) 

像这样?

library(tidyverse)

DA <- data.frame(
  Imp=c("2015","2019"),
  "mismatch"=c(220,209),
  "match"=c(3465,3347),
  "NA"=c(501,630),
  check.names = FALSE)

DA %>%
  pivot_longer(-Imp) %>%
  ggplot(aes(x = Imp, y = value, fill = name)) +
  geom_col(color = "white", lwd = 1,
           position = "stack", width = 0.75) + 
  scale_fill_manual(name="", values=c("aquamarine4", "orange", "coral")) +
  scale_y_continuous(expand = c(0,0),
                     limits = c(0, 4200)) +
  labs(y="Imputed Genotypes (Count)") +
  geom_text(aes(label = value), color = "white", size = 5, 
            position = position_stack(vjust = 0.5), 
            show.legend = FALSE) +
  theme_light(base_size = 18) +
  theme(legend.position = "right",
        axis.title.x = element_blank())

代表 package (v2.0.1) 于 2021 年 12 月 19 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM