danielecook
3/20/2017 - 12:59 AM

Generate an N2/CB4856 Map

Generate an N2/CB4856 Map

library(tidyverse)
library(leaflet)
devtools::install_github("wch/webshot")
library(webshot)

# Also requires that you install phantomjs;
# You can install with webshot::install_phantomjs()

strain_isotype <- readr::read_tsv("https://docs.google.com/spreadsheets/d/1V6YHzblaDph01sFDI8YK_fP0H7sVebHQTXypGdiQIjI/pub?output=tsv") %>%
  dplyr::filter(reference_strain == 1, !is.na(isotype)) %>%
  dplyr::filter(isotype %in% c("N2", "CB4856"))

width <- 100
height <- 100

icons <- iconList(
  "N2" = makeIcon(
    iconUrl = "https://storage.googleapis.com/andersenlab.org/img/N2.svg",
    iconWidth = width, iconHeight = height,
    popupAnchorX = 0.00001, popupAnchorY = -height+13,
    iconAnchorX = width/2, iconAnchorY = height), 
  "CB4856" = makeIcon(
    iconUrl = "https://storage.googleapis.com/andersenlab.org/img/CB4856.svg",
    iconWidth  = width, iconHeight = height, 
    popupAnchorX = 0.00001, popupAnchorY = -height+13,
    iconAnchorX = width/2, iconAnchorY  = height
  ))

# Generate a map with locations of all strains
leaflet_data <- strain_isotype %>% 
  dplyr::filter(!is.na(latitude)) %>%
  dplyr::mutate(type = ifelse(!is.na(isotype), "red","grey")) 

attach(leaflet_data)
m <- leaflet(data = leaflet_data) %>% 
  addProviderTiles("Thunderforest.OpenCycleMap") %>%
  addMarkers(~longitude, ~latitude, popup = paste0(strain, " - ", country), 
             icon = ~icons[isotype] ) %>%
  fitBounds(-85, -180, 85, 180)

htmlwidgets::saveWidget(m, "temp.html", selfcontained = FALSE)
webshot("temp.html", file = "map_N2_CB.png",
        cliprect = "viewport", vwidth = 1984, vheight = 1488)