Reputation: 2531
I'm just trying to create a basic data.frame as below:
data <- data.frame(Names = c('A', 'B', 'C', 'D', 'E'), Marks = c(65, 78, 55, 66, 93))
And wanted to draw a simple bar graph out of it:
barplot(data)
But I keep getting the below error message:
Error in barplot.default(data) : 'height' must be a vector or a matrix
I tried looking for answers and also tried: barplot(as.matrix(data))
but that gives a weird vertical graph.
What am I failing to see here?
Upvotes: 1
Views: 55
Reputation: 35314
The first argument height
must contain only the data, that is, only the heights of the bars. To specify labels for the bars, you must pass the labels to the names.arg
argument. (Alternatively, you could attach names to the height
argument, but I think names.arg
makes more sense in this case.) See barplot()
.
barplot(data$Marks,names.arg=data$Names);
barplot(setNames(data$Marks,data$Names)); ## alternative
Upvotes: 1