Skip to content

Instantly share code, notes, and snippets.

@bobthecat
Last active October 7, 2015 20:18
Show Gist options
  • Save bobthecat/3219574 to your computer and use it in GitHub Desktop.
Save bobthecat/3219574 to your computer and use it in GitHub Desktop.
twitter air pollution
library(twitteR)
library(ggplot2)
library(grid)
# download all that you can
pol <- userTimeline('BeijingAir', n=3200)
length(pol)
# 3200
myGrep <- function(x){
grep("PM2.5 24hr avg;", x$getText(), value=T)
}
POL <- unlist(lapply(pol, myGrep))
# cleaning no data tweets
POL <- POL[-grep("No Data", POL)]
# uncomment the following to combine with previous extract
# allPM <- unique(c(allPM, POL))
allPM <- POL
time <- sub("^(.*) to .*", "\\1", allPM)
# to posix time
time <- strptime(time, format="%m-%d-%Y %R")
PM <- as.numeric(sub("^.* 24hr avg; (.*); .*; .*", "\\1", allPM, perl=T))
data <- data.frame(PM=PM, time=time)
data <- data[order(data$time),]
yrange <- c(25, 75, 125, 175, 250, 400)
tsize=4
textPos <- as.POSIXct(strsplit(as.character(min(data$time)), " ")[[1]][1])
p <- qplot(time, PM, data=data, geom=c("blank"), group=1)
p +
labs(x = "Time", y = "Fine particles (PM2.5) 24hr avg", size = expression(log[10](pval))) +
opts(title="Air pollution in Beijing\nTwitter @BeijingAir", panel.background=theme_rect(colour="white")) +
geom_hline(aes(yintercept=50), colour="green", alpha=I(1/5), size=2) +
geom_hline(aes(yintercept=100), colour="yellow", alpha=I(1/5), size=2) +
geom_hline(aes(yintercept=150), colour="orange", alpha=I(1/5), size=2) +
geom_hline(aes(yintercept=200), colour="red", alpha=I(1/5), size=2) +
geom_hline(aes(yintercept=300), colour="darkred", alpha=I(1/5), size=2) +
geom_path(aes(time, PM), data=data, group=1) +
annotate("text", x=textPos, y=yrange[1], label="good", size=tsize, colour="grey70") +
annotate("text", x=textPos, y=yrange[2], label="moderate", size=tsize, colour="grey70") +
annotate("text", x=textPos, y=yrange[3], label="unhealthy", size=tsize, colour="grey70") +
annotate("text", x=textPos, y=yrange[4], label="unhealthy +", size=tsize, colour="grey70") +
annotate("text", x=textPos, y=yrange[5], label="unhealthy ++", size=tsize, colour="grey70") +
annotate("text", x=textPos, y=yrange[6], label="hazardous", size=tsize, colour="grey70") +
opts(title="Air pollution in Beijing\nTwitter @BeijingAir",
panel.background=theme_rect(colour="white"))
ggsave(filename="twitter_pol.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment