geom_tile和重复数据的问题维护顺序

时间:2018-11-26 13:20:18

标签: r ggplot2

我正在尝试不松开数据框的顺序,信息在数据框中具有顺序。 但是由于数据已复制到数据帧中,所以我获得了错误消息或引入了NA。 我没有找到类似的解决方案,我尝试过使用此How to preserve the order of tiles in geom_tile ggplot,但没有成功

library(readr)
library(ggplot2)
library(RColorBrewer)

url_soccer <- 'https://raw.githubusercontent.com/frm1789/soccer_ea/master/tableau.m.csv'

tableau.m <- read_csv(url_soccer)

tableau.m <- tableau.m[,-1]

(p <- ggplot(tableau.m, aes(Team, variable)) + 
geom_tile(aes(fill = rescale), colour = "white") + 
scale_fill_gradient(low = "white", high = "steelblue"))

预期的解决方案是这样的: How the visualization should be...

1 个答案:

答案 0 :(得分:1)

重新排列变量,请参阅:

tableau.m$Team <- factor(tableau.m$Team, c("Brasil", "Argentina", "Uruguay"))
tableau.m$variable <- factor(tableau.m$variable, c("Titles", "Match", "Points", "Points_1", "Performance"))

ggplot(tableau.m, aes(variable, Team, fill = rescale)) + 
  geom_tile(show.legend = FALSE) + 
  scale_fill_gradient(low = "white", high = "steelblue") +
  theme_minimal()

enter image description here