如何跨不同年份使用for循环并将多个地块放在一起?

How to use for loop across various years and get multiple plots together?

https://www.kaggle.com/nowke9/ipldata----包含数据集。

我对 R 编程还很陌生。这是对 IPL 数据集进行的探索性研究。 (link 用于上面附加的数据)在将两个文件与 "id" 和 "match_id" 合并后,我试图绘制不同城市的团队赢得的比赛之间的关系。

但是,由于 12 个赛季已经结束,我得到的输出无助于做出充分的结论。为了绘制每年的关系,需要使用 for 循环。现在,所有 12 年的输出都显示在一个图表中。

如何纠正这个错误并使用适当的配色方案为每一年绘制单独的图表?

library(tidyverse)
matches_tbl <- read_csv("data/matches_updated.csv")
deliveries_tbl <- read_csv("data/deliveries_updated.csv")

combined_matches_deliveries_tbl <- deliveries_tbl %>%
    left_join(matches_tbl, by = c("match_id" = "id"))

combined_matches_deliveries_tbl %>%
    group_by(city, winner)%>%
    filter(season == 2008:2019, !result == "no result")%>%
    count(match_id)%>%
    ungroup()%>%
    ggplot(aes(x = winner))+
    geom_bar(aes(fill = city),alpha = 0.5, color = "black", position = "stack")+
    coord_flip()+
    theme_bw() 

输出结果如下:-

  There were 50 or more warnings (use warnings() to see the first 50)
  [Winner of teams across cities for the years between 2008 and 2019][1]

所需的输出是:- 一个代码中的 12 个单独的图形,具有适当的配色方案。 提前谢谢了。

这是你想要的吗?

combined_matches_deliveries_tbl %>%
  group_by(city, winner,season)%>%
  filter(season %in% 2008:2019, !result == "no result")%>%
  count(match_id)%>%
  ggplot(aes(x = winner))+
  geom_bar(aes(fill = city),alpha = 0.5, color = "black", position = "stack")+
  coord_flip()+ facet_wrap(season~.)+
  theme_bw() 

这是一个使用 mtcars 将变量拆分为单独的图的示例。我创建的是 vsmpg 的散点图,方法是将数据集拆分为 cyl。首先创建一个空列表。然后我使用 lapply 循环遍历 cyl (4,6,8) 的值,然后按该值过滤数据。之后,我绘制子集的散点图并将其保存到空列表中。列表的每个部分都代表一个图,您可以根据需要将它们拉出。

library(dplyr)
library(ggplot2)
gglist <- list()

gglist <- lapply(c(4,6,8), function(x){

ggplot(filter(mtcars, cyl == x))+
    geom_point(aes(x=vs,y=mpg))
})