https://www.kaggle.com/nowke9/ipldata ----包含数据集。
这是对两个IPL数据集执行的探索性数据分析。我正在寻找根据比赛数据集在团队赢得比赛和比赛胜利者之间建立联系的关系。当我使用ifelse条件将数据分为赢家和输家并绘制图表时,我得到的输出为单个条形图,其中仅包含比赛总数,而所有这些都是丢失的图例的一部分。
这是代码-
library(tidyverse)
deliveries_tbl <- read.csv("data/deliveries_updated.csv")
matches_tbl <- read.csv("data/matches_updated.csv")
matches_normal_result_tbl <- matches_tbl[matches_tbl$result == "normal",]
# Is winning toss really an adnavtage ? ----
matches_normal_result_tbl$toss_match <- ifelse(as.character(matches_normal_result_tbl$toss_winner)==
as.character(matches_normal_result_tbl$winner),
"Won", "Lost")
ggplot(matches_normal_result_tbl[which(!is.na(matches_normal_result_tbl$toss_match)),], aes(toss_match, fill = toss_match))+
geom_bar()+
xlab("Toss")+ ylab("Number of matches won")+
ggtitle("How much of advantage is winning the toss ?")
输出如下:
如何将数据分为赢家和输家两列并获得两个条形图?提前谢谢了。
要计算基于抛球赢得的比赛次数,您可以执行以下操作:
library(dplyr)
library(ggplot2)
matches %>%
mutate(toss_match = ifelse(toss_winner == winner, "Won", "Loss")) %>%
count(toss_match) %>%
ggplot() + aes(toss_match, n, fill = toss_match) +
geom_col() +
xlab("Toss")+ ylab("Number of matches won")+
ggtitle("How much of advantage is winning the toss ?")
您可以走得更远,对进行比赛的热门城市进行相同的分析。
matches %>%
mutate(toss_match = ifelse(toss_winner == winner, "Won", "Loss")) %>%
count(city, toss_match) %>%
group_by(city) %>%
filter(all(n > 10)) %>%
mutate(n = n/sum(n) * 100) %>%
ggplot() + aes(city, n, fill = toss_match) +
geom_col() +
xlab("City")+ ylab("Percentage") +
ggtitle("Advantage of winning toss in each city")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句