Ticket #10218: ptcc.R

File ptcc.R, 1.9 KB (added by dcf, 4 years ago)
Line 
1library(ggplot2)
2library(scales)
3
4x <- read.csv("merged-2016-02-01.csv.xz")
5x <- x[x$node=="bridge" & x$metric=="responses" & x$version=="", ]
6
7# Total responses per fingerprint.
8x.total <- x[x$country=="" & x$transport=="", c("fingerprint", "val")]
9colnames(x.total)[colnames(x.total)=="val"] <- "val_total"
10# Total responses per fingerprint and transport.
11x.transport <- x[x$transport!="", c("fingerprint", "transport", "val")]
12colnames(x.transport)[colnames(x.transport)=="val"] <- "val_transport"
13# Total responses per fingerprint and country.
14x.country <- x[x$country!="", c("fingerprint", "country", "val")]
15colnames(x.country)[colnames(x.country)=="val"] <- "val_country"
16
17tmp <- expand.grid(fingerprint=unique(x.total$fingerprint), country=unique(x.country$country), transport=unique(x.transport$transport))
18tmp <- merge(tmp, x.total, by=c("fingerprint"), all.x=T)
19tmp$val_total[is.na(tmp$val_total)] <- 0
20tmp <- merge(tmp, x.transport, by=c("fingerprint", "transport"), all.x=T)
21tmp$val_transport[is.na(tmp$val_transport)] <- 0
22tmp <- merge(tmp, x.country, by=c("fingerprint", "country"), all.x=T)
23tmp$val_country[is.na(tmp$val_country)] <- 0
24
25tmp$val_lower <- pmax(0, tmp$val_transport + tmp$val_country - tmp$val_total)
26tmp$val_upper <- pmin(tmp$val_transport, tmp$val_country)
27
28# Sum by transport and country.
29x <- aggregate(tmp[, c("val_transport", "val_lower", "val_upper")], list(transport=tmp$transport, country=tmp$country), sum)
30
31for (transport in c("obfs3", "meek", "fte")) {
32        y <- x[x$transport==transport, ]
33        y$country <- factor(y$country, levels=y$country[order(-y$val_upper)])
34
35        p <- ggplot(y, aes(country))
36        p <- p + geom_linerange(aes(ymin=0, ymax=val_transport), color="gray", size=1)
37        p <- p + geom_linerange(aes(ymin=val_lower, ymax=val_upper), color="blue", size=2)
38        p <- p + ylab("responses") + ggtitle(transport)
39        p <- p + theme_minimal()
40        p <- p + coord_flip()
41        ggsave(sprintf("ptcc-%s.png", transport), p, width=5, height=24, dpi=120)
42}