使用geom_text在填充的条形图中居中标签

4

我刚接触ggplot2(和R),正在尝试制作一个填充的条形图,每个方块内都有标签指示组成该块的百分比。

这是我当前图表的示例,我想添加标签:

##ggplot figure 
library(gpplot2)
library(scales) 

#specify order I want in plots
ZIU$Affinity=factor(ZIU$Affinity, levels=c("High", "Het", "Low"))
ZIU$Group=factor(ZIU$Group, levels=c("ZUM", "ZUF", "ZIM", "ZIF"))

ggplot(ZIU, aes(x=Group))+
geom_bar(aes(fill=Affinity), position="fill", width=1, color="black")+
scale_y_continuous(labels=percent_format())+
scale_fill_manual("Affinity", values=c("High"="blue", "Het"="lightblue", "Low"="gray"))+
labs(x="Group", y="Percent Genotype within Group")+
ggtitle("Genotype Distribution", "by Group")

我希望在每个框中心添加标签,显示该框所代表的百分比。

我尝试使用以下代码添加标签,但它不断产生错误消息“Error: geom_text requires the following missing aesthetics: y”,但我的图没有y美学,这是否意味着我不能使用geom_text?(另外,我不确定一旦y美学问题得到解决,geom_text语句的其余部分是否会实现我所需的,在每个框中心的白色标签。)

ggplot(ZIU, aes(x=Group)) +
geom_bar(aes(fill=Affinity), position="fill", width=1, color="black")+
geom_text(aes(label=paste0(sprintf("%.0f", ZIU$Affinity),"%")),
    position=position_fill(vjust=0.5), color="white")+
scale_y_continuous(labels=percent_format())+
scale_fill_manual("Affinity", values=c("High"="blue", "Het"="lightblue", "Low"="gray"))+
labs(x="Group", y="Percent Genotype within Group")+
ggtitle("Genotype Distribution", "by Group")

此外,如果有人对消除NA值有建议,将不胜感激!我尝试过。
geom_bar(aes(fill=na.omit(Affinity)), position="fill", width=1, color="black")

但是出现了错误:"错误:美学必须是长度为1或与数据相同(403):填充,x"

 dput(sample)
 structure(list(Group = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 
 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
 2L), .Label = c("ZUM", "ZUF", "ZIM", "ZIF"), class = "factor"), 
StudyCode = c(1, 2, 3, 4, 5, 6, 20, 21, 22, 23, 143, 144, 
145, 191, 192, 193, 194, 195, 196, 197, 10, 24, 25, 26, 27, 
28, 71, 72, 73, 74, 274, 275, 276, 277, 278, 279, 280, 290, 
291, 292), Affinity = structure(c(3L, 2L, 1L, 2L, 3L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 1L, 1L, 1L, 3L, 
2L, 1L, 2L, 2L, 1L, 2L, 2L, 3L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 
3L, 2L, 2L, 2L), .Label = c("High", "Het", "Low"), class = "factor")), .Names = c("Group", 
"StudyCode", "Affinity"), row.names = c(NA, 40L), class = c("tbl_df", 
"tbl", "data.frame"))

非常感谢!


1
在SO上有几个关于居中盒子标签的问题。例如,这里这里 - eipi10
我在上面的评论中指的是“bar”标签,而不是“box”标签。 - eipi10
嗨@eipi10,当我尝试解决我的问题时,我查看了这两篇帖子,但在每种情况下,他们的图形都有一个y美学,而我的没有,因此会产生错误消息。由于我的图表是“整体的一部分”,所以我无法在y轴上放置任何东西--这是否意味着我根本不能使用geom_text? - Sarah
你能否发布一份数据样本?请将 dput(data_sample) 的输出粘贴到你的问题中。 - eipi10
已添加 @eipi10!如果有帮助,请告诉我。谢谢! - Sarah
1个回答

5
链接的示例具有“y”美学,因为数据是预先汇总的,而不是由ggplot在内部计数。对于您的数据,类似的方法是:
library(scales) 
library(tidyverse)

# Summarize data to get counts and percentages
ZIU %>% group_by(Group, Affinity) %>%
  tally %>%
  mutate(percent=n/sum(n)) %>%   # Pipe summarized data into ggplot
  ggplot(aes(x=Group, y=percent, fill=Affinity)) +
   geom_bar(stat="identity", width=1, color="black") +
   geom_text(aes(label=paste0(sprintf("%1.1f", percent*100),"%")), 
             position=position_stack(vjust=0.5), colour="white") +
   scale_y_continuous(labels=percent_format()) +
   scale_fill_manual("Affinity", values=c("High"="blue", "Het"="lightblue", "Low"="gray")) +
   labs(x="Group", y="Percent Genotype within Group") +
   ggtitle("Genotype Distribution", "by Group")

enter image description here

另一个选择是使用线图,这可能会使相对值更清晰。假设“Group”值不形成自然序列,则该线只是作为指南,以区分在不同“Group”值下的“Affinity”值。
ZIU %>% group_by(Group, Affinity) %>%
  tally %>%
  mutate(percent=n/sum(n)) %>%   # Pipe summarized data into ggplot
  ggplot(aes(x=Group, y=percent, colour=Affinity, group=Affinity)) +
  geom_line(alpha=0.4) +
  geom_text(aes(label=paste0(sprintf("%1.1f", percent*100),"%")), show.legend=FALSE) +
  scale_y_continuous(labels=percent_format(), limits=c(0,1)) +
  labs(x="Group", y="Percent Genotype within Group") +
  ggtitle("Genotype Distribution", "by Group") +
  guides(colour=guide_legend(override.aes=list(alpha=1, size=1))) +
  theme_classic()

enter image description here


1
太棒了!非常感谢 @eipi10!! :) - Sarah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接