by rnadna · 13y ago · 0 comments
fluPlot <- function(country="ca", regions="Nova.Scotia")
{
url <- sprintf("http://www.google.org/flutrends/intl/en_us/%s/data.txt", country)
d <- read.csv(url, skip=11, header=TRUE)
n <- length(regions)
t <- as.POSIXct(d[["Date"]])
for (i in 1:n) {
if (i == 1) {
plot(t, d[[regions[i]]], xlab="", ylab=regions[i], type='l', col=i)
grid()
} else {
lines(t, d[[regions[i]]], col=i)
}
}
legend("top", col=1:n, lwd=par('lwd'), legend=regions, bg='white')
}
par(mar=c(2, 2, 2, 2), mgp=c(2, 0.7, 0))
fluPlot("ca", c("Ontario", "Nova.Scotia")) # Memoize read.csv so each data set only needs to be downloaded once
require(memoise)
readcsv <- memoise(read.csv)
fluPlot2 <- function(country="ca", regions="Nova.Scotia") {
require(ggplot2)
require(reshape2)
url <- sprintf("http://www.google.org/flutrends/intl/en_us/%s/data.txt", country)
d <- readcsv(url, skip=11, header=TRUE)
d$Date <- as.Date(d$Date)
# Convert to long format
dl <- melt(d, id.vars = "Date", variable.name = "region")
# Select regions of interest
dlsub <- subset(dl, region %in% regions)
ggplot(dlsub, aes(x=Date, y=value, colour=region)) + geom_line() +
theme_bw()
}
fluPlot2("us", c("Minnesota", "California"))You can probably plug in any other area's data. the JS is just in the index.html.