计算XY极坐标以将对象放置在值标签正下方。

5

考虑以下数据集:

ig_5 <- data.frame(
  category = c("A", "B", "C", "D", "E", "F"),
  prop = c(0.1, 0.2, 0.15, 0.25, 0.05, 0.25)
) %>%
  mutate(lab.ypos = cumsum(prop) - 0.5*prop)

我使用以下代码创建了一个甜甜圈图表:
ggplot(ig_5, aes(x = 2, y = prop, fill = prop)) +
  geom_bar(stat = "identity", color = "white") +
  coord_polar(theta = "y", start = 0) +
  geom_text(aes(y = lab.ypos, label = prop), color = "black", size = 5) +
  xlim(.5, 2.5)

如您所见,值标签放置在每个类别的中间。现在,我需要将图形放置在每个值标签的正下方。我不能只是将新对象粘贴到值上,因为文本和图形的颜色不同。因此,我想象应该有一种方法可以使用值标签的坐标(x = 2,y = lab.ypos = cumsum(prop)-0.5 * prop)来定位所需的图形。

我使用geom_label添加了新图形,并给它一个与值标签的XY坐标成比例的位置。然而,我还没有成功。这是我尝试过的一个示例。

ig_5 <- data.frame(
  category = c("A", "B", "C", "D", "E", "F"),
  prop = c(0.1, 0.2, 0.15, 0.25, 0.05, 0.25)) %>%
  mutate(lab.ypos = cumsum(prop) - 0.5*prop, 
         lab.ypos2 = cumsum(prop) + 0.3*prop)

 
ggplot(ig_5, aes(x = 2, y = prop, fill = prop)) +
  geom_bar(stat = "identity", color = "white") +
  coord_polar(theta = "y", start = 0) +
  geom_text(aes(y = lab.ypos, label = prop), color = "black", size = 5) +
  geom_label(aes(y = lab.ypos,
                 x = c(1.85, 2.1, 2.2, 1.95, 1.8, 1.85),
                 label = "figure"), 
             color = 'red') +
  xlim(.5, 2.5) 

而我得到的结果是:

Result


1
多好且可重现的第一个问题!欢迎来到本站! - Gregor Thomas
3个回答

4

我有点偏见,但我认为geomtextpath在这里很有效:

library(geomtextpath)

ggplot(ig_5, aes(x = 2, y = prop, fill = prop)) +
  geom_bar(stat = "identity", color = "white", linewidth = 1.5) +
  coord_polar(theta = "y", start = 0) +
  geom_textpath(aes(y = lab.ypos, label = prop, group = category), 
                color = "black", size = 8, angle = 90, vjust = 1) +
  geom_labelpath(aes(y = lab.ypos, label = "figure", group = category), 
                color = "red4", size = 6, angle = 90, vjust = -1.5) +
  scale_fill_gradientn(colours = c("lightyellow", "gold", "orange")) +
  xlim(.5, 2.5) +
  theme_void(base_size = 20)

enter image description here


3
我们可以滥用vjust来垂直地调整标签,超出正常的0到1范围。这将在视觉上垂直移动标签,而不考虑坐标系。适用的数量可能取决于输出的尺寸 - 只需尝试值,直到它起作用 :)
ggplot(ig_5, aes(x = 2, y = prop, fill = prop)) +
  geom_bar(stat = "identity", color = "white") +
  coord_polar(theta = "y", start = 0) +
  geom_text(aes(y = lab.ypos, label = prop), color = "black", size = 5) +
  geom_label(aes(y = lab.ypos,
                 label = "figure"), 
             vjust = 1.5,
             color = 'red') +
  xlim(.5, 2.5)

enter image description here


3
另一个选择是使用ggtext :: geom_richtext,通过label.margin参数允许“移动”标签半个值标签的大小,结合使用vjust = 1应该确保标签放置在值标签正下方。
library(ggplot2)

ggplot(ig_5, aes(x = 2, y = prop, fill = prop)) +
  geom_bar(stat = "identity", color = "white") +
  coord_polar(theta = "y", start = 0) +
  geom_text(aes(y = lab.ypos, label = prop),
    color = "black", size = 5
  ) +
  ggtext::geom_richtext(
    aes(
      y = lab.ypos,
      label = "figure"
    ),
    color = "red",
    label.margin = unit(2.5, "mm"),
    vjust = 1
  ) +
  xlim(.5, 2.5)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接