|
#!/usr/bin/RScript |
|
require(ggplot2) |
|
require(reshape2) |
|
require(gtable) |
|
require(grid) |
|
|
|
rm(list=ls()) |
|
#https://www.netmarketshare.com/operating-system-market-share.aspx?qprid=9&qpcustomb=0&qpsp=197&qpnp=24&qptimeframe=M |
|
#https://www.netmarketshare.com/browser-market-share.aspx?qprid=1&qpcustomb=0&qpsp=197&qpnp=24&qptimeframe=M |
|
#select the time range as wide as you want and then export to csv either as `osshare.csv` or `browsershare.csv` to your WD. |
|
|
|
os.meta <- list(file="osshare.csv", |
|
drop.list=c("FreeBSD", "OpenBSD", "Other"), |
|
incl.list=c("Linux", "Mac", "Windows"), |
|
title.plot="Share of major operating systems\nnetmarketshare.com", |
|
ofile="operating-system") |
|
browser.meta <- list(file="browsershare.csv", |
|
drop.list=c("Other"), |
|
incl.list=c("Chrome", "Internet.Explorer", "Firefox", |
|
"Microsoft.Edge", "Safari"), |
|
title.plot="Share of major web browsers\nnetmarketshare.com", |
|
ofile="browser") |
|
unit.meta <- os.meta #switch between `browser.meta` or `os.meta`, depending on what you want to visualize |
|
data <- read.csv(unit.meta$file, header=T, sep=",") |
|
data$Month <- as.Date(paste(as.character(data$Month), "01"), format="%B, %Y %d") |
|
|
|
logit <- function(x) {return(log(x/(1-x)))} |
|
logit.inv <- function(x) {return(1/(1+exp(-x)))} |
|
logit.inv.percent <- function(x) {return(paste(round(100*logit.inv(x), 2), |
|
"%", sep=""))} |
|
|
|
for (os in unit.meta$drop.list) { |
|
data[[os]] <- NULL |
|
} |
|
|
|
for (os in unit.meta$incl.list) { |
|
loop.tmp <- data[[os]] |
|
loop.tmp <- as.character(loop.tmp) |
|
loop.tmp <- sub("%", "", loop.tmp) |
|
data[[os]] <- as.numeric(loop.tmp)/100 |
|
} |
|
data.diff <- data |
|
|
|
#logit |
|
for (os in unit.meta$incl.list) { |
|
data[[os]] <- logit(data[[os]]) |
|
data.diff[[os]] <- c(NA, diff(filter(data[[os]], filter=c(0.222,0.333,0.444), sides=1))) |
|
} |
|
|
|
#plot |
|
data.melt <- melt(data, id="Month") |
|
data.diff.melt <- melt(data.diff, id="Month") |
|
plot <- ggplot(data=data.melt, aes(x=Month, y=value, group=variable |
|
, color=variable)) + |
|
geom_line() + |
|
scale_y_continuous(labels=logit.inv.percent, breaks=c(-5,-4,-3,-2,-1,0, |
|
1,2,3,4,5)) + |
|
labs(y="share (logit scale)", |
|
title=unit.meta$title.plot, |
|
color="OS") + |
|
theme_minimal() |
|
#plot.diff |
|
plot.diff <- ggplot(data=data.diff.melt, aes(x=Month, y=value, group=variable |
|
, color=variable)) + |
|
geom_line() + |
|
labs(y="share - 1st difference of smoothed logits\nwith kernel [0.22,0.33,0.44]", |
|
title=unit.meta$title.plot, |
|
color="OS") + |
|
theme_minimal() |
|
plot.grob <- ggplotGrob(plot) |
|
plot.diff.grob <- ggplotGrob(plot.diff) |
|
grob <- rbind(plot.grob, plot.diff.grob, size="first") |
|
grid.newpage() |
|
png(paste(unit.meta$ofile, "-", format(Sys.time(), "%Y-%m-%dT%H-%M-%S"), ".png", sep=""), width=700, height=700) |
|
print(grid.draw(grob)) |
|
dev.off() |
|
grid.draw(grob) |