位置= barplot中的geom_col中的道奇
这是一个足球运动员的数据集,我需要可视化在一个酒吧情节中每个国家 /地区玩游戏数量旁边收到的黄牌总数。因此,我需要计算黄牌的总数和每个联盟国家的游戏总数,并将数据纳入长期格式。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个选项是首先汇总数据以计算
Yellowcards
和GAMES
byleagueCountry
的数量。之后,您可以将长时间转换为长时间,从而可以轻松地通过ggplot2
绘制。使用一些虚假的随机示例数据模仿您的真实数据:
One option would be to first aggregate your data to compute the number of
yellowCards
andgames
byleagueCountry
. Afterwards you could convert to long which makes it easy to plot viaggplot2
.Using some fake random example data to mimic your real data:
像这样:
Something like this: