R:热图错误(数据):&x27;x';必须是数字矩阵。其中';x';它指的是什么?

R:热图错误(数据):&x27;x';必须是数字矩阵。其中';x';它指的是什么?,r,R,我正在尝试在R中创建热图。这是我的数据集: Team, Att_L, Att_M, Att_R, Aston Villa, 0.37, 0.24, 0.39 Birmingham City, 0.34, 0.26, 0.4 Blackburn Rovers, 0.38, 0.26, 0.36 Bolton Wanderers, 0.32, 0.27, 0.41 Brentford, 0.34, 0.28, 0.38 Bristol City, 0.37, 0.26, 0.37 Derby Cou

我正在尝试在R中创建热图。这是我的数据集:

Team, Att_L, Att_M, Att_R,
Aston Villa, 0.37, 0.24, 0.39
Birmingham City, 0.34, 0.26, 0.4
Blackburn Rovers, 0.38, 0.26, 0.36
Bolton Wanderers, 0.32, 0.27, 0.41
Brentford, 0.34, 0.28, 0.38
Bristol City, 0.37, 0.26, 0.37
Derby County, 0.34, 0.26, 0.4
Hull City, 0.38, 0.21, 0.41
Ipswich Town, 0.33, 0.24, 0.43
Leeds United, 0.37, 0.24, 0.39
Middlesbrough, 0.37, 0.25, 0.38
Millwall, 0.3, 0.24, 0.46
Norwich City, 0.36, 0.24, 0.4
Nottingham Forest, 0.37, 0.22, 0.41
Preston North End, 0.35, 0.26, 0.39
Queens Park Rangers, 0.35, 0.24, 0.41
Reading, 0.37, 0.23, 0.4
Rotherham United, 0.38, 0.27, 0.35
Sheffield United, 0.41, 0.21, 0.38
Sheffield Wednesday, 0.37, 0.29, 0.34
Stoke City, 0.36, 0.25, 0.39
Swansea City, 0.38, 0.24, 0.38
West Bromwich Albion, 0.38, 0.25, 0.37
Wigan Athletic, 0.38, 0.24, 0.38
它以一个称为俯仰轴的TIBLE开始。我将其转换为一个表,并使用
gather()
为热图准备数据

 data2 <- as.data.table(gather(Pitch_axis, variable, value, Att_L:Att_R))
这将返回错误:

>Error in heatmap(data2) : 'x' must be a numeric matrix
我不能从两个方面理解这一点:1。我不明白哪个变量是'x'2。我试着转换成一个矩阵,所有的观察结果都以双“,”的形式出现,大概是字符串


有没有办法解决这个问题?

试试这个?独家代表

library(tidyverse)
df<-as.tibble(df)
df<-df %>% 
  gather("id","value",2:ncol(.))

df2<-matrix(df$value,4,8)


 heatmap(df2,labRow=df$Team,labCol = df$id)
库(tidyverse)

df我最终得到了这个

        ggplot(data2, aes(Team, variable)) +
  geom_tile(aes(fill = value), color = "white") +
  scale_fill_gradient(low = "yellow", high = "red") +
  ylab("") +
  xlab("") +
  theme(legend.title = element_text(size = 10),
        legend.text = element_text(size = 12),
        plot.title = element_text(size=16),
        axis.title=element_text(size=14,face="bold"),
        axis.text.x = element_text(angle = 90, hjust = 1)) +
  ggtitle("Attack more down left, right or centre?",
          subtitle = "") +
  labs(fill = "%age") +
coord_flip()

有关错误消息,请参见热图,并查看函数的第一个参数。对于第二个问题:矩阵只能包含单一类型的变量。尝试
矩阵(c(1,2,“c”),1)
您将看到前两个元素将被强制为字符,就像您的情况一样(因为团队列)。
        ggplot(data2, aes(Team, variable)) +
  geom_tile(aes(fill = value), color = "white") +
  scale_fill_gradient(low = "yellow", high = "red") +
  ylab("") +
  xlab("") +
  theme(legend.title = element_text(size = 10),
        legend.text = element_text(size = 12),
        plot.title = element_text(size=16),
        axis.title=element_text(size=14,face="bold"),
        axis.text.x = element_text(angle = 90, hjust = 1)) +
  ggtitle("Attack more down left, right or centre?",
          subtitle = "") +
  labs(fill = "%age") +
coord_flip()