Flu Trends

April 18, 2011

(This article was first published on mickeymousemodels, and kindly contributed to R-bloggers)

Not a model, but certainly Mickey Mousey: here’s some R code that plots Google’s US flu data:

df <- read.csv(url("http://www.google.org/flutrends/us/data.txt"), skip=11)

df$Date <- as.Date(df$Date)

dev.new(height=8, width=12)

# Leave a thin outer margin

par(oma=c(0.5, 0.5, 0.5, 0.5))

# Plot data; suppress x-axis

plot(df$Date, df$United.States, main="US Flu Trends", ylab="Measure of US flu-related queries", xlab="", xaxt="n", xlim=(range(df$Date) + c(-60, +60)), col="tomato", type="l", lwd=2)

# Add x-axis with yearly tick marks

years.to.tick <- as.Date(sprintf("%s-01-01", (format(min(df$Date), "%Y"):format(max(df$Date), "%Y"))[-1]))

axis(1, at=years.to.tick, labels=rep("", length(years.to.tick)))

# Add x-axis with no ticks, but centered text labels every other year

years.to.label <- years.to.tick[1:(length(years.to.tick) - 1)][c(TRUE, FALSE)]

axis(1, at=(years.to.label + 365/2), labels=format(years.to.label, "%Y"), tck=0)

mtext("Data Source: Google Flu Trends (http://www.google.org/flutrends)")

mtext(sprintf("Plotted on %s", Sys.Date()), side=1, line=3, cex=0.75)



To leave a comment for the author, please follow the link and comment on their blog: mickeymousemodels.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)