-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplot_overPass.R
executable file
·133 lines (117 loc) · 6.33 KB
/
plot_overPass.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
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
130
131
132
133
#!/usr/bin/env Rscript
# R-script to process and plot tag usage data world-wide
# usage: ./plot_overPass.R [-i input.csv] [-o output.png|.jpg|.pdf] [--tag1 old=tag] [--tag2 new=tag]
# -i|--input - input file in csv format with header [default or -: stdin]
# -o|--output - output picture file formats: png, jpg, pdf (default: .png with autogenerated name)
# -t|--tag1 - name of subject tag, must match header (default: taken from second-last header value)
# -n|--tag2 - name of tag for comparison, must match header (default: taken from last header value)
# -c|--color - color scheme for the plot: green-red, blue-red, viridis, plasma [GR, BR, V, P] (default: GR)
#
# pipe: ./csv_compare_tags.sh | ./plot_overPass.R
args = commandArgs(trailingOnly = TRUE)
# Load packages
packages = c("tidyverse", "ggspatial", "sf", "rnaturalearth",
"rnaturalearthdata", "rgeos", "cowplot", "optparse", "ggtext")
pack_check <- lapply(packages,
FUN = function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, dependencies = TRUE)
library(x, character.only = TRUE)
}
}
)
# Read options
option_list <- list(
make_option(c("-i", "--input", type="character"),
default = NULL, help = 'Input CSV file with counts',
metavar = 'character'),
make_option(c("-o", "--output", type="character"),
default = paste0("plot_", format(Sys.time(), "%y%m%d_%H%M%S"), ".png"),
help = 'Outputfile for plot (.png, ,jpg, .pdf)',
metavar = 'character'),
make_option(c("-t", "--tag1", type="character"),
default = NULL, help = 'Old tag name',
metavar = 'character'),
make_option(c("-n", "--tag2", type="character"),
default = NULL, help = 'New tag name',
metavar = 'character'),
make_option(c("-c", "--color", type="character"),
default = "GR", help = 'Color scheme for plot (GR, BR, V, P)',
metavar = 'character'))
opt_parser <- OptionParser(option_list = option_list)
opt <- parse_args(opt_parser)
# Get data about countries
world <- ne_countries(scale = "medium", returnclass = "sf")
# Load overpass data
if (opt$input == "-" || is.null(opt$input)) {
overpass <- read_csv(file("stdin"), na = "")
} else {
overpass <- read_csv(opt$input, na = "")
}
# Get names of the tags
if (is_null(opt$tag1)) {
tag1 <- rev(names(overpass))[2]
tag1 <- rlang::sym(tag1)
} else {
tag1 <- rlang::sym(opt$tag1)
}
if (is_null(opt$tag2)) {
tag2 <- rev(names(overpass))[1]
tag2 <- rlang::sym(tag2)
} else {
tag2 <- rlang::sym(opt$tag2)
}
# Calculate persent used
overpass <- overpass %>%
mutate(percent = !!tag1 / (!!tag1 + !!tag2) * 100)
# Merge world with overpass data
plot <- world %>%
left_join(overpass, by = c("iso_a2" = "iso_a2")) %>%
ggplot() +
geom_sf(aes(fill = percent), color = "black", size = 0.1) +
coord_sf(expand = FALSE, ylim = c(-55, 90)) +
scale_y_continuous(breaks = c(-50, 0 , 50)) +
theme_minimal() +
theme( plot.title.position = "plot",
plot.title = element_markdown(size = 11))
if (opt$color == "V"){
plot <- plot +
scale_fill_viridis_c(option = "viridis",
name = "%",
limits = c(0,100)) +
labs(title = paste0("Country preference for <span style = 'color:#DCE319FF;'>**", tag1, "**</span>
vs. <span style = 'color:#482677FF;'>**", tag2, "**</span>"),
caption = Sys.Date())
} else if (opt$color == "P") {
plot <- plot +
scale_fill_viridis_c(option = "plasma",
name = "%",
limits = c(0,100)) +
labs(title = paste0("Country preference for <span style = 'color:#F7E225FF;'>**", tag1, "**</span>
vs. <span style = 'color:#2D0594FF;'>**", tag2, "**</span>"),
caption = Sys.Date())
} else if (opt$color == "BR") {
plot <- plot +
scale_fill_gradient2(name = "%",
low = "dodgerblue3",
mid = "white",
high = "red3",
midpoint = 50,
limits = c(0,100)) +
labs(title = paste0("Country preference for <span style = 'color:red3;'>**", tag1, "**</span>
vs. <span style = 'color:dodgerblue3;'>**", tag2, "**</span>"),
caption = Sys.Date())
} else {
plot <- plot +
scale_fill_gradient2(name = "%",
low = "darkgreen",
mid = "white",
high = "red3",
midpoint = 50,
limits = c(0,100)) +
labs(title = paste0("Country preference for <span style = 'color:red3;'>**", tag1, "**</span>
vs. <span style = 'color:darkgreen;'>**", tag2, "**</span>"),
caption = Sys.Date())
}
# Save output figure
save_plot(opt$output, plot)