1

This is my dataframe in R,

New York  8755
     Texas  7654
California  6726
   Florida  6322

I simply want the graph to compare the numbers in a bar stacked side by side.I have not been able to achieve that. I am getting the error "Height must be a vector of Matrix." Please suggest! Thanks a lot!

4
  • 1
    What is the command you are using to plot it? You probably want something like this - barplot(x$V2,names.arg=x$V1) where x is your data, and v1 and v2 are the names of the city and data columns. Commented Jun 26, 2015 at 18:09
  • I was using barplot(us.june) Error in barplot.default(us.june) : 'height' must be a vector or a matrix > barplot(us.june) Commented Jun 26, 2015 at 18:19
  • 1
    so, barplot(us.june[,2],names.arg=us.june[,1]). You need to tell R which column is the data, and which is the names. The error you got is R telling you it needed only numbers to plot, but you gave it something else. Commented Jun 26, 2015 at 18:22
  • Thanks a lot.. I was working on it for quite a while! I will keep this in mind now. Commented Jun 26, 2015 at 18:26

1 Answer 1

7

R cannot tell which variable is which when you pass it an entire data.frame to plot. In this case, the error, 'height' must be a vector or a matrix is telling you you didn't give the plot function what it wanted.

For a barplot, using ?barplot will tell you what is needed:

barplot(height, width = 1, space = NULL, names.arg = NULL ....

In your case, you have a data.frame with heights in the second column, and names in the first, so you want to do:

barplot(data[ ,2], names.arg = data[ ,1])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.