GGplot2 - aes(填充)的问题

Posted

技术标签:

【中文标题】GGplot2 - aes(填充)的问题【英文标题】:GGplot2 - problems with aes( fill) 【发布时间】:2021-01-14 22:24:26 【问题描述】:

我正在尝试为即将到来的足球比赛创建一个情节,颜色基于球队在表格中的位置 (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")

【问题讨论】:

不幸的是,这并没有奏效,尽管行是不同的颜色,但各个单元格没有编码它们应该如何编码。尽管它强调了单元格似乎是从左侧标签中的团队而不是单元格中的团队着色的。 也许这意味着我需要更改我的数据 @BulletTooth 是的,但我的答案中显示的只是一行代码 【参考方案1】:

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

【讨论】:

以上是关于GGplot2 - aes(填充)的问题的主要内容,如果未能解决你的问题,请参考以下文章

ggplot2 没有坐标轴、图例等的绘图

ggplot geom_bar() 在绘图上填充不着色条

如何设置 ggplot2 填充颜色以聚合统计信息?

ggplot2:geom_bar 与组,position_dodge 和填充

在barplot ggplot2中个性化每个面上的填充颜色

ggplot2使用初探