-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot_tagHistory.R
executable file
·135 lines (113 loc) · 6.79 KB
/
plot_tagHistory.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
134
135
#!/usr/bin/env Rscript
# R-script to process and plot tag history
# usage: ./plot_tagHistory.R -i input.csv [-j input2.csv] [-o output.png|.jpg|.pdf] [--tag1 tag_name] [--tag2 tag_name]
# -i|--input1 - input file for first tag in csv format with header and four columns containing date, nodes, ways, relations
# -j|--input2 - input file for sevond tag in csv format with header and four columns containing date, nodes, ways, relations
# -o|--output - output picture file formats: png, jpg, pdf (default: .png with autogenerated name)
# -t|--tag1 - name of first tag to display in plot title
# -u|--tag2 - name of second tag to display in plot title
# -w|--binwidth - width of the time interval (week, month, quarter, year)
#
args = commandArgs(trailingOnly = TRUE)
# Load packages
packages = c("tidyverse", "cowplot", "optparse", "ggtext", "lubridate", "scales")
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", "--input1", type="character"),
default = NULL, help = 'Input CSV file for tag1',
metavar = 'character'),
make_option(c("-j", "--input2", type="character"),
default = NULL, help = 'Input CSV file for tag2',
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 = "tag1", help = 'tag1 name',
metavar = 'character'),
make_option(c("-g", "--tag2", type="character"),
default = "tag2", help = 'tag2 name',
metavar = 'character'),
make_option(c("-w", "--binwidth", type="character"),
default = "year", help = 'Binning of timeline (week, month, quarter, year)',
metavar = 'character'))
opt_parser <- OptionParser(option_list = option_list)
opt <- parse_args(opt_parser)
# Load overpass data and calculate total
overpass1 <- read_csv(opt$input1, na = "")
if (!is.null(opt$input2)) {
overpass2 <- read_csv(opt$input2, na = "")
overpass1 <- bind_rows("OP1" = overpass1, "OP2" = overpass2, .id = "OP")
#overpass1 <- left_join(overpass1, overpass2, by = c("@date" = "@date"))
}
# Calculate total
overpass1 <- overpass1 %>% mutate(rate = `@nodes` + `@ways` + `@relations`)
# Bin data
if (opt$binwidth == "year") {
overpass1 <- overpass1 %>% mutate(timebin = year(`@date`))
} else if (opt$binwidth == "quarter") {
overpass1 <- overpass1 %>% mutate(timebin = as.numeric(year(`@date`)) + as.numeric(quarter(`@date`))/4)
} else if (opt$binwidth == "month") {
overpass1 <- overpass1 %>% mutate(timebin = as.numeric(year(`@date`)) + as.numeric(month(`@date`))/12)
} else if (opt$binwidth == "week") {
overpass1 <- overpass1 %>% mutate(timebin = as.numeric(year(`@date`)) + as.numeric(week(`@date`))/52)
} else {
overpass1 <- overpass1 %>% mutate(timebin = `@date`)
}
# Plot change in object numbers
if (is.null(opt$input2)) {
plot <- overpass1 %>%
group_by(timebin) %>%
summarize(rate = sum(rate)) %>%
ggplot() +
geom_bar(aes(x = timebin, y = rate), stat = "identity", fill = "deeppink3") +
theme_half_open() +
background_grid(major = "xy",
color.major = "grey90") +
scale_y_continuous(breaks = breaks_extended(5),
labels = label_comma()) +
labs(title = paste0("Change in ", opt$tag1, " objects per ", opt$binwidth),
caption = Sys.Date()) +
ylab(paste0("Objects/",opt$binwidth)) +
xlab(NULL)
}
# If two tags were provided calculate whether the chage was towards tag1 (positive values) or tag2 (negative values)
if (!is.null(opt$input2)) {
plot <- overpass1 %>%
select(timebin, rate, OP) %>%
group_by(timebin, OP) %>%
summarize(rate = sum(rate)) %>%
pivot_wider(names_from = OP, values_from = rate) %>%
replace_na(list(OP1 = 0, OP2 = 0)) %>%
mutate(change = OP1 - OP2,
preference = if_else(change > 0 , "+", "-")) %>%
ggplot() +
geom_bar(aes(x = timebin, y = change, fill = preference), stat = "identity") +
theme_half_open() +
#theme_classic() +
background_grid(major = "xy",
color.major = "grey90") +
scale_y_continuous(breaks = breaks_extended(5),
labels = label_comma()) +
labs(title = paste0("Preference for <span style = 'color:#00C2C6;'>**", opt$tag1,
"**</span> vs. <span style = 'color:#FF6C67;'>**", opt$tag2, "**</span> per ", opt$binwidth),
caption = Sys.Date()) +
ylab(paste0("Objects/",opt$binwidth)) +
xlab(NULL) +
theme(plot.title.position = "plot",
plot.title = element_markdown(size = 14),
legend.position = "none")
}
# Save output figure
#save_plot(opt$output, plot)
#ggsave(opt$output, plot, height = 3.71, width = 6)
ggsave(opt$output, plot, bg = "white", height = 4.5, width = 6.5)