-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_plot.R
More file actions
80 lines (64 loc) · 2.27 KB
/
map_plot.R
File metadata and controls
80 lines (64 loc) · 2.27 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
library(tidyverse)
library(igraph)
library(ggmap)
library(viridis)
##################
# RETRIEVE EDGES
##################
# Get files
friendship <- read.csv("data/friendship.csv", stringsAsFactors=FALSE)
users <- read.csv('data/users.csv', stringsAsFactors=FALSE)
positions <- read.csv("data/positions.csv", stringsAsFactors=FALSE)
# Remove NAs from positions
positions <- positions %>%
filter(!is.na(id)) %>%
select(id, everything())
# Add 'from' user info
friendship <- users %>%
select(id) %>%
inner_join(friendship, by=c('id' = 'from'))
# Filter friendship by known users
friendship <- friendship %>%
inner_join(users %>% select(id),
by=c('to' = 'id'),
suffix=c('.from', '.to')) %>%
rename(id.from = id, id.to = to) %>%
distinct
# Create network object
g <- graph_from_data_frame(friendship, directed = TRUE, vertices = positions)
# Get positions for the edges
edges_for_plot <- friendship %>%
inner_join(positions %>% select(id, lng, lat), by = c('id.from' = 'id')) %>%
rename(x = lng, y = lat) %>%
inner_join(positions %>% select(id, lng, lat), by = c('id.to' = 'id')) %>%
rename(xend = lng, yend = lat) %>%
filter(!((x == xend) & (y == yend))) # Exclude edges between universities of the same cities
###########
# PLOT
###########
# base map
maptheme <- theme(panel.grid = element_blank()) +
theme(axis.text = element_blank()) +
theme(axis.ticks = element_blank()) +
theme(axis.title = element_blank()) +
theme(legend.position = "bottom") +
theme(panel.grid = element_blank()) +
theme(panel.background = element_rect(fill = "#596673")) +
theme(plot.margin = unit(c(0, 0, 0.5, 0), 'cm'))
country_shapes <- geom_polygon(aes(x = long, y = lat, group = group),
data = map_data('world'),
fill = "#CECECE", color = "#515151",
size = 0.15)
mapcoords <- coord_fixed(xlim = c(-150, 180), ylim = c(-55, 80))
ggplot() + country_shapes +
# Draw lines
geom_curve(data=edges_for_plot,
aes(x=x, y=y, xend=xend, yend=yend),
curvature=0.33, alpha=0.2, color='dodgerblue') +
# Draw points
geom_point(data=positions,
aes(x=lng, y=lat),
color=positions$color) +
# Draw map
mapcoords +
maptheme