Note: I couldn't create a reprex from the code probably because reprex doesn't like this big of a dataset, but i had to have a larger dataset to show a realistic graph. My questions are:
-
Why is 'x' the y axis label and 'Freq' shown as the x axis label. I would have expected Freq to be the y axis label and 'x' as the x axis label.
-
How do i get the percentages to align correctly--so that they show up as one per color block, and not in the figure obtained by running the code i give below where some of the percentages are overlapping two color blocks, two percentages are placed within the same color block, and none of the percentages seem to be placed symmetrically near the center of the color block.
-
I tried inserting position = position_fill(vjust = 0.5) within geom_text and i also tried inserting position = position_stack(vjust = 0.5) within geom_text, but doing so resulted in making the pie chart worse since i started obtaining an incomplete pie chart when using position_stack and a pie chart with the percentages stacked on top of each other when using position_fill.
The code is:
library(dplyr)
library(ggplot2)
color_code <- c("green", "green", "green", "green", "green", "light green", "yellow", "red",
"dark red", "dark red", "red","orange", "light green", "light green", "red","light green",
"light green", "yellow", "red", "red", "yellow", "light green", "light green",
"green",
"light green", "green", "green", "green", "light green", "green",
"light green", "yellow",
"yellow", "yellow", "red", "red", "yellow",
"light green", "yellow", "light green",
"yellow", "light green", "green", "green", "light green", "green",
"green", "orange",
"green", "green", "green", "light green", "yellow", "light green",
"yellow", "dark red",
"light green", "light green", "red", "dark red", "dark red", "green" ,
"green", "light green",
"green", "red", "dark red", "orange", "yellow", "orange",
"red", "green",
"green", "light green", "light green", "green" , "green", "orange" ,
"dark red", "yellow",
"dark red", "dark red", "orange", "yellow", "yellow", "green", "yellow",
"yellow",
"green", "orange", "yellow", "yellow", "green", "orange", "green",
"red",
"green", "orange", "yellow")
percent_table <- table(color_code)
percent_table
percent_table
prop <- round(percent_table/sum(percent_table)*100, 2)
prop
prop <- as.data.frame(prop)
prop
colnames(prop) <- c("color", "prop")
prop
dat <- as.data.frame(percent_table)
dat
colnames(dat) <- c("color", "Freq")
dat
total <- merge(dat, prop, by = "color")
total
total <- total %>% mutate(lab.ypos = cumsum(prop) - 0.5*prop)
total
total$color <- factor(total$color, levels = c("dark red", "red", "orange", "yellow", "light green", "green"))
total <- total[order(total$color), ]
total
total <- total %>% mutate(prop.mod = paste0(prop, "%" ))
total
mycols <- c("darkred", "red", "orange", "yellow", "lightgreen", "green")
ggplot(total, aes(x = "", y = Freq, fill = (color))) + geom_bar(width = 1, stat = "identity") +
coord_polar("y") + scale_fill_manual(values = mycols) +
geom_text(aes(y = lab.ypos, label = prop.mod)) + theme_classic()