如何正确地将标签放在 ggplot 中的多面饼图上?
How to properly put lables on a faceted pie chart in ggplot?
所以我有这样的数据:
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)))
你走对了!
细微调整:
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')
所以我有这样的数据:
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)))
你走对了!
细微调整:
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')