Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
---
title: "Get Started"
author: "John Coene"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{John Coene}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library(datamaps)
```
Initiate your map with `datamaps`, pipe (`%>%`) your data to add data (`add_data`) or layers.
### Your first map
```{r}
# 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
```
### Examples
```{r}
data.frame(
state = state.abb,
area = state.area
) %>%
datamaps(scope = "usa", height = "400") %>%
add_choropleth(state, area)
```
Combine types.
```{r}
library(datamaps)
library(dplyr)
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")
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))
sesh %>%
datamaps(default = "lightgray", height = "400") %>% # initiate map
add_choropleth(iso3c, Sessions, colors = RColorBrewer::brewer.pal(5, "YlOrRd")) %>% # add choropleth 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.
```{r}
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).
```{r}
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)
```
### Custom map
```{r}
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 };
}
')
)
```
### Plugins
SVG icons
```{r}
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
```{r}
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)
```