gpt4 book ai didi

r - 如何对数据进行分类并绘制图形

转载 作者:行者123 更新时间:2023-12-04 10:12:07 24 4
gpt4 key购买 nike

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

这是对两个 IPL 数据集执行的探索性数据分析。我希望从比赛数据集中建立球队赢得的 throw 与比赛获胜者之间的关系。当我使用 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 ?")

输出如下:-

Is winning the toss an advantage ?

如何将数据分成赢家和输家两列并得到两个条形图?
提前谢谢了。

最佳答案

要根据 throw 计算赢得的比赛数量,您可以执行以下操作:

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 ?")

enter image description here

您可以更进一步,对进行比赛的顶级城市进行相同的分析。
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")

enter image description here

关于r - 如何对数据进行分类并绘制图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61284828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com