如何在 ggplot 的多面饼图上正确放置标签?
Posted
技术标签:
【中文标题】如何在 ggplot 的多面饼图上正确放置标签?【英文标题】:How to properly put lables on a faceted pie chart in ggplot? 【发布时间】:2021-11-18 00:03:32 【问题描述】:所以我有这样的数据:
tag team count pct
E A 12 1.00
E B 5 .50
E C 1 .20
I B 5 .50
I C 4 .80
我想用这些数据制作一个分面饼图,但我无法让文本标签出现在绘图上的适当位置......我在这里做错了什么......
ggplot(data, aes(x = factor(1), y = pct, fill = tag)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start=0) +
theme_void()+ # remove background, grid, numeric labels
facet_wrap(~team)+
geom_text(aes(label = sprintf("%1.2f%%", 100*pct)))
【问题讨论】:
【参考方案1】:你在正确的轨道上!
小幅调整:
ggplot(df, aes(x = factor(1), y = pct, fill = tag)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start=0) +
theme_void()+ # remove background, grid, numeric labels
facet_wrap(~team)+
geom_text(aes(label = sprintf("%1.2f%%", 100*pct)),position = position_stack(vjust = 0.5)) +
coord_polar(theta = 'y')
需要包含position= position_stack(vjust = 0.5)
和coord_polar(theta = 'y')
【讨论】:
以上是关于如何在 ggplot 的多面饼图上正确放置标签?的主要内容,如果未能解决你的问题,请参考以下文章
如何在逻辑图上制作标签(使用 ggplot/ggplotly 制作)显示 % 与小数