0

when I try this code for barplot (L$neighbourhood is the apartment neighbourhood in Paris for example, Champs-Elysées, Batignolles, which is string data, and L$price is the numeric data for apartment price).

 barplot(L$neighbourhood, L$price, main = "TITLE", xlab = "Neighbourhood", ylab = "Price")

But, I get an error:

Error in barplot.default(L$neighbourhood, L$price, main = "TITLE", xlab = "Neighbourhood", : 'height' must be a vector or a matrix

We cannot use string data as an input in barplot function in R? How can I fix this error please?

dataframe

price

barplotresult

allneighbourhoods

Shuti
  • 169
  • 1
  • 4
  • 18
  • 1
    Try `barplot(price ~ neighbourhood, data=L)`. – jay.sf Jan 09 '20 at 16:21
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Pictures of data are not helpful because we can't copy/paste the data to test it. Are you trying to plot the sum of all the prices? Or the mean of the prices? or something else? – MrFlick Jan 09 '20 at 16:23
  • Be sure to read [docs](https://www.rdocumentation.org/packages/graphics/versions/3.6.2/topics/barplot) with examples. – Parfait Jan 09 '20 at 16:35
  • Hello @jay.sf thank you so much for your comment. You're right, it's difficult to reproduce the issue only with the screenshots. I tried many ways and I found out that I needed to make the input data as matrix cuz they were in data.frame format. I used table() function to transform them into matrix and now it works as no more error :-) – Shuti Jan 09 '20 at 21:46

1 Answers1

1

Quite unclear what you want to barplot. Let's assume you want to see the average price per neighborhood. If that's what you're after you can proceed like this.

First some illustrative data:

set.seed(123)
Neighborhood <- sample(LETTERS[1:4], 10, replace = T)
Price <- sample(10:100, 10, replace = T)
df <- data.frame(Neighborhood, Price)
df
   Neighborhood Price
1             C    23
2             C    34
3             C    99
4             B   100
5             C    78
6             B   100
7             B    66
8             B    18
9             C    81
10            A    35

Now compute the averages by neighborhood using the function aggregate and store the result in a new dataframe:

df_new <- aggregate(x = df$Price, by = list(df$Neighborhood), function(x) mean(x))
df_new
  Group.1  x
1       A 35
2       B 71
3       C 63

And finally you can plot the average prices in variable x and add the neighborhood names from the Group.1column:

barplot(df_new$x, names.arg = df_new$Group.1)

An even simpler solution is this, using tapplyand mean:

df_new <- tapply(df$Price, df$Neighborhood, mean)
barplot(df_new, names.arg = names(df_new))
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
  • this is exactly what I wanted to do – Shuti Jan 09 '20 at 22:01
  • I just have the last question. I think I have quite a lot of data therefore, when I did the last line you suggested me: barplot(tmp_new$x, name.arg = tmp_new$Group.1) I have a warning and I cannot see the name of the nerighbourhood properly on the x-axis. – Shuti Jan 09 '20 at 22:22
  • I have one more question @Chris Ruehlemann I think I have a lot of data, therefore when I tried the last line of code you suggested, I get a warning as per the screenshot below. And, I cannot see the names of neighbourhood properly on the x-axis. Is there a way I can adjust the x-axis with the barplot function? https://i.stack.imgur.com/Nycqn.png – Shuti Jan 09 '20 at 22:26
  • And here are all the neighbourhoods I have before plotting them, sorry I am still new to this post, I don't know how to put images directly in each post https://i.stack.imgur.com/ZqNH9.png – Shuti Jan 09 '20 at 22:32
  • Look at the warning: it says "`name.arg` is not a graphcal parameter"--that's right, because the argument's name is `names.arg`(with an -s)! Also, I strongly recommend that you use the simpler code with `tapply`. – Chris Ruehlemann Jan 10 '20 at 03:41
  • Thank you @Chris Ruehlemann I missed typed the code. Now, it's ok thank you very much – Shuti Jan 10 '20 at 18:50
  • 1
    @Shuti If my post was useful to your query feel free to click the upward arrow – Chris Ruehlemann Jan 11 '20 at 22:52