在 R 中创建此分组条形图时我的错误是什么?我该如何解决?

What is my mistake when creating this grouped barplot in R and how do i resolve this?

这是我的数据框数据

dput(data4)

structure(list(`number of goals scored` = c(311, 271), 
               `number of fouls commited` = c(2147, 2246)), 
               row.names = c("Hometeam", "AwayTeam"), 
               class = "data.frame")

这是我的条形图代码和我在创建分组条形图时遇到的错误

barplot(data4, 
        main = 'total num of goals and fouls commited',
        xlab = 'Teams',ylab = 'frequency',
        col = c('red','yellow'),
        legend.text = rownames(data4),
        beside = TRUE)

Error in barplot.default(data4, main = "total num of goals and fouls commited", : 'height' must be a vector or a matrix

看看这是不是你想要的:

barplot(as.matrix(data4[,1:2]), 
    main = 'total num of goals and fouls commited',
    xlab = 'Teams',ylab = 'frequency',
    col = c('red','yellow'),
    legend.text = rownames(data4),
    beside = TRUE)

barplot 在第一个参数中要求一个矩阵。所以给它一个矩阵而不是 data.frame.