Pie chart issue involving geom_text()

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:

  1. 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.

  2. 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.

  3. 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()

Hi @blue.eyes, could you paste your data here, like this?

```
 mycols <- c("darkred", "red", "orange", "yellow", "lightgreen", "green")
<--- paste the output of dput(total) here
```

That will make it easier for folks to help.

You should narrow down your code to just the relevant part, with all that extra code your issue looks harder than it actually is. I think this is what you are trying to accomplish

library(ggplot2)

total <- data.frame(
  stringsAsFactors = FALSE,
              Freq = c(9L, 11L, 9L, 20L, 21L, 29L),
              prop = c(9.09, 11.11, 9.09, 20.2, 21.21, 29.29),
          lab.ypos = c(4.545, 74.235, 64.135, 89.89, 48.985, 23.735),
          prop.mod = c("9.09%", "11.11%", "9.09%", "20.2%", "21.21%", "29.29%"),
             color = as.factor(c("dark red",
                                 "red","orange","yellow","light green",
                                 "green"))
)

mycols <- c("darkred", "red", "orange", "yellow", "lightgreen", "green")

ggplot(total, aes(x = "", y = prop / 100, fill = color)) +
    geom_col(width = 1) +
    coord_polar("y", start = 0) +
    geom_text(aes(x = 1, label = prop.mod),
              position = position_stack(vjust = 0.5)) +
    scale_fill_manual(values = mycols) + 
    theme_minimal() +
    theme(axis.text.x=element_blank(),
          axis.title.x = element_blank(),
          axis.title.y = element_blank())

Created on 2020-03-15 by the reprex package (v0.3.0.9001)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.