简体   繁体   English

如何在 ggplot 饼图上进一步移动 geom_text 标签?

[英]How can I move geom_text labels further out on a ggplot pie chart?

I have some survey data for six groups of respondents (res).我有六组受访者(res)的一些调查数据。 I've added a column for the colour I want to assign each group in my chart, though that's probably redundant.我在图表中为要分配给每个组的颜色添加了一列,尽管这可能是多余的。 I've made a pie chart which shows the number of respondents in each group:我制作了一个饼图,显示了每个组中的受访者人数:

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"))

饼图组

I'm pretty happy with the chart, I just want to move the numbers a bit further away from the centre.我对图表很满意,我只是想将数字移到离中心更远的地方。 At the moment they're about halfway between the centre and the edge, I want them to be about 75% of the way from the centre and 25% of the way from the edge.目前它们大约在中心和边缘之间的中间,我希望它们离中心大约 75%,离边缘大约 25%。

I've tried various configurations of position_stack, position_dodge, nudge_x, nudge_y, can't seem to make it work.我已经尝试了 position_stack、position_dodge、nudge_x、nudge_y 的各种配置,似乎都无法让它工作。 I'm pretty new to R as well...我对 R 也很陌生...

My data looks like this (209 rows in total):我的数据如下所示(共 209 行):

Grp         GrpColour
Group F     #F47920
Group B     #6C217E
Group E     #FFDA00
etc

Help would be much appreciated!帮助将不胜感激!

You do that by changing the x values for geom_text您可以通过更改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"))

Created on 2020-03-24 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2020 年 3 月 24 日创建

看来您只需要增加 0.5 的值。

position = position_stack(vjust = 0.5))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问由 `ggplot2` 中的 `geom_text` 绘制的标签尺寸? - How can I access dimensions of labels plotted by `geom_text` in `ggplot2`? 如何使用 ggplot 在 R 的多面堆叠条形图中使用不同的 geom_text() 标签? - How can I have different geom_text() labels in a faceted, stacked bar graph in R with ggplot? 使用ggplot2和geom_text在比例条形图中插入标签 - insert labels in proportional bar chart with ggplot2 and geom_text 如何在 ggplot 的 geom_text_repel 或 geom_text 标签中包含斜体文本? - How do I include italic text in geom_text_repel or geom_text labels for ggplot? 如何在 ggplot 的 geom_text_repel 或 geom_text 标签中包含删除线文本? - How do I include strikethrough text in geom_text_repel or geom_text labels for ggplot? 如何切割由“geom_curve”绘制的曲线,以便它们不与“ggplot2”中的“geom_text”绘制的标签重叠? - How can I cut curves drawn by `geom_curve` in order they don't overlap labels plotted by `geom_text` in `ggplot2`? ggplot的饼图geom_text()标签隐藏了由geom_bar()制作的饼图 - ggplot's geom_text() labelling of pie chart hides the pie made by geom_bar() 如何防止 geom_text 重叠标签? - How can I prevent geom_text from overlapping labels? 如何将 geom_text 标签移动到 ggplot2 中的 geom_segment 箭头之后? - How do I shift the geom_text labels to AFTER a geom_segment arrow in ggplot2? ggplot:如何将标签添加到多个 plot(带有 geom_text,无图例)? - ggplot : how add labels to multiple plot (with geom_text, no legend)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM