如何使标签在每个条形的每个部分内垂直居中?
这是我的代码:
ggplot(data, aes(A, fill=reorder(Y, desc(Y)))) +
geom_bar(position='fill') +
#geom_text(aes(label=scales::percent(5*round((..count..)/sum(..count..),2))), stat='count', position='stack') +
geom_text(aes(label = scales::percent(15*(..count..)/sum(..count..)),
y= 15*((..count..)/sum(..count..))), stat="count",
vjust = 0) +
labs(x="A", y="Stacked Percentage", fill="Y") +
facet_wrap(~B, nrow=1) +
scale_y_continuous(labels = scales::percent)
输出如下:
这是我的数据示例:
A W I Y Y2 B
<fct> <fct> <fct> <fct> <fct> <fct>
1 A1 ELAQY4349B 4905 Weakly Yes Yes B1
2 A2 ELAQY4349B 4905 Weakly No No B1
3 A3 ELAQY4349B 4905 No No B1
4 A4 ELAQY4349B 4905 No No B1
5 A5 ELAQY4349B 4905 Yes Yes B1
6 A1 ELAQY4349B 3642 Yes Yes B1
7 A2 ELAQY4349B 3642 Weakly No No B1
8 A3 ELAQY4349B 3642 Weakly Yes Yes B1
9 A4 ELAQY4349B 3642 Weakly Yes Yes B1
10 A5 ELAQY4349B 3642 Yes Yes B1
答案 0 :(得分:0)
找到了。子geom_text
geom_text(aes(label = scales::percent(15*(..count..)/sum(..count..)),
y= 15*((..count..)/sum(..count..))), stat="count",
position = position_stack(vjust = 0.5)) +