Initiate your map with datamaps
, pipe (%>%
) your data to add data (add_data
) or layers.
# Fake data st <- c("AR", "NY", "CA", "IL", "CO", "MT", "TX", "OK", "AL", "NM", "NV", "NC", "OH", "PA") states <- data.frame(from = st, to = sample(st, 14)) states %>% datamaps(scope = "usa", height = "400") %>% # intiate map add_arcs_name(from, to) %>% # add arcs layers add_labels() # add state labels
data.frame( state = state.abb, area = state.area ) %>% datamaps(scope = "usa", height = "400") %>% add_choropleth(state, area)
Combine types.
library(datamaps) 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 sesh <- read.csv("https://raw.githubusercontent.com/JohnCoene/projects/master/htmlwidgets/echarts/data/sessions.csv") # datamaps uses iso3c format sesh$iso3c <- countrycode::countrycode(sesh$Country, "country.name", "iso3c") #> Warning in countrycode::countrycode(sesh$Country, "country.name", "iso3c"): Some values were not matched unambiguously: , (not set) sesh <- sesh[!is.na(sesh$iso3c),] # remove NA sesh$continent <- countrycode::countrycode(sesh$iso3c, "iso3c", "continent") # find continents # sum Sessions by continent continents <- sesh %>% group_by(continent) %>% summarise(Sessions = sum(Sessions)) %>% mutate(long = c(3.917313, -99.133209, 106.660172, 16.363449), lat = c(7.401962, 19.432608, 10.762622, 48.210033)) #> `summarise()` ungrouping output (override with `.groups` argument) sesh %>% datamaps(default = "lightgray", height = "400") %>% # initiate map add_choropleth(iso3c, Sessions, colors = RColorBrewer::brewer.pal(5, "YlOrRd")) %>% # add chorpleth layer add_data(continents) %>% # add continent dataset add_bubbles(long, lat, log(Sessions) * 5, continent, continent, colors = RColorBrewer::brewer.pal(4, "Set1")) # add continent bubbles
Set different projections and add graticule.
data <- data.frame(name = c("USA", "CAN", "BRA", "ARG", "MEX", "CUB", "BOL"), color = round(runif(7, 1, 10))) data %>% datamaps(projection = "orthographic", height = "400") %>% add_choropleth(name, color, colors = c("skyblue", "yellow", "orangered")) %>% add_graticule()
All of it (nearly).
coords <- data.frame(city = c("London", "New York", "Beijing", "Sydney"), lon = c(-0.1167218, -73.98002, 116.3883, 151.18518), lat = c(51.49999, 40.74998, 39.92889, -33.92001), values = c(11, 23, 29 , 42)) data <- data.frame(name = c("USA", "FRA", "CHN", "RUS", "COG", "DZA", "BRA", "AFG"), color = round(runif(8, 1, 10))) edges <- data.frame(origin = c("USA", "FRA", "BGD", "ETH", "KHM", "GRD", "FJI", "GNB", "AUT", "YEM"), target = c("BRA", "USA", "URY", "ZAF", "SAU", "SVK", "RWA", "SWE", "TUV", "ZWE"), strokeColor = rep(c("gray", "black"), 5)) data %>% datamaps(default = "lightgray", height = "400") %>% add_choropleth(name, color) %>% add_data(coords) %>% add_bubbles(lon, lat, values, values, city, colors = c("skyblue", "darkblue")) %>% add_data(edges) %>% add_arcs_name(origin, target, strokeColor)
topo <- paste0( "https://rawgit.com/Anujarya300/bubble_maps/", "master/data/geography-data/india.topo.json" ) data <- data.frame(state = c("JH", "MH"), value = c(55, 28)) data %>% datamaps(scope = "india", height = "600") %>% add_choropleth(state, value) %>% config_geo(data.url = topo) %>% set_projection(htmlwidgets::JS(' function (element) { var projection = d3.geo.mercator() .center([78.9629, 23.5937]) .scale(1000); var path = d3.geo.path().projection(projection); return { path: path, projection: projection }; } 'et_projection(htmlwidgets::JS(' function (element) { var projection = d3.geo.mercator() .center([78.9629, 23.5937]) .scale(1000); var path = d3.geo.path().projection(projection); return { path: path, projection: projection }; } ') )
SVG icons
coords <- data.frame( city = c("London", "New York", "Beijing", "Sydney"), lon = c(-0.1167218, -73.98002, 116.3883, 151.18518), lat = c(51.49999, 40.74998, 39.92889, -33.92001) ) coords %>% datamaps() %>% add_icons(lon, lat)
Custom markers
icon_url <- "http://github.com/JohnCoene.png" coords %>% datamaps() %>% markers_options( icon = list( url = icon_url, width = 20, height = 20 ), fillOpacity = 1 ) %>% add_markers(lon, lat)