Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emilhvitfeldt/tilemapr
R functions for square and hextile maps
https://github.com/emilhvitfeldt/tilemapr
data-visualization ggplot2 hextile-maps r rstats
Last synced: about 2 months ago
JSON representation
R functions for square and hextile maps
- Host: GitHub
- URL: https://github.com/emilhvitfeldt/tilemapr
- Owner: EmilHvitfeldt
- License: other
- Created: 2017-05-17T17:32:20.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-12-22T17:39:21.000Z (almost 7 years ago)
- Last Synced: 2024-08-06T03:05:14.185Z (5 months ago)
- Topics: data-visualization, ggplot2, hextile-maps, r, rstats
- Language: R
- Size: 1.64 MB
- Stars: 8
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```## Overview
[![Travis build status](https://travis-ci.org/EmilHvitfeldt/tilemapr.svg?branch=master)](https://travis-ci.org/EmilHvitfeldt/tilemapr)
R functions for creating square and hextile maps for various countries (only USA for now).
## Installation
```{r, eval = FALSE}
# install.packages("devtools")
devtools::install_github("EmilHvitfeldt/tilemapr", force = TRUE)
```## Usage
```{r, include=FALSE}
devtools::install_github("EmilHvitfeldt/tilemapr", force = TRUE)
```Tile grid maps are good alternative when the population isn't of much interest. Below we see a minimal example using ggplot2 and `geom_map()`:
```{r, message=FALSE}
library(tidyverse)
library(tilemapr)# Creating data
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
states_map <- square_usa()ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = states_map) +
expand_limits(x = states_map$long, y = states_map$lat)
```All the functions include a number of parameters to control the resulting data.frame. The two most important ones are the `d` and `center`. `d` is a kind of diameter parameter which is used to control the size of the tiles. And `center` make the function gives us a data.frame with the coordinates of the center of the tile.
```{r}
states_map <- hex_usa(d = 0.5)ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = states_map) +
expand_limits(x = states_map$long, y = states_map$lat) +
geom_text(data = hex_usa(d = 0.5, center = TRUE),
aes(x = long, y = lat, label = region_abr),
inherit.aes = FALSE)
```We see here that DC is floating, this will be a common issue. It can be resolved by either removing it with `exclude` or to plotting a dataless map underneath:
```{r}
states_map <- hex_usa(d = 0.5)ggplot(states_map, aes(map_id = region)) +
geom_map(aes(), map = states_map) +
geom_map(data = crimes, map = states_map,
aes(fill = Murder, map_id = state)) +
expand_limits(x = states_map$long, y = states_map$lat) +
geom_text(data = hex_usa(d = 0.5, center = TRUE),
aes(x = long, y = lat, label = region_abr),
inherit.aes = FALSE)
```Lastly some of the functions have different layout, which are picked using the `style`.
```{r, echo=FALSE, message=FALSE}
library(gridExtra)states_map <- square_usa(style = "The Guardian")
q1 <- ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = square_usa(style = "NPR")) +
expand_limits(x = c(-1, 13), y = c(-2, 9)) +
labs(title = "NPR") +
guides(fill = "none") +
theme_void() +
coord_fixed(ratio = 1)
q2 <- ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = square_usa(style = "The New York Times")) +
expand_limits(x = c(-1, 13), y = c(-2, 9)) +
labs(title = "The New York Times") +
guides(fill = "none") +
theme_void() +
coord_fixed(ratio = 1)
q3 <- ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = square_usa(style = "538")) +
expand_limits(x = c(-1, 13), y = c(-2, 9)) +
labs(title = "538") +
guides(fill = "none") +
theme_void() +
coord_fixed(ratio = 1)
q4 <- ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = square_usa(style = "Propublica")) +
expand_limits(x = c(-1, 13), y = c(-2, 9)) +
labs(title = "Propublica") +
guides(fill = "none") +
theme_void() +
coord_fixed(ratio = 1)
q5 <- ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = square_usa(style = "Bloomberg")) +
expand_limits(x = c(-1, 13), y = c(-2, 9)) +
labs(title = "Bloomberg") +
guides(fill = "none") +
theme_void() +
coord_fixed(ratio = 1)
q6 <- ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = square_usa(style = "The Guardian")) +
expand_limits(x = c(-1, 13), y = c(-2, 9)) +
labs(title = "The Guardian") +
guides(fill = "none") +
theme_void() +
coord_fixed(ratio = 1)
q7 <- ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = square_usa(style = "Wall Street Journal")) +
expand_limits(x = c(-1, 13), y = c(-2, 9)) +
labs(title = "Wall Street Journal") +
guides(fill = "none") +
theme_void() +
coord_fixed(ratio = 1)
q8 <- ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = square_usa(style = "WNYC")) +
expand_limits(x = c(-1, 13), y = c(-2, 9)) +
labs(title = "WNYC") +
guides(fill = "none") +
theme_void() +
coord_fixed(ratio = 1)
q9 <- ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = square_usa(style = "The Marshall Project")) +
expand_limits(x = c(-1, 13), y = c(-2, 9)) +
labs(title = "The Marshall Project") +
guides(fill = "none") +
theme_void() +
coord_fixed(ratio = 1)grid.arrange(q1, q2, q3, q4, q5, q6, q7, q8, q9, ncol = 3, nrow = 3)
```## References
I have gotten the corrent layouts from [http://blog.yanofsky.info/ (square_usa)](http://blog.yanofsky.info/post/117635988235/there-appears-to-be-some-disagreement-on-the?tweet=tweet) and [http://blog.apps.npr.org/ (hex_usa)](http://blog.apps.npr.org/2015/05/11/hex-tile-maps.html)