GGplot2 - aes(填充)问题

GGplot2 - problems with aes( fill)

我正在尝试为即将到来的足球比赛创建一个图表,颜色基于球队在 table 中的位置 (Pos)。但是,填充似乎没有为各个图块选择正确的颜色,如下所示。我在下面包含了我的数据。任何帮助将不胜感激!

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

数据(输出(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")

您是否正在尝试根据对手的位置来制作填充颜色?如果是这样,您需要在使用 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"))