Search code examples
rggplot2pie-chart

ggplot2 pie chart, numbers don't appear


i'm quite new in data visualisation and in R with ggplot2. I'm trying to visualize some data in a pie chart. The code i've used is this:

percentageData <- data.frame(Year = "1987", 
                             TypeOfDelays = c(percDepDelays[1], percArrDelays[1], percAntDepdelays[1], percAntArrDelays[1]),
                             Label = factor(c("Departure delays", "Arrival Delays", "Early Departure", "Early arrival")))

labels = c("Departure delays", "Arrival Delays", "Early Departure", "Early arrival")
ggplot(data = percentageData) +
  geom_bar(aes(x="", y=TypeOfDelays, fill=Label), stat = "identity", width = 1) + 
  coord_polar(theta = "y", start = 0) +
  theme_void() +
  geom_text(aes(x = 1, y=cumsum(TypeOfDelays) - TypeOfDelays/2, label=labels))

The dataset that i'm analysing is this: flights dataset

My problem is to visualise for each year the number of delays for each type of delay (i'm considering 4 type of delays, departure, arrival, departure before the time and arrival before the time).

My idea is to create a data frame for each year containing the Year, percentage of number of delays for each type (total #delays in a year/#flights in that year) and Label that describe the type of delay. I've already calculated the percentage for each the type of delays. I want to visualise these data in a pie chart, I tryed to create a pie chart with the code above and the result is this: enter image description here

So my problem are these:

1) Why percentage data are not visualised? What is the problem in my code?

2) How to properly visualise the label in the right position?

Thanks in advance.


Solution

  • To get what you want, you can adapt your code on the following points:

    • Move the aes to the ggplot() part
    • Remove the the x and the y specification from the aes in geom_text
    • Add position = position_stack(vjust = 0.5) to geom_text

    The final code:

    ggplot(data = percentageData, aes(x="", y = TypeOfDelays, fill = Label)) +
      geom_bar(stat = "identity", width = 1) + 
      geom_text(aes(label = labels), position = position_stack(vjust = 0.5)) +
      coord_polar(theta = "y", start = 0) +
      theme_void()
    

    which gives:

    enter image description here

    The numbers didn't appear because you didn't ask ggplot2 to include them. A possibility:

    ggplot(data = percentageData, aes(x="", y = TypeOfDelays, fill = Label)) +
      geom_bar(stat = "identity", width = 1) + 
      geom_text(aes(label = paste0(labels, ': ', TypeOfDelays,' %')), position = position_stack(vjust = 0.5)) +
      coord_polar(theta = "y", start = 0) +
      theme_void()
    

    which gives:

    enter image description here


    Used data:

    percentageData <- data.frame(Year = "1987", 
                                 TypeOfDelays = c(30, 45, 5, 20),
                                 Label = factor(c("Departure delays", "Arrival Delays", "Early Departure", "Early arrival")))
    labels <- c("Departure delays", "Arrival Delays", "Early Departure", "Early arrival")