首先,您为两个层提供不同的y值。其次,默认情况下,条形图是堆叠的,但标签不是。我们需要提供
position
为了使标签与条的堆叠相匹配,我们可以使用justice参数将标签放置在条的一半:
ggplot(DATA, aes(x = 1, n_RCA, fill=EthnicCd)) +
geom_col(width = 1, color="white") +
geom_label_repel(
aes(x = 1.49, label = paste0(n_RCA,"\n(",percent,")")),
position = position_stack(vjust = 0.5),
size = 3,
show.legend = FALSE
) +
coord_polar("y", start = 5.5) +
theme_void() +
scale_fill_brewer(palette="Set1")+
guides(fill = guide_legend(title = bquote(~bold("Ethnic Background"))))
我将标签的x位置设置为~1.5,这样它们就可以放在馅饼的外面。这是因为我设置
x = 1
对于条形图,条形图的“宽度”为1,因此其范围为0.5至1.5。
极坐标有时会令人困惑,首先用笛卡尔坐标绘制通常会有所帮助,这样堆叠问题就会变得更加明显。