位置= barplot中的geom_col中的道奇

发布于 2025-02-13 04:33:17 字数 2954 浏览 1 评论 0原文

这是一个足球运动员的数据集,我需要可视化在一个酒吧情节中每个国家 /地区玩游戏数量旁边收到的黄牌总数。因此,我需要计算黄牌的总数和每个联盟国家的游戏总数,并将数据纳入长期格式。

    dput(head(new_soccer_referee))
structure(list(playerShort = c("lucas-wilchez", "john-utaka", 
"abdon-prats", "pablo-mari", "ruben-pena", "aaron-hughes"), player = c("Lucas Wilchez", 
"John Utaka", " Abdón Prats", " Pablo Marí", " Rubén Peña", "Aaron Hughes"
), club = c("Real Zaragoza", "Montpellier HSC", "RCD Mallorca", 
"RCD Mallorca", "Real Valladolid", "Fulham FC"), leagueCountry = c("Spain", 
"France", "Spain", "Spain", "Spain", "England"), birthday = structure(c(4990, 
4390, 8386, 8643, 7868, 3598), class = "Date"), height = c(177L, 
179L, 181L, 191L, 172L, 182L), weight = c(72L, 82L, 79L, 87L, 
70L, 71L), position = c("Attacking Midfielder", "Right Winger", 
NA, "Center Back", "Right Midfielder", "Center Back"), games = c(1L, 
1L, 1L, 1L, 1L, 1L), victories = c(0L, 0L, 0L, 1L, 1L, 0L), ties = c(0L, 
0L, 1L, 0L, 0L, 0L), defeats = c(1L, 1L, 0L, 0L, 0L, 1L), goals = c(0L, 
0L, 0L, 0L, 0L, 0L), yellowCards = c(0L, 1L, 1L, 0L, 0L, 0L), 
    yellowReds = c(0L, 0L, 0L, 0L, 0L, 0L), redCards = c(0L, 
    0L, 0L, 0L, 0L, 0L), photoID = c("95212.jpg", "1663.jpg", 
    NA, NA, NA, "3868.jpg"), rater1 = c(0.25, 0.75, NA, NA, NA, 
    0.25), rater2 = c(0.5, 0.75, NA, NA, NA, 0), refNum = c(1L, 
    2L, 3L, 3L, 3L, 4L), refCountry = c(1L, 2L, 3L, 3L, 3L, 4L
    ), Alpha_3 = c("GRC", "ZMB", "ESP", "ESP", "ESP", "LUX"), 
    meanIAT = c(0.326391469021736, 0.203374724564378, 0.369893594187172, 
    0.369893594187172, 0.369893594187172, 0.325185154120009), 
    nIAT = c(712L, 40L, 1785L, 1785L, 1785L, 127L), seIAT = c(0.000564112354334542, 
    0.0108748941063986, 0.000229489640866464, 0.000229489640866464, 
    0.000229489640866464, 0.00329680952361961), meanExp = c(0.396, 
    -0.204081632653061, 0.588297311544544, 0.588297311544544, 
    0.588297311544544, 0.538461538461538), nExp = c(750L, 49L, 
    1897L, 1897L, 1897L, 130L), seExp = c(0.0026964901062936, 
    0.0615044043187379, 0.00100164730649311, 0.00100164730649311, 
    0.00100164730649311, 0.013752210497518), BMI = c(22.98190175237, 
    25.5922099809619, 24.1140380330271, 23.8480304816206, 23.6614386154678, 
    21.4346093466973), position_new = c("Offense", "Offense", 
    "Goalkeeper", "Defense", "Midfield", "Defense"), rater_mean = c(0.375, 
    0.75, NA, NA, NA, 0.125), ageinyear = c(28, 30, 19, 18, 20, 
    32), ageinyears = c(28, 30, 19, 18, 20, 32)), row.names = c(NA, 
6L), class = "data.frame")

使用数据绘制具有以下特征的条形图:

