Question about creating a barplot with means

Hi! I'm fairly new to R and thought this task would be simple but I'm hitting a bit of a block...
I am using a dataset on Airbnb listings in Sydney and am trying to create a barplot with the average price by neighbourhood. Here is the data:

SydneyAirbnbListings = read.csv("http://data.insideairbnb.com/australia/nsw/sydney/2019-07-10/visualisations/listings.csv", header=T)
names(SydneyAirbnbListings)
Neighbourhood = SydneyAirbnbListings$neighbourhood
Price = SydneyAirbnbListings$price
Type = SydneyAirbnbListings$room_type

The only way I can think of to do it is to break it down like this (repeating this for all neighbourhoods) and then creating the barplot by hand but I assume there has to be a much easier way to do it?:

AshfieldPrices = Price[Neighbourhood == "Ashfield"]
AuburnPrices = Price[Neighbourhood == "Auburn"]
BankstownPrices = Price[Neighbourhood == "Bankstown"]
BlacktownPrices = Price[Neighbourhood == "Blacktown"]
BotanyBayPrices = Price[Neighbourhood == "Botany Bay"]
BurwoodPrices = Price[Neighbourhood == "Burwood"]

Thanks so much in advance!!

Are you looking for something like this?

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
SydneyAirbnbListings = read.csv("http://data.insideairbnb.com/australia/nsw/sydney/2019-07-10/visualisations/listings.csv", 
                                header=T)
ByNeighborhood <- SydneyAirbnbListings %>% group_by(neighbourhood) %>% 
  summarize(Avg = mean(price))
print(ByNeighborhood)
#> # A tibble: 38 x 2
#>    neighbourhood   Avg
#>    <fct>         <dbl>
#>  1 Ashfield      138. 
#>  2 Auburn        145. 
#>  3 Bankstown      99.4
#>  4 Blacktown      84.2
#>  5 Botany Bay    118. 
#>  6 Burwood        92.5
#>  7 Camden        121. 
#>  8 Campbelltown  140. 
#>  9 Canada Bay    149. 
#> 10 Canterbury    101. 
#> # ... with 28 more rows

ggplot(ByNeighborhood, aes(x = neighbourhood, y = Avg)) +
  geom_col(color = "white", fill = "skyblue") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

Created on 2019-09-07 by the reprex package (v0.2.1)

2 Likes

Thanks so much! This is super helpful.

Is there anyway to do the plot with the barplot() function? or is the ggplot2 package really the only way to do it?

Thanks again!

Something like this?

SydneyAirbnbListings <- read.csv(file = "http://data.insideairbnb.com/australia/nsw/sydney/2019-07-10/visualisations/listings.csv",
                                 header = TRUE)

with(data = SydneyAirbnbListings,
     expr =
       {
         barplot(height = tapply(X = price,
                                 INDEX = list(neighbourhood),
                                 FUN = mean),
                 cex.names = 0.625,
                 las = 2)
       })

Created on 2019-09-08 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.