How to draw lines from labels to circle border in pie chart using ggplot?

I'm trying to get rid of labels overlapping as you see below.
reprex:

library(tidyverse)
library(ggrepel)

tibble(
  n = c(1, 1, 1, 2, 50, 46),
  label = paste0(c(1, 1, 1, 2, 50, 46), "%")) %>% 
  ggplot(aes(x = "", y = n)) +
  geom_bar(width = 1, stat = "identity") +
  geom_text(aes(x = 1.6, label = label),
            position = position_stack(vjust = 0.5)) +
  coord_polar("y")

result (undesired overlaps):

enter image description here

desired output:

enter image description here

enter image description here

I tried ggrepel but result is not desired, no lines from labels to circle border are drawn.

tibble(
  n = c(1, 1, 1, 2, 50, 46),
  label = paste0(c(1, 1, 1, 2, 50, 46), "%")) %>% 
  ggplot(aes(x = "", y = n)) +
  geom_bar(width = 1, stat = "identity") +
  ggrepel::geom_text_repel(aes(x = 1.6, label = label),
            position = position_stack(vjust = 0.5)) +
  coord_polar("y")

result (I don't know how to add lines or arrows).

enter image description here

Could you choose a different type of chart (not a pie chart)?

sorry, I would like to, but I cannot =/.

1 Like

This may get you closer to what you want.

library(ggplot2)
library(tibble)

tibble(
  n = c(1, 1, 1, 2, 50, 46),
  label = paste0(c(1, 1, 1, 2, 50, 46), "%")) |> 
  ggplot(aes(x = "", y = n)) +
  geom_bar(width = 1, stat = "identity",color="white") +
  geom_text(aes(x = 1.6,vjust=rep(c(-0.5,1),3), label = label),size=2,
            position = position_stack(vjust = 0.5)) +
  theme(axis.text.x=element_blank())+
  geom_segment(aes(x=x,xend=xend,y=y,yend=yend),
               data = tibble(x=1.5,xend=c(1.6,1.55,1.6,1.55),
                             y=c(0.5,1.5,2.5,3.7),yend=c(0.5,1.5,2.5,3.7)))+
  coord_polar("y")

Created on 2021-11-18 by the reprex package (v2.0.1)

1 Like

I just edited my reprex in the previous post to show a better plot.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.