Skip to content

Instantly share code, notes, and snippets.

@DannyArends
Last active June 19, 2020 16:34
Show Gist options
  • Save DannyArends/4d2fb6c1fc6ebf32b6825f24aefbf222 to your computer and use it in GitHub Desktop.
Save DannyArends/4d2fb6c1fc6ebf32b6825f24aefbf222 to your computer and use it in GitHub Desktop.
#
# Download the earth texture from: https://www.solarsystemscope.com/textures/, open in MS Paint, scaled down to 50% size and save as BMP
# Or download it from: https://www.dannyarends.nl/etc/img/2k_earth_daymap.bmp
# JHU CovID19 data from: CSSE at Johns Hopkins University (CSSEGISandData)
# copyright GPL-3 by Danny Arends - Molekulare Zuchtungsbiology - HU Berlin
# Written May, 2020, last modified May, 2020
#
# Change: /path/to/ and set the working dir to the BMP image
setwd("/<path>/<to>/")
# Download the CoVID19 data from CSSE at Johns Hopkins University
baseURL <- "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/"
infected <- read.csv(paste0(baseURL, "time_series_covid19_confirmed_global.csv"), sep = ",", header = TRUE, check.names = FALSE)
infected[is.na(infected)] <- 0
# Read the binary data from a BMP file (1024 x 512 pixels)
image.file <- "2k_earth_daymap.bmp"
myimage.info <- file.info(image.file)
myimage.data <- readBin(image.file, n = as.numeric(myimage.info["size"]), what = "raw")
myimage.colordata <- myimage.data[-c(1:54)] # Remove the header
# Get the 3 different color components in the image
# BMP files store pixtel color in a list of [1: Blue 2: Green 3: Red]
myimage.blue <- matrix(as.numeric(myimage.colordata[seq(1, length(myimage.colordata), 3)]), 1024, 512)
myimage.green <- matrix(as.numeric(myimage.colordata[seq(2, length(myimage.colordata), 3)]), 1024, 512)
myimage.red <- matrix(as.numeric(myimage.colordata[seq(3, length(myimage.colordata), 3)]), 1024, 512)
# Pixel locations (1024 x 512), and color coversion using the rgb() function
locations <- cbind(rep(1:1024, 512), unlist(lapply(seq(1, 512), rep, 1024)))
image.colors <- rgb(as.numeric(myimage.red), as.numeric(myimage.green), as.numeric(myimage.blue), maxColorValue = 255)
# Recreate the image using pointillism, (1024 x 512)
plot(c(1,1024), c(1,512), t = 'n', yaxt = 'n' , xaxt = 'n', xaxs = "i", yaxs = "i", ann=FALSE)
points(locations, col = image.colors, cex=0.7, pch=15)
# Select which countries we want to plot, get the number of infected
selected <- c("France", "Germany", "Netherlands", "Australia", "Portugal", "US", "China")
infected <- infected[which(as.character(infected[,"Country/Region"]) %in% selected),]
# Assign colors to each country
library(RColorBrewer)
col.countries = brewer.pal(length(selected), "Accent")
names(col.countries) <- selected
# Get the number of infected people on a certain date
Ninfected <- infected[, "6/17/20"]
Ninfected <- round(log10(Ninfected)+1, 2)
countries <- as.character(infected[, "Country/Region"])
# Modify the positional data to fit the size of our image
# Latidudes are on a circle from -90 North, to +90 South
# Longitude are on a circle from -180 East, to +180 West
latitude <- ((as.numeric(infected[, "Lat"]) + 90) / 180) * 512 # Move the range from -90 - 90 to our image size (0 - 512)
longitude <- ((as.numeric(infected[, "Long"]) + 180) / 360) * 1024 # Move the range from -180 - 180 to our image size (0 - 1024)
# Plot the number of CoVID19 infections on a certain date onto the map
points(x = longitude, y = latitude, cex = Ninfected/2, pch = 19, col = col.countries[countries])
legend("topleft", names(col.countries), fill = col.countries, bg = "white")
box()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment