1

Hy guys, I'm quite new in R and ggplot2. I'm making analysis on a dataset of american flights from 1987 to 2008: dataset

This is just to introduce. My problem is this, i want to show the delays distribution along the years, but years are 22 and I don't think to write 22 times these code lines:

percDepDelays<- depDelaysByYear$`count(DepDelay)` / allFlights$`count(*)`
percArrDelays <- arrDelaysByYear$`count(ArrDelay)` / allFlights$`count(*)`
percAntDepdelays <- antDepDelaysByYear$`count(DepDelay)` / allFlights$`count(*)`
percAntArrDelays <- antArrDelaysByYear$`count(ArrDelay)` / allFlights$`count(*)`

slices <- c(percDepDelays[1], percArrDelays[1], percAntDepdelays[1], percAntArrDelays[1])
pct <- round(slices/sum(slices) * 100, 2)

percentageData <- data.frame(Year = "1987",
                             Percentage = pct,
                             TypeOfDelays = factor(c("Departure delays", "Arrival Delays", "Early Departure", "Early arrival")))
labels <- c("Departure delays", "Arrival Delays", "Early Departure", "Early arrival")

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

I'm using code above to show the, as a percentage in a pie chart, the number of each delays for each year, obviously for each year data changes and this is the reason why i need a loop, what I've tried is this:

p <- vector()
while (i < length(percDepDelays)) {

  slices <- c(percDepDelays[i], percArrDelays[i], percAntDepdelays[i], percAntArrDelays[i])
  pct <- round(slices/sum(slices) * 100)
  percentageData <- data.frame(Year = yearsLabels[i],
                             Percentage = pct,
                             TypeOfDelays = factor(c("Departure delays", "Arrival Delays", "Early Departure", "Early arrival")))
  labels <- c("Departure delays", "Arrival Delays", "Early Departure", "Early arrival")
  p[i] <- ggplot(data = percentageData, aes(x="", y = Percentage, fill = TypeOfDelays)) +
            geom_bar(stat = "identity", width = 1) +
            geom_text(aes(label = paste0(Percentage,' %')), position = position_stack(vjust = 0.5)) +
            coord_polar(theta = "y", start = 0) +
            theme_void()
  i = i + 1
}

In practice, I put all ggplot graphs in an array and then I would have used ggarrange to put all together in one screen. The problem is that in the vector p are store data.frame structure used to make the graph (I want remember that data changes for each year, its the reason why i need a loop), its really strange, because outside the loop (the first code) all works fine, p is a ggplot class. So, how can i solve this problem? There is a way to combine a great number of plot without writing thousands and thousands of same code lines?

Thanks for help

chaw359
  • 505
  • 1
  • 5
  • 14

1 Answers1

1

I'd recommend using a list structure.

p <- list()

and then when assigning to the list use a second index

p[[i]]
  • Ok, your solution works, in fact if within list I have ggplot objects. But now I've tried to show in a While loop with this line of code `ggarrange(p[[i]], ncol=2, nrow=length(p)` but nothing happens... How can I show all plots within the list? – chaw359 Jul 04 '18 at 16:46