Total Pageviews

Wednesday 3 February 2016

Mosquitos and Average Temperature plot using R

#setwd("../working")
library(data.table)
library(ggplot2)
library(lubridate)
dataFolder = "../input"
dtTrain = fread(file.path(dataFolder,"train.csv"))
weather = fread(file.path(dataFolder, "weather.csv"))

dtTrain[,Date:=as.Date(Date)]
dtTrain[,':=' (year=year(Date), dayOfYear=yday(Date))]

weather[Tavg=='M', Tavg:='-1']
weather[,':=' (Date=as.Date(Date),Tmax=as.integer(Tmax),Tmin=as.integer(Tmin),Tavg=as.integer(Tavg))]
weather[, ':=' (year=year(Date), dayOfYear=yday(Date))]
weather[Tavg==-1,Tavg:=as.integer((Tmax+Tmin)/2)]

mosquitosStats<-dtTrain[,.(dayOfYear,year,NumMosquitos.sum=sum(NumMosquitos)),by=Date]
#Total mosquitos by Date
log_scale_mosquitos<-ggplot(mosquitosStats)+geom_point(aes(dayOfYear, log(NumMosquitos.sum),color=NumMosquitos.sum))+
  facet_grid(year ~ .)+
  scale_color_gradient(low="blue", high="Red")+
  ggtitle("Total mosquitos by Day")
ggsave("log_scale_mosquitos.png", log_scale_mosquitos)

mosquitosSum<-dtTrain[,.(NumMosquitos.sum=sum(NumMosquitos)),by=Date]

weatherTrain<-weather[Station==1&year%%2==1,]
mosquitosByDate<-merge(weatherTrain,mosquitosSum,by="Date",all.x=TRUE)
mosquitosByDate[is.na(NumMosquitos.sum), NumMosquitos.sum:=0]
#put mosquitos count and temperature into one plot,scle mosquitos count and Tavg to fit a similar range
mosquitos_temperature_plot <- ggplot(mosquitosByDate)+geom_point(aes(dayOfYear, Tavg/10,color=Tavg))+
  geom_line(aes(dayOfYear, log(NumMosquitos.sum)),color="olivedrab")+
  facet_grid(year ~ .)+
  scale_color_gradient(low="blue", high="Red")+
  ggtitle("log(NumMosquitos.sum) and Tavg plot")
ggsave("mosquitos_temperature_plot.png",mosquitos_temperature_plot)


                
This script has been released under the Apache 2.0 open source license.



Loading required package: methods

Attaching package: ‘lubridate’

The following objects are masked from ‘package:data.table’:

    hour, mday, month, quarter, wday, week, yday, year

Saving 12.5 x 6.67 in image
Saving 12.5 x 6.67 in image

No comments:

Post a Comment