GGplot2 - problems with aes( fill)

691 Views Asked by At

I am trying to create a plot for upcoming football fixtures, with the colours based on the team's position (Pos) in the table. However, the fill doesn't seem to be choosing the right colour for the individual tiles as shown below. I have included my data below. Any help would be greatly appreciated!

    df %>%
ggplot() +
      geom_tile(aes(x=GW,y=team,fill=Pos), colour="black") +
      geom_text(aes(x=GW,y=team,label=oppo),size=3) +
      theme_void() +
      theme(axis.text = element_text(face = "bold")) +
      theme(axis.text.y = element_text(margin=margin(0,-20,0,0))) + 
      scale_x_continuous(position="top",breaks=1:15) +
      labs(caption=paste("xxx rocks | ",Sys.Date(),sep=""))

Plot

Data (dput(df)):

structure(list(team = c("CHE", "EVE", "LEE", "NEW", "LEI", "SOU", "ARS", "WOL", "MUN", "AVL", "CHE", "CRY", "EVE", "LEE", "LEI", "MCI", "NEW", "SHU", "TOT", "WBA", "CRY", "BHA", "MCI", "BUR", "WHU", "WBA", "SHU", "FUL", "TOT", "LIV", "SOU", "BHA", "LIV", "WOL", "AVL", "ARS", "MUN", "FUL", "WHU", "BUR"), fdr = c(1070L, 1070L, 1350L, 1150L, 1100L, 1030L, 1220L, 1030L, 1260L, 1350L, 1060L, 1070L, 1350L, 1190L, 1080L, 1270L, 1250L, 1030L, 1100L, 1150L, 1200L, 1100L, 1030L, 1090L, 1180L, 1150L, 1190L, 1200L, 
1250L, 1050L, 1200L, 1080L, 1100L, 1030L, 1180L, 1310L, 1090L, 1170L, 1200L, 1020L), Pos = c(9, 3, 7, 10, 1, 15, 5, 16, 14, 4, 9, 6, 3, 7, 1, 13, 10, 19, 8, 17, 6, 12, 13, 18, 11, 17, 19, 20, 8, 2, 15, 12, 2, 16, 4, 5, 14, 20, 11, 18), GW = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), home = c("CHE", "EVE", "LEE", "NEW", "LEI", "SOU", "ARS", "WOL", "MUN", "AVL", "CHE", "CRY", "EVE", "LEE", "LEI", "MCI", "NEW", "SHU", "TOT", "WBA", "CHE", "EVE", "LEE", "NEW", "LEI", "SOU", "ARS", "WOL", "MUN", "AVL", "CHE", 
"CRY", "EVE", "LEE", "LEI", "MCI", "NEW", "SHU", "TOT", "WBA"), away = c("CRY", "BHA", "MCI", "BUR", "WHU", "WBA", "SHU", "FUL", "TOT", "LIV", "SOU", "BHA", "LIV", "WOL", "AVL", "ARS", "MUN", "FUL", "WHU", "BUR", "CRY", "BHA", "MCI", "BUR", "WHU", "WBA", "SHU", "FUL", "TOT", "LIV", "SOU", "BHA", "LIV", "WOL", 
"AVL", "ARS", "MUN", "FUL", "WHU", "BUR"), oppo = structure(c(6L, 3L, 12L, 4L, 19L, 18L, 15L, 8L, 17L, 11L, 16L, 3L, 11L, 20L, 2L, 1L, 13L, 8L, 19L, 4L, 5L, 7L, 9L, 14L, 10L, 16L, 1L, 20L, 13L, 2L, 5L, 6L, 7L, 9L, 10L, 12L, 14L, 15L, 17L, 18L), .Label = c("ARS", "AVL", "BHA", "BUR", "CHE", "CRY", "EVE", "FUL", "LEE", "LEI", "LIV", "MCI", "MUN", "NEW", "SHU", "SOU", "TOT", "WBA", "WHU", "WOL"), class = "factor"), G_A = c(6, 3, 7, 4, 4, 6, 4, 7, 5, 0, 6, 3, 3, 7, 4, 6, 4, 4, 4, 11, 3, 6, 6, 5, 4, 11, 4, 10, 4, 
4, 6, 6, 4, 7, 0, 4, 5, 10, 4, 5), G_F = c(6, 8, 8, 3, 12, 3, 6, 3, 4, 4, 6, 5, 8, 8, 12, 5, 3, 0, 6, 5, 5, 6, 5, 2, 5, 5, 0, 3, 6, 9, 3, 6, 9, 3, 4, 6, 4, 3, 5, 2), PTS = c(4, 9, 6, 4, 9, 3, 6, 3, 3, 6, 4, 6, 9, 6, 9, 3, 4, 0, 4, 1, 6, 3, 3, 0, 3, 1, 0, 0, 4, 9, 3, 3, 9, 3, 6, 6, 3, 0, 3, 0)), row.names = c(NA, 
-40L), class = "data.frame")
1

There are 1 best solutions below

0
On BEST ANSWER

Are you perhaps trying to make the fill color according to the opponents' position? If so, you will need to calculate this for each row before plotting using match:

library(ggplot2)
library(dplyr)

df %>%
  mutate(oppo_pos = Pos[match(oppo, team)]) %>%
  ggplot() +
  geom_tile(aes(x = GW, y = team, fill = oppo_pos), colour = "black") +
  geom_text(aes(x = GW, y = team, label = oppo), size = 3) +
  theme_void() +
  theme(axis.text = element_text(face = "bold")) +
  theme(axis.text.y = element_text(margin=margin(0, -20, 0, 0))) + 
  scale_x_continuous(position = "top",breaks = 1:15) +
  labs(caption = paste("xxx rocks | ", Sys.Date(), sep = "")) +
  scale_fill_gradientn(colors = c("forestgreen", "gold", "tomato"))

enter image description here