R 向堆叠条形图添加百分比标签
我已经成功地在R中制作了一个堆叠条形图,其中几个不同类别的百分比加起来为100%。数据帧如下所示:R 向堆叠条形图添加百分比标签,r,ggplot2,R,Ggplot2,我已经成功地在R中制作了一个堆叠条形图,其中几个不同类别的百分比加起来为100%。数据帧如下所示: sujeito teste epentese vozeamento palavra tipo ortografia <chr> <chr> <chr> <chr> <chr> <chr> <chr> 1 a n 1 0
sujeito teste epentese vozeamento palavra tipo ortografia
<chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 a n 1 0 cats ts cs
2 b l 1 1 ducks ks cs
3 c l 1 1 cups ps cs
4 d l 0 0 grapes ps ces
5 e l 1 0 lakes ks ces
6 f n 1 0 gates ts ces
7 g n 0 0 books ks cs
8 h n 1 0 cakes ks ces
9 a n 1 1 kites ts ces
10 b n 1 0 boats ts cs
不过,我的意图是将其作为这张图片右侧的图表:
我尝试了不同的软件包,也处理了geom_文本,但仍然没有成功,特别是因为我不需要两个“填充类别”的标签,只需要红色的标签。
我希望这不是多余的。提前谢谢 仅标记可以使用的红色条,例如
if\u else
在geom\u文本中
此外,我删除了冗余的geom_col()
,并使用了一些随机示例数据
库(ggplot2)
图书馆(dplyr)
种子(42)
护墙板%
小组成员(sujeito,epentese)%>%
总结(quantidade=n())%>%
变异(frequencia=quantidade/sum(quantidade))%>%
ggplot(aes(x=sujeito,y=frequencia,fill=factor(epentese)))+
几何坐标(位置=位置堆栈(反向=真))+
geom_文本(aes(标签=if_-else(epentese==0,比例::百分比(频率,精度=1),“”),vjust=0,微移y=.01)+
比例y连续(标签=比例::百分比)+
实验室(title=“受试者的妊娠率”)+
主题(plot.title=element\u text(hjust=0.5))+
xlab(“主体”)+ylab(“频率”)
#>`summary()`通过'sujeito'重新分组输出(用'.groups'参数重写)
dados%>%
group_by(sujeito, epentese)%>%
summarise(quantidade = n())%>%
mutate(frequencia = quantidade/sum(quantidade))%>%
ggplot(., aes(x = sujeito, y = frequencia, fill = epentese))+
geom_col()+
geom_col(position = position_fill(reverse=TRUE))+
scale_y_continuous(labels=scales::percent)+
labs(title = "Epenthesis rates by subject")+
theme(plot.title = element_text(hjust = 0.5))+
xlab("Subject")+ylab("Frequency")