-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapa_visualization.R
More file actions
129 lines (84 loc) · 3.58 KB
/
Mapa_visualization.R
File metadata and controls
129 lines (84 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
## graficar el network espacialmente
## siguiendo el siguiente site: http://kateto.net/network-visualization
## https://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html
library(ggplot2)
library(ggmap)
library(maps)
library(mapdata)
library('geosphere')
library(igraph)
library(sf)#for using spatial objects
library(tidyverse)#for using tidy syntax
library(tmap)#for visualizing maps
library(spData)
library(geobr)
library(ggrepel)
library(stplanr) #for using od2line function to convert points to lines
##### Read data
nodes.mpa <- read.csv("Nodos_MPAs.csv", header=T, as.is=T)
edges.mpa.lo <- read.csv("EDGES_MPA_revLOrena.csv", header=T, as.is=T)
##### Build graph from dataframe
net_mpa <- graph_from_data_frame( d = edges.mpa.lo, vertices = nodes.mpa, directed = FALSE )
summary(net_mpa)
# Download coastline
coast <- ne_download(scale = 10, type = "coastline", category = "physical", returnclass = "sf")
# Download country boundaries
countries <- ne_countries(scale = "medium", returnclass = "sf") %>%
filter(admin %in% c("Brazil", "Uruguay", "Argentina"))
# Crop global coastline to bounding box of selected countries
bbox_south_atlantic <- st_bbox(countries) %>% st_as_sfc()
coast_cropped <- st_intersection(coast, bbox_south_atlantic)
## construyo el geompoint para cada MPA
MPASpatial = nodes.mpa %>%
st_as_sf(coords=c("Longitud", "Latitude"), crs = "EPSG: 4326")
head(MPASpatial, 3)
# create line geometry
MPAEdges_toLine = od2line(edges.mpa.lo, MPASpatial)
MPAEdges_toLine[c(1:3),]
## Mpa Edges
#Construct a network and calculate the degree for each node.
MPASpatial$degree = degree(
net_mpa,
v = V(net_mpa),
mode = c("all"),
loops = FALSE,
normalized = TRUE
)
# create a line weight column based on edge distance
MPAEdges_toLine = MPAEdges_toLine %>%
mutate(weight = as.numeric(st_length(geometry)))
# Mapa completo
p_full <- ggplot() +
geom_sf(data = coast_cropped, color = "gray60") +
geom_sf(data = countries, fill = "gray95", color = "gray70") +
geom_sf(data = MPAEdges_toLine, color = "black", alpha = 0.5) +
geom_sf(data = MPASpatial, aes(fill = Country), shape = 21, size = 2, stroke = 0.2) +
scale_color_brewer(palette = "Dark2") +
scale_fill_manual(values = c("Brazil" = "green", "Uruguay" = "deepskyblue", "Argentina" = "orange")) +
theme_minimal() +
labs(title = "MPA Network - All Species", color = "Specie", fill = "Country")
# Guardar
ggsave("outputs/map_full_network.png", p_full, width = 10, height = 8)
library(purrr)
# Obtener lista de especies únicas
species_list <- unique(MPAEdges_toLine$Specie)
# Función para graficar por especie
plot_by_species <- function(sp) {
edges_sp <- MPAEdges_toLine %>% filter(Specie == sp)
# Filtrar nodos conectados
node_ids <- unique(c(edges_sp$from, edges_sp$to))
nodes_sp <- MPASpatial %>% filter(ID %in% node_ids)
# Crear plot
p_sp <- ggplot() +
geom_sf(data = coast_cropped, color = "gray60") +
geom_sf(data = countries, fill = "gray95", color = "gray70") +
geom_sf(data = edges_sp, color = "black", alpha = 0.6) +
geom_sf(data = nodes_sp, aes(fill = Country), shape = 21, size = 2, stroke = 0.2) +
scale_fill_manual(values = c("Brazil" = "green", "Uruguay" = "deepskyblue", "Argentina" = "orange")) +
theme_minimal() +
labs(title = paste("MPA Network -", sp), fill = "Country")
# Guardar
ggsave(paste0("outputs/map_", sp, ".png"), p_sp, width = 8, height = 6)
}
# Aplicar a cada especie
walk(species_list, plot_by_species)