![](/img/trans.png)
[英]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.