Adding labels with 3 elements in GGplot coord_polar

Viewed 28

I am building a pie / donut chart with 2 levels and want to label them using the Name, Value and Percentage. For example:

Tiger Block
3596 (20%)

enter image description here

Here is my code so far:


gplot(usage.may, aes(x = Level, y = Percent, fill = Subcategory, label = Label)) +
  geom_bar(stat = "identity", color='white', show.legend = FALSE) +
  geom_text(aes(label = Value),
            size = 3, 
            colour = "white", 
            check_overlap = TRUE, 
            position = position_stack(vjust = 0.5)) +
    coord_polar('y') + theme_minimal()

And some sample data:

structure(list(Level = structure(c(2L, 3L, 3L, 3L, 3L, 2L, 3L, 
2L, 3L, 3L, 3L, 2L, 3L, 3L, 1L), levels = c("0", "1", "2"), class = "factor"), 
    Category = structure(c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 
    4L, 4L, 5L, 5L, 5L, 1L), levels = c("C00", "C01", "C02", 
    "C03", "C04"), class = "factor"), Subcategory = structure(c(2L, 
    3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
    1L), levels = c("C00", "C01", "C011", "C012", "C013", "C014", 
    "C02", "C021", "C03", "C031", "C032", "C033", "C04", "C041", 
    "C042"), class = "factor"), Colour = structure(c(2L, 3L, 
    3L, 3L, 3L, 4L, 5L, 6L, 7L, 7L, 7L, 8L, 9L, 9L, 1L), levels = c("0", 
    "1", "2", "3", "4", "5", "6", "7", "8"), class = "factor"), 
    Label = c("Cafe (R1 & R2)  ", "Non-checked ", "Spider Monkey  ", 
    "Signing-in Cabin", "Solar (cafe)", "Vet Room", "Non-checked", 
    "Butchery", "Non-checked", "Solar (lynx)", "Solar (butchery)", 
    "Tiger Block", "Farm", "Non-checked", ""), Value = c(5323L, 
    921L, 2611L, 34L, 1791L, 534L, 534L, 8479L, 6689L, 1371L, 
    419L, 3596L, 87L, 3247L, 0L), Percent = c(30L, 5L, 15L, 0L, 
    10L, 3L, 3L, 47L, 37L, 8L, 2L, 20L, 2L, 18L, 0L), X = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
    X.1 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA)), row.names = c(NA, -15L), class = "data.frame")

Thanks in advance

1 Answers

With paste0 you could do:

library(ggplot2)

ggplot(df, aes(x = Level, y = Percent, fill = Subcategory)) +
  geom_bar(stat = "identity", color = "white", show.legend = FALSE) +
  geom_text(aes(label = paste0(Label, "\n", Value, " (", Percent, ")")),
    size = 3,
    colour = "white",
    check_overlap = TRUE,
    position = position_stack(vjust = 0.5)
  ) +
  coord_polar("y") +
  theme_minimal()

Related