-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot-remap.R
70 lines (60 loc) · 1.86 KB
/
plot-remap.R
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
library("dplyr")
if (!require("REmap")) devtools::install_github("lchiffon/REmap")
library("REmap")
dat <- read.csv("https://raw.githubusercontent.com/nanxstats/tea-sea-cha-land/master/tea-sea-cha-land.csv")
# remove missing data
rawDat <- dat %>% na.omit()
markPoint <- rawDat %>% select(id, value)
markPoint$color <- ifelse(
markPoint$value == 1, "red",
ifelse(markPoint$value == 2, "blue", "green")
)
geoData <- rawDat %>% select(longitude, latitude, id)
# first plot: without paths
data <- data.frame(country = mapNames("world"), value = 5 * sample(178) + 200)
head(data)
out <- remapC(
data,
maptype = "world", color = "white",
markPointTheme = markPointControl(
effect = FALSE, symbol = "pin", symbolSize = 5
),
markPointData = markPoint,
geoData = geoData
)
plot(out)
# second plot: with computed paths
# logic for computing the possible tea trade paths:
# type should be the same; year of the starting point
# is smaller than that of the ending point; with the shortest path
output <- list()
for (i in 1:nrow(rawDat)) {
tmpLine <- rawDat[i, ]
if (tmpLine$value == 3) next
tmpDat <- rawDat %>%
filter(value == tmpLine$value & year < tmpLine$year) %>%
arrange((latitude - tmpLine$latitude)^2 + (longitude - tmpLine$longitude)^2)
if (nrow(tmpDat) > 0) {
output <- append(output, list(c(
ori = as.character(tmpDat[1, ]$id),
des = as.character(tmpLine$id),
value = tmpLine$value
)))
}
}
markLineDat <- output %>% do.call(rbind, .) %>% as.data.frame()
markLineDat$color <- ifelse(
markLineDat$value == 1, "red",
ifelse(markLineDat$value == 2, "blue", "green")
)
out <- remapB(
color = "Blue",
markPointTheme = markPointControl(
effect = FALSE, symbol = "pin", symbolSize = 5
),
markPointData = markPoint,
markLineData = markLineDat,
markLineTheme = markLineControl(lineWidth = 2),
geoData = geoData
)
plot(out)