我认为问题在于 geom_text 图层不太知道要堆叠什么,特别是不知道 订购 堆叠。这是因为 Fuel 列确定条形图的堆叠顺序,但根本不映射条形图 geom_text 层。
geom_text
Fuel
最简单的解决方法是将美学映射移动到 ggplot() 而不是单个层。这样它将被后续层继承(也避免了重复 x 和 y 每层的美学)。通过魔术 ggplot , 即使 geom_text 不使用 fill 对于填充的映射,它仍然知道按位置的美学分组/顺序。
ggplot()
x
y
ggplot
fill
我做了那个改变,改变了 position_stack 至 position_fill 对于文本图层,并替换 geom_bar(stat = "identity") 同 geom_col 这是成语的首选方法。
position_stack
position_fill
geom_bar(stat = "identity")
geom_col
ggplot(data = df, aes(factor(1), y = fuel.share, fill = Fuel)) + geom_col(position = "fill") + geom_text(aes(label = label) , size =2 ,position = position_fill(vjust = 0.5)) + coord_polar(theta = "y") + facet_wrap(prov ~ .) + scale_x_discrete(name = "", breaks = NULL) + scale_y_continuous(name = "", breaks = NULL)