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

Time:01-13

This is my dataframe data

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

This is the my barplot code and the error i get when creating the grouped barplot

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

CodePudding user response:

See if this is what you wanted:

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 is asking for a matrix in the 1st argument. So give it a matrix intead of a data.frame.

  •  Tags:  
  • Related