1

Been trying to make a pie chart with ggrepel labels. The chart looks fine itself but the label positions are way off from what they should be.


lightingdf <- structure(
 list(
 Light = c(
 "Daylight",
 "Dark",
 "Dark, artificial",
 "Dusk",
 "Dawn",
 "Dusk, artificial",
 "Daylight, artificial",
 "Dawn, artificial"
 ),
 count = c(1291L, 209L, 186L, 71L, 29L, 18L, 14L, 13L),
 percentage = c(
 70.5079191698525,
 11.4145275805571,
 10.1583833970508,
 3.87766247951939,
 1.58383397050792,
 0.983069361004915,
 0.764609503003823,
 0.70999453850355
 )
 ),
 row.names = c(NA, -8L),
 class = c("tbl_df", "tbl", "data.frame")
)
lightingdf <- lightingdf %>% mutate(Light = str_replace_all(Light, " ",""))
light_levels <- c("Daylight","Daylight,artificial",
 "Dark","Dark,artificial",
 "Dusk,artificial","Dusk",
 "Dawn,artificial","Dawn")
lightingdf <- lightingdf %>% mutate(Light = factor(Light,levels = light_levels))
baseplot <- lightingdf %>% ggplot(aes(x = "", y = percentage, fill = Light)) + 
 geom_bar(stat = 'identity', color = 'black',width = .5) + coord_polar("y", start = pi/2)
color_palette <- c(
 "Daylight" = "#27B7CE", 
 "Daylight,artificial" = "#93DBE7", 
 "Dawn" = "#FB8048",
 "Dawn,artificial" = "#FCA67F",
 "Dusk" = "#FFAA00",
 "Dusk,artificial" = "#B37700",
 "Dark,artificial" = "#47B972",
 "Dark" = "#FB6476"
)
themedChart <- baseplot + 
 labs(caption = "Collisions by light type - 2020 to 2024") +
 theme_void() + 
 scale_fill_manual(values = color_palette) +
 theme(plot.caption = element_text(hjust=0.5,vjust=10,size=14,face="bold"),
 plot.margin = unit(c(1.2,1,1.2,1),"cm"),
 panel.background = element_rect(fill = "white"),
 legend.position = "none")
positionDF <- lightingdf %>% 
 mutate(
 csum = rev(cumsum(rev(percentage))), 
 pos = percentage/2 + lead(csum, 1), 
 pos = if_else(is.na(pos), percentage/2, pos) 
 ) 
themedChart + 
geom_text_repel(data = positionDF,
 aes(y = pos,
 label = paste(Light,round(percentage,1),"%",sep=" ")),
 size = 3,
 color = "black",
 fontface="bold.italic",
 nudge_x = 0.75,
 show.legend = FALSE)

I'm getting this as output

Script PieChart

But the 'leader lines' from ggrepel don't match up with my factor. Ex: The line for "Dawn" is pointing to the section for "Dusk", and "Daylight,artificial" is on the right of the plot while the wedge that corresponds to it is the light blue one.

The output i'd like to get would look something like the following: Desired PieChart

I'm fine with some of the smaller labels (<1%) not being placed, but I want this order.

I tried rearranging light by factor and recalculating pos, but got the same output. I tried manually adjust the pos positions, but I doubt that's the 'right' approach to take.

asked Nov 2, 2024 at 6:12

1 Answer 1

1

The issue is your computation of the positions for the labels, which can be easily seen when plotting in cartesian coordinates.

After fixing the computation of the positions for the labels your code works fine.

library(ggplot2)
library(ggrepel)
library(dplyr, warn = FALSE)
positionDF <- lightingdf |>
 arrange(desc(Light)) |>
 mutate(
 csum = cumsum(percentage),
 pos = (csum + lag(csum, default = 0)) / 2
 )
baseplot <- lightingdf |>
 ggplot(aes(x = "", y = percentage, fill = Light)) +
 geom_col(color = "black", width = .5) +
 geom_text_repel(
 data = positionDF,
 aes(
 x = 1.2,
 y = pos,
 label = paste(
 Light,
 scales::number(percentage, accuracy = .1, suffix = " %")
 )
 ),
 size = 3,
 color = "black",
 fontface = "bold.italic",
 nudge_x = 0.3,
 show.legend = FALSE
 )
themedChart <- baseplot +
 scale_fill_manual(values = color_palette) +
 labs(caption = "Collisions by light type - 2020 to 2024") +
 theme_void() +
 theme(
 plot.caption = element_text(
 hjust = 0.5, vjust = 10,
 size = 14, face = "bold"
 ),
 plot.margin = unit(c(1.2, 1, 1.2, 1), "cm"),
 panel.background = element_rect(fill = "white"),
 legend.position = "none"
 )
themedChart +
 coord_polar("y", start = pi / 2)

enter image description here

answered Nov 2, 2024 at 6:26
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.