-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_graph.R
executable file
·71 lines (49 loc) · 2.22 KB
/
draw_graph.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
#!/usr/bin/Rscript
"Usage: draw_graph.R --csv_file <csv_file_path> --dest_dir <dest_dir_path>
Options:
draw_graph.R --csv_file
draw_graph.R --dest_dir" -> doc
require("ggplot2")
require("data.table")
require("docopt")
opts <- docopt(doc)
# Cairo is optional
#require("Cairo")
#Cairo()
csv_path = opts[["csv_file_path"]]
dest_dir = opts[["dest_dir_path"]]
if(!file.exists(csv_path)) {
print("no such csv file!")
quit()
}
if(!dir.exists(dest_dir)) {
print("no such output path!")
quit()
}
data = fread(csv_path)
for (x in 0:(nrow(data)-1)) {data$hours_end[x+1] = data$hours[(x+1)%%nrow(data)+1] }
weekdays_en <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
data <- data[order(factor(weekday, levels = weekdays_en),hours),]
data$timeline <- paste0(data$hours, " - ", data$hours_end, " ", data$weekday)
freq_table <- data[, list(median(freq), quantile(freq, .25), quantile(freq, .75)), by="timeline"]
setnames(freq_table, c("timeline", "median", "p25", "p75"))
freq_table$timeline <- factor(freq_table$timeline, levels=freq_table$timeline) # preserve order
max_y <- max(freq_table$p75)
polar_plot <- ggplot(freq_table) + theme_bw() + geom_blank() +
coord_polar(start=pi/28, direction=1) +
labs(x="Median ± 25 %") +
theme(axis.title.x=element_text(family = "Georgia", color="#303030", face="italic", size=12, hjust=0),
axis.text.x=element_blank(),
axis.title.y=element_blank(),
panel.grid.major = element_line(colour = "#0000FF50"),
plot.title = element_text(family = "Georgia", color="#000000", size=18)) +
geom_area(aes(x=timeline, y=p75, group=1), fill="#FF000040") +
geom_line(aes(x=timeline, y=median, group=1), size=1, color="#FF0000FF") +
geom_area(aes(x=timeline, y=p25, group=1), fill="#FFFFFFFF") +
geom_vline(xintercept=c(0, 4, 8, 12, 16, 20, 24)) +
annotate("text",label = weekdays_en, x = c(2,6,10,14,18,22,26), y=rep(max_y*.9,7)) +
scale_y_sqrt(limits=c(0,max_y)) +
ggtitle(paste0("Helsinki Hacklab flickr upload activity\n",min(data$date_taken), " — ", max(data$date_taken)))
image_filename <- paste0(dest_dir, min(data$date_taken),"_",max(data$date_taken),"_stats.png")
ggsave(file=image_filename)
#ggsave(file=image_filename, type="cairo-png")