更改圆环图中标签的位置

Bru*_*ita 8 r ggplot2 ggrepel

我试图将圆环图中标签的位置移动到图表的右侧,但我无法做到。我正在使用 ggplot 和 ggrepel 来制作图表。

library(ggplot2)
library(ggrepel)

expenditurePie = data.frame(
  value = c(98,2),
  area = c("A","B"),
  label = c("","This is a label"))

ggplot(expenditurePie, aes(y=value, fill = area, label = label)) +
  geom_bar(aes(x = 4), stat = "identity", show.legend = F) +
  geom_text_repel(size = 5, x= 4, point.padding = unit(1.8, "lines"), direction = "x") +
  xlim(0.5, 4.5) +
  annotate(geom = "text", x=0.5, y=0, label = "24 444", size = 16, color = "grey") +
  scale_fill_manual(values = c(A = "grey", B = "black")) +
  coord_polar(theta = "y", start = 1) +
  theme_void() +
  theme(legend.position = 'none')
Run Code Online (Sandbox Code Playgroud)

下图是上面代码的结果: 在此处输入图片说明

但下图显示了我需要的东西: 在此处输入图片说明

我该怎么做?谢谢。

ste*_*fan 1

也许这是您的一个选择。不是依赖ggrepel我的方法的魔力,而是手动设置标签(仍然使用ggrepel,但力设置为 0),并使用 绘制线段geom_segement。尝试这个:

library(ggplot2)
library(ggrepel)

expenditurePie = data.frame(
  value = c(98,2),
  area = c("A","B"),
  label = c("","This is a label"))

ggplot(expenditurePie, aes(y = value, fill = area, label = label)) +
  geom_bar(aes(x = 4), stat = "identity", show.legend = F) +
  geom_text_repel(size = 5, x = 7, point.padding = unit(1.8, "lines"), direction = "x", force = 0, seed = 42) +
  geom_segment(aes(x = 4.6, xend = 5.5, y = 1, yend = 1)) +
  xlim(0.5, 5.5) +
  annotate(geom = "text", x=0.5, y=0, label = "24 444", size = 20, color = "grey") +
  scale_fill_manual(values = c(A = "grey", B = "black")) +
  coord_polar(theta = "y", start = 1) +
  theme_void() +
  theme(legend.position = 'none')
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020-05-25 创建