
### Download libraries
install.packages("ggmap")
library(ggmap)
library(ggplot2)

## Create a ggmap
my_location <- c(-70,43,-55,50)
myMap <- get_map(location=my_location, source="stamen", maptype="watercolor", crop=FALSE) 
ggmap(myMap)

### Import the geographic coordinates
geo <- read.table("Geographical_data.txt", header=TRUE, dec=".")
geo <- geo[-2,]

### Import the AEM results
aem <- read.table("AEM_vectors.txt", header=TRUE, dec=".")

### Merge both information
geo_aem <- cbind(geo,aem)

### Make a ggplot graph (assignment)
x_title="Longitude"
y_title="Latitude"
graph1 <- ggmap(myMap,extent="panel", fill="grey")
graph1 + geom_point(aes(x = LON, y = LAT,  fill = V4), data=geo_aem, shape = 21, size=3)+ 
  scale_fill_gradient2(low="white", mid="orange", high = "red") + 
  geom_text(data=geo_aem, aes(x=LON,y=LAT,label=POP), vjust=-1.3, colour="black", size=3)+
  theme(axis.text.x=element_text(colour="black"))+
  theme(axis.text.y=element_text(colour="black"))+
  theme(legend.justification=c(1,0), legend.position=c(1,0))+
  guides(fill=FALSE)+
  labs(x=x_title)+
  labs(y=y_title)+
  theme(panel.border = element_rect(colour="black", fill=NA, size=1),
        axis.title=element_text(size=12,colour="black",family="Helvetica"),
        legend.title=element_text(size=10,colour="black",family="Helvetica"))

### Save the map
ggsave("AEM4-lobster.pdf",width=16,height=16,dpi=600,units="cm",useDingbats=F)



### Import the PCNM results
pcnm <- read.table("PCNM_vectors.txt", header=TRUE, dec=".")

### Merge both information
geo_pcnm <- cbind(geo,pcnm)

### Make a ggplot graph (assignment)
x_title="Longitude"
y_title="Latitude"
graph1 <- ggmap(myMap,extent="panel", fill="grey")
graph1 + geom_point(aes(x = LON, y = LAT,  fill = X1), data=geo_pcnm, shape = 21, size=3)+ 
  scale_fill_gradient2(low="white", mid="orange", high = "red") + 
  geom_text(data=geo_pcnm, aes(x=LON,y=LAT,label=POP), vjust=-1.3, colour="black", size=3)+
  theme(axis.text.x=element_text(colour="black"))+
  theme(axis.text.y=element_text(colour="black"))+
  theme(legend.justification=c(1,0), legend.position=c(1,0))+
  guides(fill=FALSE)+
  labs(x=x_title)+
  labs(y=y_title)+
  theme(panel.border = element_rect(colour="black", fill=NA, size=1),
        axis.title=element_text(size=12,colour="black",family="Helvetica"),
        legend.title=element_text(size=10,colour="black",family="Helvetica"))

### Save the map
ggsave("PCNM1-lobster.pdf",width=16,height=16,dpi=600,units="cm",useDingbats=F)
