LearneR
LearneR

Reputation: 2531

How to create a simple basic bar graph with a basic dataframe?

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

Answers (1)

bgoldst
bgoldst

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

plot

Upvotes: 1

Related Questions