3

I have a list of competition winners divided by year and position as follows:

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

When graphing winner I simply use the following:

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

However what I can't figure out is to create the same barplot but while adding all teams up in the columns Runner Up, Third & Fourth.

I tried to use aggregate but with no luck.

Thanks

Bake.G
  • 123
  • 1
  • 1
  • 12
  • 1
    `ggplot` expects data in a long format, which will allow you to group your data properly. Can you `dput` a larger sample of the data you're working with? – camille Jun 11 '18 at 18:57
  • By dput do you mean for me to put more sample data in the question? – Bake.G Jun 11 '18 at 19:02
  • 1
    Call the function `dput` on your data frame or a representative sample of it and paste the output. [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R post that's easy for folks to answer – camille Jun 11 '18 at 19:03
  • Thanks, will note for future posts. Updated the question – Bake.G Jun 11 '18 at 19:05

1 Answers1

1

This solution just counts how often a Team placed in the top 4 and makes the barplot you had before.

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 You could also use something like facet_wrap to produce separate plots for the individual placements:

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

If you do not mind using a legend, we could also use colors to indicate the placement of a country:

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

Niko
  • 341
  • 1
  • 8
  • Nice one on facet_wrap, I was using grid.arrange and doing the code out for each graph instead. – Bake.G Jun 11 '18 at 19:40
  • Oh right sorry...I just started to load tidyverse on default when running R so I always forget to mention it :) – Niko Jun 11 '18 at 19:42
  • Sorry, I used df instead of data, all sorted now, thanks for the awesome input – Bake.G Jun 11 '18 at 19:43