Home > Software engineering >  Incorrect label position using ggpubr (happens in ggplot as well)
Incorrect label position using ggpubr (happens in ggplot as well)

Time:08-19

I slightly modified the donut chart example in the ggpubr package and it moves the label to the wrong group when I add the label. In my example below, 'A' is the smallest group, but it gets the label 'D' instead. As I recall, this happens in ggplot as well. I will add an example of that as well. Anyone know how to fix this?

enter image description here

library(ggpubr)
df <- data.frame(group = c("A", "B", "C","D"),
  value = c(1, 21, 26, 52))
ggdonutchart(df, "value", label = "group")
 ggdonutchart(df, "value", label = "group",
      fill = "group", color = "white")
# Show group names and value as labels
labs <- paste0(df$group, " (", df$value, "%)")
ggdonutchart(df, "value", label = labs,
   fill = "group", color = "white")

CodePudding user response:

Pass labs with quotes "labs":

library(ggpubr)
library(dplyr)

df %>% 
  mutate(labs = paste0(group, " (", value, "%)")) %>% 
  ggdonutchart(., "value", label = "labs",
             fill = "group", color = "white")

enter image description here

  • Related