# Niels Holst, Aarhus University, Denmark
# niels.holst@agro.au.dk
# 14 March 2022

rm(list=ls(all=TRUE))

library(ggmap)
library(ggplot2)
library(ggpubr)
library(lubridate)
library(plyr)
library(reshape2)

# See https://developers.google.com/maps/documentation/maps-static/get-api-key
key = Sys.getenv("GOOGLE_CLOUD_API_KEY") # Fetch key value, which I have set in my registry
register_google(key)                     # Use key to log into Google cloud

plot_map = function(M, left=0, right=0, top=0, bottom=0, size=2, alpha=0.4) {
  geo_rect = 
    c(
      floor(min(M$Longitude, na.rm=TRUE)) - left,
      floor(min(M$Latitude, na.rm=TRUE)) - bottom,
      ceiling(max(M$Longitude, na.rm=TRUE)) + right,
      ceiling(max(M$Latitude, na.rm=TRUE)) + top
  )
  
  my_map = get_map(location = geo_rect, zoom=6)
  ggmap(my_map) +
    geom_point(aes(Longitude,Latitude), data=M, colour="brown", alpha=alpha,size=size) +
    labs(x="", y="") +
    theme(
      axis.text = element_blank(),
      axis.ticks = element_blank()
    )
}

# See https://data-se.netlify.com/2018/12/12/changing-the-default-color-scheme-in-ggplot2/
red = '#e41a1c'
blue = '#377eb8'
green = '#4daf4a'
violet = '#984ea3'
orange = '#ff7f00'
brown = '#a65628'
pink = '#f781bf'
grey = '#999999'
unisim_colours = c(red,blue,green,violet,orange,brown,pink,grey) 

unisim_colour = function(i) { 
  strtoi(c(
    paste0("0X", substr(unisim_colours[i], 2,3)),
    paste0("0X", substr(unisim_colours[i], 4,5)),
    paste0("0X", substr(unisim_colours[i], 6,7))
  ))
}

scale_colour_discrete = function(...) {
  scale_colour_manual(..., values = rep(unisim_colours, 10))
}

theme_set(theme_bw())

