[英]How can I access dimensions of labels plotted by `geom_text` in `ggplot2`?
[英]How can I move geom_text labels further out on a ggplot pie chart?
我有六组受访者(res)的一些调查数据。 我在图表中为要分配给每个组的颜色添加了一列,尽管这可能是多余的。 我制作了一个饼图,显示了每个组中的受访者人数:
Grp.PieChart <- ggplot(data = count(res, Grp, GrpColour), aes(x = "", y = n, fill = GrpColour)) +
geom_bar(stat = "identity") +
coord_polar(theta = "y") +
theme_void() +
geom_text(aes(label = n),
size = 8,
colour = "#161844",
family = "Raleway",
position = position_stack(vjust = 0.5)) +
ggtitle("Groups of Respondents") +
theme(
legend.title = element_blank(),
legend.text = element_text(colour = "#161844", size = 12, family = "Raleway"),
plot.title = element_text(colour = "#161844", size = 24, family = "Raleway", face = "bold")) +
scale_fill_manual(values = c("#21409A", "#6C217E", "#30BAED", "#39B44A", "#FFDA00", "#F47920"),
breaks = c("#21409A", "#6C217E", "#30BAED", "#39B44A", "#FFDA00", "#F47920"),
labels = c("Group A", "Group B", "Group C", "Group D", "Group E", "Group F"))
我对图表很满意,我只是想将数字移到离中心更远的地方。 目前它们大约在中心和边缘之间的中间,我希望它们离中心大约 75%,离边缘大约 25%。
我已经尝试了 position_stack、position_dodge、nudge_x、nudge_y 的各种配置,似乎都无法让它工作。 我对 R 也很陌生...
我的数据如下所示(共 209 行):
Grp GrpColour
Group F #F47920
Group B #6C217E
Group E #FFDA00
etc
帮助将不胜感激!
您可以通过更改geom_text
的 x 值来做到这geom_text
library(tidyverse)
mydat <- data.frame(n = c(4,27,12,30,85,51), group = LETTERS[1:6])%>% mutate(nperc = n*100/sum(n))
ggplot(data = mydat, aes(x ='', y= nperc, fill = group)) +
geom_col() +
geom_text(aes(x = 1.2,label = n), position = position_stack(vjust = 0.5)) +
coord_polar(theta = 'y') +
scale_fill_manual(values = c("#21409A", "#6C217E", "#30BAED", "#39B44A", "#FFDA00", "#F47920"))
由reprex 包(v0.3.0) 于 2020 年 3 月 24 日创建
看来您只需要增加 0.5 的值。
position = position_stack(vjust = 0.5))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.