I can only find a solution for this in relation to single-level pie charts. I have the chart below and some of the labels on the outer ring don't fit well.
I'd like to keep the labels for the inner ring where they are but move the labels for the second ring to the outside (or at least the ones that don't fit).
Here is my code
ggplot(usage.may, aes(x = Level, y = Percent, fill = Subcategory, label = Label))
geom_bar(stat = "identity", color='white', show.legend = FALSE)
geom_text(aes(label = paste0(Label, "\n", Value, " (", Per_label, ")")),
size = 2.5,
colour = "white",
check_overlap = TRUE,
position = position_stack(vjust = 0.5))
coord_polar('y')
scale_fill_manual(values = c("C01" = "#404688FF",
"C011" = "#3B528BFF","C012" = "#3B528BFF","C013" = "#3B528BFF","C014" = "#3B528BFF",
"C02" = "#287C8EFF",
"C021" = "#287C8EFF",
"C03" = "#27AD81FF",
"C031" = "#35B779FF","C032" = "#35B779FF","C033" = "#35B779FF",
"C04" = "#8FD744FF",
"C041" = "#8FD744FF","C042" = "#8FD744FF"))
labs(title = "Electricity Usage May 2022") ylab("") xlab("")
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.background = element_blank(),
strip.background = element_blank(),
axis.text= element_blank(),
axis.ticks= element_blank())
And 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), Per_label = c("30%",
"5%", "15%", "0%", "10%", "3%", "3%", "47%", "37%", "8%",
"2%", "20%", "2%", "18%", "0%")), row.names = c(NA, -15L), class = "data.frame")
Thanks in advance
CodePudding user response:
One option would be to use an ifelse
to shift the x
position of the labels for the outer ring. Additionally I use an ifelse
scale_color_identity
to conditionally set the font color of the labels:
library(ggplot2)
ggplot(usage.may, aes(x = Level, y = Percent, fill = Subcategory, label = Label))
geom_bar(stat = "identity", color='white', show.legend = FALSE)
geom_text(aes(label = paste0(Label, "\n", Value, " (", Per_label, ")"),
x = as.numeric(Level) ifelse(Level == 2, 1, 0),
color = ifelse(Level == 2, "black", "white")),
size = 2.5,
check_overlap = TRUE,
position = position_stack(vjust = 0.5))
coord_polar('y')
scale_fill_manual(values = c("C01" = "#404688FF",
"C011" = "#3B528BFF","C012" = "#3B528BFF","C013" = "#3B528BFF","C014" = "#3B528BFF",
"C02" = "#287C8EFF",
"C021" = "#287C8EFF",
"C03" = "#27AD81FF",
"C031" = "#35B779FF","C032" = "#35B779FF","C033" = "#35B779FF",
"C04" = "#8FD744FF",
"C041" = "#8FD744FF","C042" = "#8FD744FF"))
scale_color_identity()
labs(title = "Electricity Usage May 2022") ylab("") xlab("")
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.background = element_blank(),
strip.background = element_blank(),
axis.text= element_blank(),
axis.ticks= element_blank())