knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(geosparklines)
library(readr)
library(magrittr)
library(dplyr)
library(sf)

Prepare your Data

You'll need :

First, download the world COVID-19 cases from John Hopkins Institute :

f <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")

Then, let's select the relevant informations :

coords <- f[, c("Long", "Lat")]
col_dates <- grep("[0-9]*/[0-9]*/[0-9]*", names(f)) # select all column dates
my_stats <- data.frame(f)[, col_dates]

Create your GeoSparkLines

Define the width and height in spatial units of your lines and the transformation mode : normal or log for your values

## Whole world
geosparks <- geospark(coords, my_stats, width=4, height=2, direction = "ne", mode = "log") %>% st_set_crs(4326)
print(class(geosparks))

You can add your GeoSparkLines to your data :

st_geometry(f) <- geosparks

Let's do some simple plotting :

library(rnaturalearth)
library(rnaturalearthdata)
library(ggplot2)
library(rgeos)
world <- ne_countries(scale = "medium", returnclass = "sf")

last_day <- names(f)[col_dates %>% tail(1)]

ggplot() +
    geom_sf(data=world) +
    geom_sf(data=f, col="red") +
    coord_sf(xlim = c(-20, 30), ylim = c(25, 60), expand = FALSE) +
    ggtitle(sprintf("Covid-19 cases sparklines (log-transformed)\n%s", last_day))

Export your work

...for further styling, for example in QGIS

st_write(f, "world_sparklines.gpkg", delete_dsn = TRUE)

Individual sparklines

You might want to create sparklines individually, for example to pilot the individual heights and widths of your sparklines more precisely

You can do it with singleGeospark.

Here is an example on France.

i <- which(f$`Country/Region`=="France" & is.na(f$`Province/State`))
v <- my_stats[i, ] %>% as.numeric
my_coords <- coords[i, ] %>% as.matrix
sp_line <- singleGeospark(my_coords, v, width=20, height=20, mode="log")
plot(st_geometry(sp_line))

By default, the maximum value which defines the height will be the maximum value of the values you give to the function.

You can put a custom max value. Here, I use the maximum value of the global dataset

sp_line <- singleGeospark(my_coords, v, max_v = max(my_stats), width=20, height=20, mode="log")
plot(st_geometry(sp_line))


datagistips/geosparklines documentation built on April 28, 2020, 3:03 p.m.