簡體   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