- X轴显示联盟国家 /地区,而Y轴则显示游戏的数量和纸牌数

- 对于每个国家 /地区,彼此相邻两个酒吧:一款玩游戏的一个,一个用于收到的卡

barplot< - ggplot(new_soccer_referee,aes(x = leaguecountry,y = number))

barplot + 

geom_bar(fill=c("games","yellowCards")) +

geom_col(Position="dodge") +
  labels(x="leagueCountry", y="number")

ggplot

`

我知道这很混乱,但是我真的很困惑如何用GGPLOT建立层以及如何制定长时间的格式,谁能提供帮助?

here is a dataset of soccer players that I need to visualise the total number of yellow cards received next to the number of games played per country in one bar plot. SO I need to calculate the total number of yellow cards and the total number of games per league country and bring the data into long format.

    dput(head(new_soccer_referee))
structure(list(playerShort = c("lucas-wilchez", "john-utaka", 
"abdon-prats", "pablo-mari", "ruben-pena", "aaron-hughes"), player = c("Lucas Wilchez", 
"John Utaka", " Abdón Prats", " Pablo Marí", " Rubén Peña", "Aaron Hughes"
), club = c("Real Zaragoza", "Montpellier HSC", "RCD Mallorca", 
"RCD Mallorca", "Real Valladolid", "Fulham FC"), leagueCountry = c("Spain", 
"France", "Spain", "Spain", "Spain", "England"), birthday = structure(c(4990, 
4390, 8386, 8643, 7868, 3598), class = "Date"), height = c(177L, 
179L, 181L, 191L, 172L, 182L), weight = c(72L, 82L, 79L, 87L, 
70L, 71L), position = c("Attacking Midfielder", "Right Winger", 
NA, "Center Back", "Right Midfielder", "Center Back"), games = c(1L, 
1L, 1L, 1L, 1L, 1L), victories = c(0L, 0L, 0L, 1L, 1L, 0L), ties = c(0L, 
0L, 1L, 0L, 0L, 0L), defeats = c(1L, 1L, 0L, 0L, 0L, 1L), goals = c(0L, 
0L, 0L, 0L, 0L, 0L), yellowCards = c(0L, 1L, 1L, 0L, 0L, 0L), 
    yellowReds = c(0L, 0L, 0L, 0L, 0L, 0L), redCards = c(0L, 
    0L, 0L, 0L, 0L, 0L), photoID = c("95212.jpg", "1663.jpg", 
    NA, NA, NA, "3868.jpg"), rater1 = c(0.25, 0.75, NA, NA, NA, 
    0.25), rater2 = c(0.5, 0.75, NA, NA, NA, 0), refNum = c(1L, 
    2L, 3L, 3L, 3L, 4L), refCountry = c(1L, 2L, 3L, 3L, 3L, 4L
    ), Alpha_3 = c("GRC", "ZMB", "ESP", "ESP", "ESP", "LUX"), 
    meanIAT = c(0.326391469021736, 0.203374724564378, 0.369893594187172, 
    0.369893594187172, 0.369893594187172, 0.325185154120009), 
    nIAT = c(712L, 40L, 1785L, 1785L, 1785L, 127L), seIAT = c(0.000564112354334542, 
    0.0108748941063986, 0.000229489640866464, 0.000229489640866464, 
    0.000229489640866464, 0.00329680952361961), meanExp = c(0.396, 
    -0.204081632653061, 0.588297311544544, 0.588297311544544, 
    0.588297311544544, 0.538461538461538), nExp = c(750L, 49L, 
    1897L, 1897L, 1897L, 130L), seExp = c(0.0026964901062936, 
    0.0615044043187379, 0.00100164730649311, 0.00100164730649311, 
    0.00100164730649311, 0.013752210497518), BMI = c(22.98190175237, 
    25.5922099809619, 24.1140380330271, 23.8480304816206, 23.6614386154678, 
    21.4346093466973), position_new = c("Offense", "Offense", 
    "Goalkeeper", "Defense", "Midfield", "Defense"), rater_mean = c(0.375, 
    0.75, NA, NA, NA, 0.125), ageinyear = c(28, 30, 19, 18, 20, 
    32), ageinyears = c(28, 30, 19, 18, 20, 32)), row.names = c(NA, 
6L), class = "data.frame")

Use the data to draw a bar plot with the following characteristics:

– The x-axis displays the league country while the y-axis displays the number of games and the number of cards

– For each country there are two bars next to each other: one for the games played and one for the cards received

barplot <- ggplot(new_soccer_referee,aes(x=leagueCountry,y=number))

barplot + 

geom_bar(fill=c("games","yellowCards")) +

geom_col(Position="dodge") +
  labels(x="leagueCountry", y="number")

ggplot

`

