###R spatial II 第二次來玩玩空間繪圖,使用airbnb的資料,想看看能不能畫出一張能夠表現出
房型(room_type), 跟價格

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.1
## -- Attaching packages ------------------------------------------ tidyverse 1.2.1 --
## √ ggplot2 3.2.1     √ purrr   0.3.3
## √ tibble  2.1.3     √ dplyr   0.8.3
## √ tidyr   1.0.0     √ stringr 1.4.0
## √ readr   1.3.1     √ forcats 0.4.0
## Warning: package 'ggplot2' was built under R version 3.6.1
## Warning: package 'tibble' was built under R version 3.6.2
## Warning: package 'tidyr' was built under R version 3.6.1
## Warning: package 'purrr' was built under R version 3.6.2
## Warning: package 'dplyr' was built under R version 3.6.2
## -- Conflicts --------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.6.2
library("leaflet.providers")
## Warning: package 'leaflet.providers' was built under R version 3.6.2
airbnb <- read_csv("AB_NYC_2019.csv")
## Parsed with column specification:
## cols(
##   id = col_double(),
##   name = col_character(),
##   host_id = col_double(),
##   host_name = col_character(),
##   neighbourhood_group = col_character(),
##   neighbourhood = col_character(),
##   latitude = col_double(),
##   longitude = col_double(),
##   room_type = col_character(),
##   price = col_double(),
##   minimum_nights = col_double(),
##   number_of_reviews = col_double(),
##   last_review = col_date(format = ""),
##   reviews_per_month = col_double(),
##   calculated_host_listings_count = col_double(),
##   availability_365 = col_double()
## )

###看一下price的分布

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0    69.0   106.0   152.7   175.0 10000.0

###設置icon 把前500筆抓出來看 針對價格飆出顏色,依照上面的價位作色彩調配,同時設置好icon的樣式

air.50 <- airbnb[1:500,]

getColor <- function(airbnb) {
  sapply(airbnb$price, function(price) {
    if(price <= 69.0) {
      "green"
    } else if(price<= 152.7) {
      "blue"
    } else if(price<= 175.0) {
      "orange"}else{"red"} })
}

icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = getColor(air.50)
)

###畫出來 畫了之後覺得太擠不好看

leaflet(air.50) %>% addTiles() %>%
  addAwesomeMarkers(~longitude, ~latitude, icon=icons, label=~as.character(price))

###Cluster

#Cluster
leaflet(air.50) %>% addTiles() %>% addAwesomeMarkers(
  clusterOptions = markerClusterOptions(),icon=icons, label=~as.character(room_type))
## Assuming "longitude" and "latitude" are longitude and latitude, respectively

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.