## remove (almost) everything in the working environment.
rm(list = ls())


# to import excel files
if (!require('readxl')) install.packages('readxl'); library('readxl')

# to plot maps
if (!require('raster')) install.packages('raster'); library('raster')
library(rnaturalearth,rnaturalearthdata)

library("sf")
sf_use_s2(FALSE) #allows to get the original behavior of package sf

# Set working directory based on the this.path package, which reliably detects the path of the running script. and projects.
if (!require(this.path)) {
  install.packages("this.path")
  require(this.path)
}
setwd(dirname(this.path()))

#to install naturalearth 
#if (!require('devtools')) install.packages('devtools'); library('devtools')
#devtools::install_github("ropenscilabs/rnaturalearth")
#devtools::install_github("ropenscilabs/rnaturalearthdata")
#install.packages("rnaturalearthhires",
#                 repos = "http://packages.ropensci.org",
#                 type = "source")



#load data
profs <- as.data.frame(read_excel("siblings-extended.xlsx"))

#load full database (to get geo coordinates) 
base <- as.data.frame(read_excel("Profs.xlsx"))
#remove 9 occupations
base<-base[!(base$Field=="9"),]

#list of institutions
listi <- c("Ucopenhagen-1475","Ugroningen-1612","Ulund-1666","Uuppsala-1477","AcadCopenhagen-1742","Urostock-1419",
"Ukiel-1652","Uabo-1640","Udorpat-1632","Aamsterdam-1632","AcadDantzig-1743","Uedinburgh-1582","Ukonigsberg-1544","AcadStockholm-1739",
"Uglasgow-1451","Ugreifswald-1456","Gdanzig-1558","AcadStpetersburg-1724","AcadLund-1778","Ufraneker-1585","AcadHaarlem-1752",
"AcadEdinburgh-1783","AcadUppsala-1728","Uaberdeennew-1593","Uaberdeenold-1495","Ustandrews-1411",
"Uleiden-1575","AcadHessehamburg-1775","AcadTrondheim-1766","Uutrecht-1636","GymSoro-1586")


#select institutions
base<-base[base$IKey %in% listi,]


#redefining locations
base$LongitudeBirth <- base$Locations_Longitude
base$LatitudeBirth <- base$Locations_Latitude

#redefining locations
base$LongitudeActivity <- base$Locations_2_Longitude
base$LatitudeActivity <- base$Locations_2_Latitude



#load map
blank2 <- ne_countries(scale = "large",type = 'countries', returnclass = "sf")
blank1 <- st_crop(blank2 , xmin = -23, xmax = 44, ymin = 43, ymax = 67)


sea <- rgb(240/255,248/255,255/255,.5)  #lighter Aliceblue

#to add country borders
map <- read_sf("clip_1710.shp")
options(sf_max.plot=1)

#magnification
def <- 2



#map all scholars 

baseSL <- base[base$WeakLink==F,]
baseWL <- base[base$WeakLink==T,]


png(file="map-QQ-scandi-all.png", width = 2600*def, height = 1500*def)

plot(st_geometry(blank1),col="gray85",lty=0,xlim=c(-23,44),ylim=c(43,67))

points(baseSL[,c("LongitudeBirth","LatitudeBirth")],pch=21, bg="orangered3" ,cex=2*def,lwd=def)
points(baseWL[,c("LongitudeBirth","LatitudeBirth")],pch=21, bg="orange" ,cex=2*def,lwd=def)
points(base[,c("LongitudeActivity","LatitudeActivity")],pch=21, bg="black" ,cex=3*def,lwd=def)

legend(-22, 63, legend=c("Institutions","Origin of scholars","Origin of corresponding scholars"),
 col=c("black","black","black"),
pt.bg=c("black", "orangered3","orange"),pch=c(21,21),
pt.cex=c(3*def,2*def,2*def),pt.lwd=c(def,def,def),cex=3*def,bty="n")

plot(st_crop(map,c(xmin=-23,ymin=43,xmax=44,ymax=67)),col=rgb(0,0,0,0),add=T)

dev.off()



listp <- profs$PKey

base<-base[base$PKey %in% listp,]


baseSL <- base[base$WeakLink==F,]
baseWL <- base[base$WeakLink==T,]

png(file="map-QQ-scandi-with-g.png", width = 2600*def, height = 1500*def)

plot(st_geometry(blank1),col="gray85",lty=0,xlim=c(-23,44),ylim=c(43,67))

points(baseSL[,c("LongitudeBirth","LatitudeBirth")],pch=21, bg="orangered3" ,cex=2*def,lwd=def)
points(baseWL[,c("LongitudeBirth","LatitudeBirth")],pch=21, bg="orange" ,cex=2*def,lwd=def)
points(base[,c("LongitudeActivity","LatitudeActivity")],pch=21, bg="black" ,cex=3*def,lwd=def)

legend(-22, 63, legend=c("Institutions","Origin of scholars","Origin of corresponding scholars"),
 col=c("black","black","black"),
pt.bg=c("black", "orangered3","orange"),pch=c(21,21),
pt.cex=c(3*def,2*def,2*def),pt.lwd=c(def,def,def),cex=3*def,bty="n")

plot(st_crop(map,c(xmin=-23,ymin=43,xmax=44,ymax=67)),col=rgb(0,0,0,0),add=T)

dev.off()






##############


