---
title: "Pony_map"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(rworldmap)
library(rworldxtra)
library(ggmap)
library(mapproj)
library(RgoogleMaps)
library(ggplot2)
library(grid)
library(maps)
```

```{r}
pony_sightings <- read.csv("/Users/rachaelantwis/Dropbox/Research/Current/Papers/Ponies_microbiomes/Manuscript/Pony_map/Sightings_for_R.csv")
pony_sightings
colnames(pony_sightings)

# Read map from google maps and save data to file
mapImageData <- get_googlemap(
  c(lon=-3.578, lat=53.135), 
  zoom=14
)
save(mapImageData, file="savedMap.rda")

# Load the saved file
load(file="savedMap.rda")


# Plot
map <- ggmap(mapImageData) +
  geom_point(aes(x=Lon, y=Lat, colour=Band), data=pony_sightings, size=6) +
  labs(x='Longitude', y='Latitude') +
theme(axis.text.x=element_text(size=25),
axis.title.x=element_text(size=25), 
      axis.text.y=element_text(size=25),axis.title.y=element_text(size=25),
      legend.text=element_text(size=30), legend.title=element_text(size=30))


map

#Larger map for scale
# Read map from google maps and save data to file
mapImageDataLarge <- get_googlemap(
  c(lon=-3.578, lat=53.135), 
  zoom=6
)
save(mapImageDataLarge, file="savedMapLarge.rda")

# Load the saved file
load(file="savedMapLarge.rda")


# Plot
mapLarge <- ggmap(mapImageDataLarge) +
  geom_point(aes(x=Lon, y=Lat, colour=Band), data=pony_sightings, size=3) +
  labs(x='Longitude', y='Latitude')

mapLarge
```

#Correlate social network and microbiome composition
```{r}
#Rachaels computer
setwd("~/Dropbox (The University of Manchester)/Ponies (1)/Manuscript/Welsh_Pony_Analysis")

# Susanne's computer
setwd("~/Dropbox (The University of Manchester)/Ponies (1)/Manuscript/Welsh_Pony_Analysis")

library(igraph)
library(qgraph)

ind.att<- read.csv("individual.attribute.csv")

# load microbiome distance matrix
pony_dist<- as.matrix(read.csv("pony_dist_matrix.csv",row.names = 1))

#load 2014 association matrix
pony_association14 <- read.csv("2014_associationindex.csv",row.names = "WEIGHTINGS")

#make data frame and order by name
pony_ass14 <- as.data.frame(pony_association14)
pony_ass_sort14 <- pony_ass14[order(row.names(pony_ass14)),order(names(pony_ass14))]

# microbiome distance network plot
# Make microbiome into matrix. Take inverse to make 'close' distance a larger value than 'far' distance.
pony_dist_inverse<- 1/na.omit(as.matrix(pony_dist))

ponies_micro_dist <- graph.adjacency(pony_dist_inverse, diag= FALSE, mode="undirected",weighted=TRUE)

# get names of ponies in microbiome to subset the social data.
pony_subset <- rownames(pony_dist)

# make AI into matrix.
pony_ass_sub14 <- as.matrix(pony_ass_sort14[row.names(pony_ass_sort14) %in% pony_subset,colnames(pony_ass_sort14) %in% pony_subset])

#correlate the two matrices
cor_mat <- cor.test(as.vector(pony_ass_sub14),as.vector(pony_dist), method = "pearson")
```

#Network plots
```{r}
# as graph object- so R reads it as a netowrk
ponies_graph <- graph.adjacency(pony_ass_sub14, mode="undirected",weighted=TRUE)

#set colours by bands using attribute file
V(ponies_graph)$Band=as.character(ind.att$Band[match(V(ponies_graph)$name,ind.att$ID)])

V(ponies_graph)$color=V(ponies_graph)$Band
V(ponies_graph)$color=gsub("Valley","#619CFF",V(ponies_graph)$color) #Valley blue
V(ponies_graph)$color=gsub("Aber","#F8766D",V(ponies_graph)$color) #Aber salmon
V(ponies_graph)$color=gsub("Marsh","#00BA38",V(ponies_graph)$color) #Aber salmon

#generic plot without loabels
plot.igraph(ponies_graph,vertex.label=NA,layout=layout.fruchterman.reingold)

#plot the graph with individuals labelled by community ID- weighted by microbiome
# Wierdly the orientation moves around but the labels to go with the groups are  static
# replotting will eventually get the plot to line up with the labels. I'm sure there is a 
# way to regulate the plotting orientation but it doesn't seem to be a default that can be set.
plot.igraph(ponies_graph,vertex.size=7,vertex.label=NA,vertex.size=4, vertex.frame.color= NA,layout=layout.fruchterman.reingold, edge.color="grey",edge.width=(E(ponies_micro_dist)$weight)/2)
text(c(-1.2, 1.0, 1.0), c(0.35, -0.7, 1.2), c( "Aber","Valley","Marsh"))

#colour by commmunity analysis
#ponies_graph.com <- fastgreedy.community(ponies_graph)
#V(ponies_graph)$color <- ponies_graph.com$membership
#plot(ponies_graph)

# again- colour by band- this time with the microbiome distance data
V(ponies_micro_dist)$Band=as.character(ind.att$Band[match(V(ponies_micro_dist)$name,ind.att$ID)])

V(ponies_micro_dist)$color=V(ponies_micro_dist)$Band
V(ponies_micro_dist)$color=gsub("Valley","#619CFF",V(ponies_micro_dist)$color) #Valley blue
V(ponies_micro_dist)$color=gsub("Aber","#F8766D",V(ponies_micro_dist)$color) #Aber salmon
V(ponies_micro_dist)$color=gsub("Marsh","#00BA38",V(ponies_micro_dist)$color) #Aber salmon

plot.igraph(ponies_micro_dist,vertex.label=NA,layout=layout.fruchterman.reingold)

#cut weak edges- first to find quantiles convert to a vector (not sure necessary but worked)
pony_vector <- as.vector(pony_dist_inverse) 
summary(pony_vector)

# cut edges that are less than median
ponies_micro_dist_cut=delete.edges(ponies_micro_dist, which(E(ponies_micro_dist)$weight <=2.516)) # here's my condition.
plot.igraph(ponies_micro_dist_cut,vertex.label=NA,vertex.frame.color= NA,layout=layout.fruchterman.reingold,edge.width=(E(ponies_micro_dist_cut)$weight)/4)

# qgraph lets you play with the layout in more detail than igraph default
#e <- get.edgelist(ponies_micro_dist_cut, names=FALSE)
#l <- qgraph.layout.fruchtermanreingold(e,vcount=vcount(ponies_micro_dist_cut),area=8*(vcount(ponies_micro_dist_cut)),repulse.rad=(vcount(ponies_micro_dist_cut)^5))

#plot.igraph(ponies_micro_dist_cut,vertex.size=7,vertex.label=NA, vertex.label.dist= 1.5,layout=l, edge.color="grey", edge.width=(E(ponies_micro_dist_cut)$weight)/2)

# this is using community analysis to id groups- but replaced with attribute file above
#gg.com <- fastgreedy.community(ponies_graph)
#V(ponies_graph)$color <- gg.com$membership + 1
#plot.igraph(ponies_graph,vertex.size=7,vertex.label.cex=0.75, vertex.label.dist= 1.5, edge.color="grey",edge.width=(E(ponies_graph)$weight)*5)
```