ggplot:在内部添加文本并在外部添加标签?

ggplot: adding text to inside and label outside?

你好,我想创建一个饼图,在饼图内部有总频率,但在外面我想添加标签?

例如:

temp = structure(list(Var1 = c("diet", "water", "pepsi", "coke", "7_up", 
"sprite", "none"), Freq = c(351L, 212L, 71L, 444L, 524L, 193L, 
53L), per = c(0.189935064935065, 0.114718614718615, 0.0384199134199134, 
0.24025974025974, 0.283549783549784, 0.104437229437229, 0.0286796536796537
)), row.names = c(1L, 3L, 5L, 7L, 9L, 11L, 13L), class = "data.frame")

在这里我可以创建情节,但标签都乱七八糟。

ggplot(data = temp,
       aes(x = "", y = per, fill = Var1)) +
  geom_col() +
  geom_text(aes(label = Freq  ),
            position = position_stack(vjust = 0.5),
            color = "grey20", size = 12, fontface = "bold"  ) +
  coord_polar(theta = "y", direction = -1 ) +

  theme_void() +
  theme(legend.position = "none",
        legend.direction = "vertical")+  

  theme ( legend.text      =element_text(size=12),
          legend.title = element_text(size=12) ) +
  geom_label_repel(
    aes(
      label = Var1, y=per
    ),  box.padding = 0.25
  )

图表看起来像这样。

您可以将 x = 1 用于条形,然后将 x = 1.5 或其他一些值用于标签,而不是放置 x = ""。基本上尝试用条形图外面的标签制作条形图,然后如果你转向极坐标,你应该得到想要的输出

ggplot(data = temp,
       aes(x = 1, y = per, fill = Var1)) +
    geom_col() +
    geom_text(aes(label = Freq),
              position = position_stack(vjust = 0.5),
              color = "grey20", fontface = "bold"  ) +
    geom_label(aes(x = 1.5, label = Var1), 
               position = position_stack(vjust = 0.5)) + 
    coord_polar(theta = "y", direction = -1 ) +
    theme_void() +
    theme(legend.position = "none",
          legend.direction = "vertical")+  
    theme (legend.text = element_text(size=12),
            legend.title = element_text(size=12) )