Flu Trends
[This article was first published on mickeymousemodels, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Not a model, but certainly Mickey Mousey: here’s some R code that plots Google’s US flu data:Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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)
savePlot("US_flu_trends.png")
Huzzah!
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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.