Hacker News new | ask | show | jobs
by rnadna 4902 days ago
In case it's of any interest, below is code in the R language that produces time-series graphs for two provinces in Canada.

    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"))
2 comments

Nice! I've adapted your code and made a version that uses ggplot2. It also uses memoise() so that each data set only needs to be downloaded once per R session.

    # 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"))
I made a year-by-year line overlay for the Japanese data.

You can probably plug in any other area's data. the JS is just in the index.html.

http://poyo.co/d3stuff/flu/