1

I am trying to use barplot() in R studio to plot a graph to represent the average apartment price per neighbourhood (from airbnb database downloaded from internet). Thank you to another collaborator Chris Ruehlemann who suggested me below sample code. But, when I tried to run them on the large data, I cannot see properly the name of the neighbourhood on the x-axis. Does anyone has a suggestion to adjust the x-axis better so that we can see the names properly?

[barplotnoname][1]

[https://i.stack.imgur.com/PezKd.png]

Here are the code:

tmp <- data.frame(L$neighbourhood, L$price)
tmp
tmp_new <- aggregate(x = tmp$L.price, by = list(tmp$L.neighbourhood), function(x) mean(x))
tmp_new
barplot(tmp_new$x, name.arg = names(tmp_new$Group.1), main = "Avg Apt Price per Neighbourhood", xlab = "Neighbourhood", ylab = "Price")

***Before all this, need to make sure to download airbnb database from (https://plmbox.math.cnrs.fr/f/65765b530bbb4ed8b489/?dl=1). Then, load it to R studio using command: load('AirBnB.Rdata')

Please, see my previous post on this question: Using barplot in R studio

Shuti
  • 169
  • 1
  • 4
  • 18

1 Answers1

1

How about this?

p <- L %>% 
  transmute(neighbourhood, 
            price = as.numeric(gsub("[^0-9.]", "", price))) %>% 
  group_by(neighbourhood) %>% 
  summarize(price = mean(price)) %>% 
  ggplot(aes(y = price, x = neighbourhood)) + 
  geom_col() + 
  theme(axis.text.x = element_text(size = 8, angle = 90))
p 

enter image description here

Your original data variable "L$price" is factor variable with dollar (%) signs. I converted it to numeric before plotting. I used ggplot simply because I am a little familiar with it.

library(tidyverse)
p <- L %>% 
  transmute(neighbourhood, 
         price = as.numeric(gsub("[^0-9.]", "", price))) %>% 
  group_by(neighbourhood) %>% 
  summarize(price = mean(price)) %>% 
  ggplot(aes(x = price, y = neighbourhood)) + 
  geom_col()
p 

enter image description here

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
  • Hello @Zhiqiang Wang, thank you so much. I did already transform price data by removing $ sign but I was really struggling on showing the labels properly on x-axis. Your code really helped me. – Shuti Jan 10 '20 at 18:29