rm(list = ls())
library("highcharter")
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("purrr")
##
## Attaching package: 'purrr'
## The following object is masked from 'package:dplyr':
##
## order_by
library("geojsonio")
##
## We recommend using rgdal v1.1-1 or greater, but we don't require it
## rgdal::writeOGR in previous versions didn't write
## multipolygon objects to geojson correctly.
## See https://stat.ethz.ch/pipermail/r-sig-geo/2015-October/023609.html
##
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
##
## pretty
library("httr")
map <- "https://raw.githubusercontent.com/stefanocudini/leaflet-geojson-selector/master/examples/italy-regions.json" %>%
GET() %>%
content() %>%
jsonlite::fromJSON(simplifyVector = FALSE)
map$features[[1]]$properties
## $id
## [1] 1
##
## $name
## [1] "piemonte"
##
## $length
## [1] 1236869
##
## $area
## [1] 25394103942
dfita1 <- map$features %>%
map_df(function(x){
as_data_frame(x$properties)
})
head(dfita1)
## Source: local data frame [6 x 4]
##
## id name length area
## (int) (chr) (dbl) (dbl)
## 1 1 piemonte 1236869.4 25394103942
## 2 2 valle d'aosta 311165.1 3259040961
## 3 3 lombardia 1411265.1 23862698614
## 4 4 trentino-alto adige/sudtirol 800534.1 13608018379
## 5 5 veneto 1057856.0 18405499479
## 6 6 friuli venezia giulia 667489.7 7864293935
highchart(type = "map") %>%
hc_add_series_map(map = map, df = dfita1, joinBy = "id", value = "length")
url <- "http://code.highcharts.com/mapdata/countries/it/it-all.geo.json"
tmpfile <- tempfile(fileext = ".json")
download.file(url, tmpfile)
ita <- readLines(tmpfile)
## Warning in readLines(tmpfile): incomplete final line found on 'C:\Users
## \jkunst\AppData\Local\Temp\RtmporM2LE\filea2414387a28.json'
ita <- gsub(".* = ", "", ita)
ita <- jsonlite::fromJSON(ita, simplifyVector = FALSE)
get id the regions/states
x <- ita$features[[1]]
x$properties
## $`hc-group`
## [1] "admin1"
##
## $`hc-middle-x`
## [1] 0.64
##
## $`hc-middle-y`
## [1] 0.51
##
## $`hc-key`
## [1] "it-na"
##
## $`hc-a2`
## [1] "NA"
##
## $labelrank
## [1] "3"
##
## $hasc
## [1] "IT.NA"
##
## $`alt-name`
## [1] "Campanha|Campanie|Kampanien"
##
## $`woe-id`
## [1] "12591829"
##
## $subregion
## NULL
##
## $fips
## [1] "IT04"
##
## $`postal-code`
## [1] "NA"
##
## $name
## [1] "Napoli"
##
## $country
## [1] "Italy"
##
## $`type-en`
## [1] "Province"
##
## $region
## [1] "Campania"
##
## $longitude
## [1] "14.4226"
##
## $`woe-name`
## [1] "Napoli"
##
## $latitude
## [1] "40.8773"
##
## $`woe-label`
## NULL
##
## $type
## [1] "Province"
dfita2 <- ita$features %>%
map_df(function(x){
data_frame(hasc = x$properties$hasc, name = x$properties$name)
}) %>% # extract the keys
mutate(random = runif(nrow(.))) # create random value
head(dfita2)
## Source: local data frame [6 x 3]
##
## hasc name random
## (chr) (chr) (dbl)
## 1 IT.NA Napoli 0.8625917
## 2 IT.TP Trapani 0.6794978
## 3 IT.PA Palermo 0.6944048
## 4 IT.ME Messina 0.1042229
## 5 IT.AG Agrigento 0.4610588
## 6 IT.NU Nuoro 0.6354779
highchart(type = "map") %>%
hc_title(text = "Airports in Australia") %>%
hc_add_series_map(map = ita, df = dfita2, joinBy = "hasc", value = "random")