R ggplot2 while循环中的多个图

时间:2018-07-02 08:38:43

标签: r ggplot2 charts

嗨, 我是R和ggplot2的新手。我正在分析1987年至2008年美国航班的数据集:dataset

这只是介绍。我的问题是,我想显示多年来的延迟分布,但是年是22,我不认为要写22倍以下代码行:

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

我正在使用上面的代码以饼图的百分比形式显示每年每一次延迟的次数,显然是每年的数据变化,这就是我需要循环的原因,我我尝试过的是这样的:

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
}

在实践中,我将所有ggplot图放到一个数组中,然后我将使用ggarrange将所有ggplot图放到一个屏幕中。问题在于向量p中存储了用于制作图表的data.frame结构(我想记住数据每年都在变化,这是我需要循环的原因),这真的很奇怪,因为在循环之外(第一个代码)一切正常,p是ggplot类。那么,我该如何解决这个问题呢?有没有一种方法可以合并大量绘图而无需编写成千上万条相同的代码行?

感谢帮助

1 个答案:

答案 0 :(得分:1)

我建议使用列表结构。

p <- list()

,然后在分配给列表时使用第二个索引

p[[i]]