Error generating bar graph: 'height' must be a vector or a Matrix

Asked

Viewed 107 times

0

Hello! When building a bar chart on R Language using the RStudio I came across the following mistake:

Error in barplot.default(database$results, main = "EvasionChart", xlab = rotulo[1],  : 
'height' must be a vector or a matrix

I researched a little about, I saw answers of various types but that did not solve my problem, to help in the context I am sharing the script. Below is the code:

database <- data.frame("curso" = 1:10, "e_eolica" = 1:10)
database$curso <- c("ItemA", "ItemB", "ItemC", "ItemD", "ItemE", "ItemF", "ItemG", "ItemH", "ItemI", "sdois")
database$results <- c(1, 2, 3, 4, 5, 6, 7, 8)
rotulo <-  c(1, 2)

par(mgp=c(1,1,0))
png(filename = "C:/Users/BREWERTONTHIAGOOLIVE/Desktop/chart.png", width = 800, height = 800)
barplot(database$results, main = "EvasionChart", xlab = rotulo[1], ylab = rotulo[2], names.arg = database$curso, ylim = c(0, database$results[8]), cex.names = 0.8, xaxs = "i")
grid(nx=NA, ny=NULL)
barplot(database$results, main = "EvasionChart", xlab = rotulo[1], ylab = rotulo[2], names.arg = database$curso, ylim = c(0, database$results[8]), cex.names = 0.8, xaxs = "i", add = TRUE)

dev.off()

1 answer

0


Analyzing the line below we will see that the error code is pointing out that the xlab and ylab that are being populated with the data contained in database$curso and database$results has no equivalent value and must be filled. That is, the vector contains more elements than values:

barplot(database$results, main = "EvasionChart", xlab = rotulo[1], ylab = rotulo[2], names.arg = database$curso, ylim = c(0, database$results[8]), cex.names = 0.8, xaxs = "i")

The mistake of course the database$curso points to 10 elements in the vector and the database$results fills the vector with 8 values, leaving 2 undefined, everything can be solved by adding 2 values in the database$results as in the example below:

Before:

database$results <- c(1, 2, 3, 4, 5, 6, 7, 8)

Afterward:

database$results <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.