Home > Software design >  Labels Are Out Of Place When Creating Pie Chart Using R
Labels Are Out Of Place When Creating Pie Chart Using R

Time:07-13

I am having a hard time figuring out why my labels are out of place. See image here

My Script:

ggplot(data = percentage2, aes(x = "", y = minutes, fill = lbls))  
  geom_col(width = 1, color = 1)  
  geom_label_repel(aes(x = 1.5, 
                       label = paste0(round(minutes, 2), "%")), 
                   size = 4.5, nudge_x = 0.5, show.legend = FALSE)  
  coord_polar(theta = "y")  
  theme_void()  
  labs(title = "Percentage of Active Minutes", y = "Activity")  
  scale_fill_discrete(name = "Activity Level", 
                      breaks = c("Sedentary", "Lightly Active", "Fairly Active", "Very Active"))

lbls
Very Active
Fairly Active
Lightly Active
Sedentary

minutes
1.736602
1.113014
15.820493
81.329891

CodePudding user response:

The problem is that each label has been placed at the end of each section instead to the midpoint. You could by forward calculate the positions in a different dataframe and use these for your geom_lable_repel. You can use the following code:

library(ggplot2)
library(ggrepel)
library(dplyr)
library(forcats)

df = data.frame(lbls = c("Very Active", "Fairly Active", "Lightly Active", "Sedentary"),
                minutes = c(1.636, 1.113, 16.82, 81.33))

df2 <- df %>% 
  mutate(
    cs = rev(cumsum(rev(minutes))), 
    pos = minutes/2   lead(cs, 1),
    pos = if_else(is.na(pos), minutes/2, pos))

ggplot(df, aes(x = "" , y = minutes, fill = fct_inorder(lbls)))  
  geom_col(width = 1)  
  coord_polar(theta = "y", start = 0 )  
  scale_fill_discrete(name = "Activity Level", breaks=c("Sedentary", "Lightly Active", "Fairly Active", "Very Active"))  
  geom_label_repel(aes(y = pos, label = paste0(minutes, "%")), data = df2, size=4, show.legend = F, nudge_x = 1)  
  theme_void()

Created on 2022-07-13 by the reprex package (v2.0.1)

CodePudding user response:

The issue is that geom_col uses position="stack" while geom_label_repel uses position = "identity". To fix that use position = position_stack() for the labels too. Note however that you can't have nudging with position_stack(). Instead I went for using vjust=.5 to place the labels in the center of the bars and set direction="y". You could also adjust the x to shift the labels to the outside.

percentage2 <- data.frame(
  minutes = c(1.736602, 1.113014, 15.820493, 81.329891),
  lbls = c("Very Active", "Fairly Active", "Lightly Active", "Sedentary")
)

library(ggplot2)
library(ggrepel)

ggplot(data = percentage2, aes(x = "", y = minutes, fill = lbls))  
  geom_col(width = 1, color = 1)  
  geom_label_repel(aes(x = 1.5, label = paste0(round(minutes, 2), "%")), 
                   size = 4.5, 
             show.legend = FALSE, position = position_stack(vjust = .5), direction = "y")  
  coord_polar(theta = "y")  
  theme_void()  
  labs(title = "Percentage of Active Minutes", y = "Activity")  
  scale_fill_discrete(name = "Activity Level", 
                      breaks = c("Sedentary", "Lightly Active", "Fairly Active", "Very Active"))

  • Related