3

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

我对 R 编程相当陌生。这是针对 IPL 数据集进行的探索性研究。(上面附加数据的链接)在将两个文件与“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 个单独的图形在一个代码中,具有适当的颜色方案。提前谢谢了。

4

2 回答 2

1

这是你想要的吗?

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

在此处输入图像描述

于 2020-04-16T13:55:51.727 回答
1

这是一个mtcars用于将变量拆分为单独图的示例。我创建的是散点图,vs并将mpg数据集拆分为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))
})
于 2020-04-16T14:59:55.357 回答