R中Barplot的汇总分组

时间:2018-06-11 18:47:45

标签: r ggplot2 bar-chart

我有一份比赛获胜者名单除以年份和职位如下:

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

然而,我无法弄清楚的是创建相同的条形图但是在Runner Up,Third&amp; amp;第四。

我尝试使用aggregate,但没有运气。

由于

1 个答案:

答案 0 :(得分:1)

此解决方案只计算一个团队排在前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