Shuti
Shuti

Reputation: 319

Large data on x-axis on ggplot() in R studio

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.sstatic.net/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

Upvotes: 1

Views: 1139

Answers (1)

Zhiqiang Wang
Zhiqiang Wang

Reputation: 6759

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

Upvotes: 1

Related Questions