I know it is pretty messy but I am really confused how to build up the layers with ggplot and how to work out the long format, can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云裳 2025-02-20 04:33:17

一个选项是首先汇总数据以计算YellowcardsGAMES by leagueCountry的数量。之后,您可以将长时间转换为长时间,从而可以轻松地通过ggplot2绘制。

使用一些虚假的随机示例数据模仿您的真实数据:

set.seed(123)

new_soccer_referee <- data.frame(
  player = sample(letters, 20),
  leagueCountry = sample(c("Spain", "France", "England", "Italy"), 20, replace = TRUE),
  yellowCards = sample(1:5, 20, replace = TRUE),
  games = sample(1:20, 20, replace = TRUE)
)

library(dplyr)
library(tidyr)
library(ggplot2)

new_soccer_referee_long <- new_soccer_referee %>% 
  group_by(leagueCountry) %>%
  summarise(across(c(yellowCards, games), sum)) %>%
  pivot_longer(-leagueCountry, names_to = "variable", values_to = "number")

ggplot(new_soccer_referee_long, aes(leagueCountry, number, fill = variable)) +
  geom_col(position = "dodge")

“”

One option would be to first aggregate your data to compute the number of yellowCards and games by leagueCountry. Afterwards you could convert to long which makes it easy to plot via ggplot2.

Using some fake random example data to mimic your real data:

set.seed(123)

new_soccer_referee <- data.frame(
  player = sample(letters, 20),
  leagueCountry = sample(c("Spain", "France", "England", "Italy"), 20, replace = TRUE),
  yellowCards = sample(1:5, 20, replace = TRUE),
  games = sample(1:20, 20, replace = TRUE)
)

library(dplyr)
library(tidyr)
library(ggplot2)

new_soccer_referee_long <- new_soccer_referee %>% 
  group_by(leagueCountry) %>%
  summarise(across(c(yellowCards, games), sum)) %>%
  pivot_longer(-leagueCountry, names_to = "variable", values_to = "number")

ggplot(new_soccer_referee_long, aes(leagueCountry, number, fill = variable)) +
  geom_col(position = "dodge")

花开半夏魅人心 2025-02-20 04:33:17

像这样:

library(tidyverse)

new_soccer_referee %>% 
  select(leagueCountry, games, yellowCards) %>% 
  group_by(leagueCountry) %>% 
  summarise(games = sum(games),
            yellowCars = sum(yellowCards)
            ) %>% 
  pivot_longer(-leagueCountry) %>% 
  ggplot(aes(x=leagueCountry, fill=name, y=value)) +
  geom_col(position = position_dodge())

“在此处输入图像描述”

Something like this:

library(tidyverse)

new_soccer_referee %>% 
  select(leagueCountry, games, yellowCards) %>% 
  group_by(leagueCountry) %>% 
  summarise(games = sum(games),
            yellowCars = sum(yellowCards)
            ) %>% 
  pivot_longer(-leagueCountry) %>% 
  ggplot(aes(x=leagueCountry, fill=name, y=value)) +
  geom_col(position = position_dodge())

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文