## remove (almost) everything in the working environment.
rm(list = ls())


# to import excel files
if (!require('readxl')) install.packages('readxl'); library('readxl')

# to plot maps
if (!require('raster')) install.packages('raster'); library('raster')
library(rnaturalearth,rnaturalearthdata)

library("sf")
sf_use_s2(FALSE) #allows to get the original behavior of package sf

# Set working directory based on the this.path package, which reliably detects the path of the running script. and projects.
if (!require(this.path)) {
  install.packages("this.path")
  require(this.path)
}
setwd(dirname(this.path()))

#to install naturalearth 
#if (!require('devtools')) install.packages('devtools'); library('devtools')
#devtools::install_github("ropenscilabs/rnaturalearth")
#devtools::install_github("ropenscilabs/rnaturalearthdata")
#install.packages("rnaturalearthhires",
#                 repos = "http://packages.ropensci.org",
#                 type = "source")



#load data
profs <- as.data.frame(read_excel("siblings-extended.xlsx"))

#load full database (to get geo coordinates) 
base <- as.data.frame(read_excel("Profs.xlsx"))
#remove 9 occupations
base<-base[!(base$Field=="9"),]

#list of institutions
listi <- c("Ucopenhagen-1475","Ugroningen-1612","Ulund-1666","Uuppsala-1477","AcadCopenhagen-1742","Urostock-1419",
           "Ukiel-1652","Uabo-1640","Udorpat-1632","Aamsterdam-1632","AcadDantzig-1743","Uedinburgh-1582","Ukonigsberg-1544","AcadStockholm-1739",
           "Uglasgow-1451","Ugreifswald-1456","Gdanzig-1558","AcadStpetersburg-1724","AcadLund-1778","Ufraneker-1585","AcadHaarlem-1752",
           "AcadEdinburgh-1783","AcadUppsala-1728","Uaberdeennew-1593","Uaberdeenold-1495","Ustandrews-1411",
           "Uleiden-1575","AcadHessehamburg-1775","AcadTrondheim-1766","Uutrecht-1636","GymSoro-1586")


#select institutions
base<-base[base$IKey %in% listi,]


#redefining locations
base$LongitudeBirth <- base$Locations_Longitude
base$LatitudeBirth <- base$Locations_Latitude

#redefining locations
base$LongitudeActivity <- base$Locations_2_Longitude
base$LatitudeActivity <- base$Locations_2_Latitude



#load map
blank2 <- ne_countries(scale = "large",type = 'countries', returnclass = "sf")
blank1 <- st_crop(blank2 , xmin = -23, xmax = 44, ymin = 43, ymax = 67)


sea <- rgb(240/255,248/255,255/255,.5)  #lighter Aliceblue

#to add country borders
map <- read_sf("clip_1710.shp")
options(sf_max.plot=1)

#magnification
def <- 2



#map all scholars 

baseSL <- base[base$WeakLink==F,]
baseWL <- base[base$WeakLink==T,]


png(file="map-QQ-scandi-all.png", width = 2600*def, height = 1500*def)

plot(st_geometry(blank1),col="gray85",lty=0,xlim=c(-23,44),ylim=c(43,67))

points(baseSL[,c("LongitudeBirth","LatitudeBirth")],pch=21, bg="orangered3" ,cex=2*def,lwd=def)
points(baseWL[,c("LongitudeBirth","LatitudeBirth")],pch=24, bg="orange" ,cex=2*def,lwd=def)
points(base[,c("LongitudeActivity","LatitudeActivity")],pch=22, bg="black" ,cex=3*def,lwd=def)

legend(-22, 63, legend=c("Institutions","Origin of scholars","Origin of corresponding scholars"),
       col=c("black","black","black"),
       pt.bg=c("black", "orangered3","orange"),pch=c(22,21,24),
       pt.cex=c(3*def,2*def,2*def),pt.lwd=c(def,def,def),cex=3*def,bty="n")

plot(st_crop(map,c(xmin=-23,ymin=43,xmax=44,ymax=67)),col=rgb(0,0,0,0),add=T)

dev.off()



listp <- profs$PKey

base<-base[base$PKey %in% listp,]


baseSL <- base[base$WeakLink==F,]
baseWL <- base[base$WeakLink==T,]

png(file="map-QQ-scandi-with-g.png", width = 2600*def, height = 1500*def)

plot(st_geometry(blank1),col="gray85",lty=0,xlim=c(-23,44),ylim=c(43,67))

points(baseSL[,c("LongitudeBirth","LatitudeBirth")],pch=21, bg="orangered3" ,cex=2*def,lwd=def)
points(baseWL[,c("LongitudeBirth","LatitudeBirth")],pch=24, bg="orange" ,cex=2*def,lwd=def)
points(base[,c("LongitudeActivity","LatitudeActivity")],pch=22, bg="black" ,cex=3*def,lwd=def)

legend(-22, 63, legend=c("Institutions","Origin of scholars","Origin of corresponding scholars"),
       col=c("black","black","black"),
       pt.bg=c("black", "orangered3","orange"),pch=c(22,21,24),
       pt.cex=c(3*def,2*def,2*def),pt.lwd=c(def,def,def),cex=3*def,bty="n")

plot(st_crop(map,c(xmin=-23,ymin=43,xmax=44,ymax=67)),col=rgb(0,0,0,0),add=T)

dev.off()
