Hacker News new | ask | show | jobs
by wch 4900 days ago
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"))