gpt4 book ai didi

r - R 中 Barplot 的聚合分组

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

我有一份按年份和职位划分的竞赛获胜者名单,如下所示:

dput(data)
structure(list(Year = c(1930L, 1934L, 1938L, 1950L, 1954L, 1958L,
1962L, 1966L, 1970L, 1974L, 1978L, 1982L, 1986L, 1990L, 1994L,
1998L, 2002L, 2006L, 2010L, 2014L), Country = structure(c(14L,
7L, 5L, 2L, 13L, 12L, 3L, 4L, 9L, 6L, 1L, 11L, 9L, 7L, 15L, 5L,
8L, 6L, 10L, 2L), .Label = c("Argentina", "Brazil", "Chile",
"England", "France", "Germany", "Italy", "Korea/Japan", "Mexico",
"South Africa", "Spain", "Sweden", "Switzerland", "Uruguay",
"USA"), class = "factor"), Winner = structure(c(8L, 6L, 6L, 8L,
5L, 2L, 2L, 3L, 2L, 5L, 1L, 6L, 1L, 5L, 2L, 4L, 2L, 6L, 7L, 5L
), .Label = c("Argentina", "Brazil", "England", "France", "Germany",
"Italy", "Spain", "Uruguay"), class = "factor"), Runners.Up = structure(c(1L,
3L, 6L, 2L, 6L, 9L, 3L, 5L, 7L, 8L, 8L, 5L, 5L, 1L, 7L, 2L, 5L,
4L, 8L, 1L), .Label = c("Argentina", "Brazil", "Czechoslovakia",
"France", "Germany", "Hungary", "Italy", "Netherlands", "Sweden"
), class = "factor"), Third = structure(c(13L, 6L, 2L, 11L, 1L,
5L, 3L, 10L, 6L, 9L, 2L, 9L, 5L, 7L, 11L, 4L, 12L, 6L, 6L, 8L
), .Label = c("Austria", "Brazil", "Chile", "Croatia", "France",
"Germany", "Italy", "Netherlands", "Poland", "Portugal", "Sweden",
"Turkey", "USA"), class = "factor"), Fourth = structure(c(16L,
1L, 14L, 13L, 15L, 7L, 16L, 12L, 15L, 3L, 8L, 6L, 2L, 5L, 4L,
10L, 9L, 11L, 15L, 3L), .Label = c("Austria", "Belgium", "Brazil",
"Bulgaria", "England", "France", "Germany", "Italy", "Korea Republic",
"Netherlands", "Portugal", "Soviet Union", "Spain", "Sweden",
"Uruguay", "Yugoslavia"), class = "factor"), GoalsScored = c(70L,
70L, 84L, 88L, 140L, 126L, 89L, 89L, 95L, 97L, 102L, 146L, 132L,
115L, 141L, 171L, 161L, 147L, 145L, 171L), QualifiedTeams = c(13L,
16L, 15L, 13L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 24L, 24L, 24L,
24L, 32L, 32L, 32L, 32L, 32L), MatchesPlayed = c(18L, 17L, 18L,
22L, 26L, 35L, 32L, 32L, 32L, 38L, 38L, 52L, 52L, 52L, 52L, 64L,
64L, 64L, 64L, 64L), Attendance = c(590549L, 363000L, 375700L,
1045246L, 768607L, 819810L, 893172L, 1563135L, 1603975L, 1865753L,
1545791L, 2109723L, 2394031L, 2516215L, 3587538L, 2785100L, 2705197L,
3359439L, 3178856L, 3386810L)), .Names = c("Year", "Country",
"Winner", "Runners.Up", "Third", "Fourth", "GoalsScored", "QualifiedTeams",
"MatchesPlayed", "Attendance"), class = "data.frame", row.names = c(NA,
-20L))

绘制获胜者图表时,我只使用以下内容:

p1 <- ggplot(data, aes(fct_infreq(Winner),  fill=as.factor(Winner))) +
geom_bar()+
labs(title="Number of World Cup Wins Per Nation",
y="Number of World Cups", x="")+
scale_fill_hue(c=45, l=80)+
theme(legend.position = "none",
axis.text.x = element_text(angle = 75, hjust = 1),
plot.title = element_text(hjust = 0.5))

但是我想不通的是创建相同的条形图,但同时将所有团队添加到“亚军”、“第三”和“第四”列中。

我尝试使用 aggregate 但没有成功。

谢谢

最佳答案

此解决方案仅计算团队进入前 4 名的频率,并制作您之前的条形图。

require(tidyverse)

df %>%
gather(key="variable", value = "value", Winner, Runners.Up, Third, Fourth) -> df_new

df_new %>%
ggplot(aes(fct_infreq(value), fill=as.factor(value))) + geom_bar() +
labs(title="Number of World Cup Wins Per Nation",
y="Number of ---", x="")+
scale_fill_hue(c=45, l=80)+
theme(legend.position = "none",
axis.text.x = element_text(angle = 75, hjust = 1),
plot.title = element_text(hjust = 0.5))

enter image description here您还可以使用 facet_wrap 之类的东西为各个展示位置生成单独的图:

df_new %>%
ggplot(aes(fct_infreq(value), fill=as.factor(value))) + geom_bar() + facet_warp(~variable) +
labs(title="Number of World Cup Wins Per Nation",
y="Number of ---", x="")+
scale_fill_hue(c=45, l=80)+
theme(legend.position = "none",
axis.text.x = element_text(angle = 75, hjust = 1),
plot.title = element_text(hjust = 0.5))

enter image description here

如果您不介意使用图例,我们还可以使用颜色来指示国家/地区的位置:

df_new %>%
ggplot(aes(fct_infreq(value), fill=as.factor(variable))) + geom_bar() +
labs(title="Number of World Cup Wins Per Nation",
y="Number of ---", x="")+
scale_fill_hue(c=45, l=80)+
theme(axis.text.x = element_text(angle = 75, hjust = 1),
plot.title = element_text(hjust = 0.5))

enter image description here

关于r - R 中 Barplot 的聚合分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50804239/

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