Two very interesting packages in R for loading weather data into R:
Here is the github for the R package weatherData https://ram-n.github.io/weatherData/.
Lets give these a try and plot some visualizations of the data using the package ggplot2.
We are going to compare the weather in Seattle and San Francisco. The data is collected from weatherunderground at airports.
First we load the packages.
library(weatherData)
library(weathermetrics)
library(ggplot2)
Then we check to see if the data we are interested in is available. We can check for one day. Using Seattle as an example.
data_okay <- checkDataAvailability("SEA", "2016-02-1")
## Getting data from:
## http://www.wunderground.com/history/airport/SEA/2016/2/1/DailyHistory.html?format=1
## The following columns are available for:2016-02-1
## [1] "TimePST" "TemperatureF" "Dew_PointF"
## [4] "Humidity" "Sea_Level_PressureIn" "VisibilityMPH"
## [7] "Wind_Direction" "Wind_SpeedMPH" "Gust_SpeedMPH"
## [10] "PrecipitationIn" "Events" "Conditions"
## [13] "WindDirDegrees" "DateUTC"
## Checking Data Availability For SEA
## Found Records for 2016-02-1
## Data is Available
And we can check for a period of time.
data_okay <- checkSummarizedDataAvailability("SEA","2015-02-01","2016-02-01")
## Retrieving from: http://www.wunderground.com/history/airport/SEA/2015/2/1/CustomHistory.html?dayend=1&monthend=2&yearend=2016&req_city=NA&req_state=NA&req_statename=NA&format=1
## The following columns are available:
## [1] "PST" "Max_TemperatureF"
## [3] "Mean_TemperatureF" "Min_TemperatureF"
## [5] "Max_Dew_PointF" "MeanDew_PointF"
## [7] "Min_DewpointF" "Max_Humidity"
## [9] "Mean_Humidity" "Min_Humidity"
## [11] "Max_Sea_Level_PressureIn" "Mean_Sea_Level_PressureIn"
## [13] "Min_Sea_Level_PressureIn" "Max_VisibilityMiles"
## [15] "Mean_VisibilityMiles" "Min_VisibilityMiles"
## [17] "Max_Wind_SpeedMPH" "Mean_Wind_SpeedMPH"
## [19] "Max_Gust_SpeedMPH" "PrecipitationIn"
## [21] "CloudCover" "Events"
## [23] "WindDirDegrees"
## Checking Summarized Data Availability For SEA
## Found 366 records for 2015-02-01 to 2016-02-01
## Data is Available for the interval.
An interesting function gets the current data.
getCurrentTemperature(station_id = "SEA")
## Time TemperatureF
## 15 2016-02-23 11:53:00 55.9
Now lets compare Seattle with San Francisco. What is the current temperature in San Franisco?
getCurrentTemperature(station_id = "SFO")
## Time TemperatureF
## 12 2016-02-23 11:56:00 62.1
Suppose we are interested in looking at wind direction and wind speed. In Seattle,
wSEA <- getDetailedWeather("SEA", "2016-02-1", opt_all_columns=T)
attach(wSEA)
ggplot(data = wSEA, aes(x=Wind_SpeedMPH, y=WindDirDegrees, color=Wind_Direction)) + geom_point(size=3) + labs(title="Seattle Wind")
In San Fransisco,
wSFO <- getDetailedWeather("SFO", "2016-02-1", opt_all_columns=T)
attach(wSFO)
## The following objects are masked from wSEA:
##
## Conditions, DateUTC, Dew_PointF, Events, Gust_SpeedMPH,
## Humidity, PrecipitationIn, Sea_Level_PressureIn, TemperatureF,
## Time, TimePST, VisibilityMPH, WindDirDegrees, Wind_Direction,
## Wind_SpeedMPH
ggplot(data = wSFO, aes(x=Wind_SpeedMPH, y=WindDirDegrees, color=Wind_Direction)) + geom_point(size=3) + labs(title="San Fransisco Wind")
Let’s look at the tempature in Seattle over the last year and compare it to the data in San Fransisco over the same time period.
From the github page for the R package weatherdata there are two very useful functions for comparing temperatures at two locations.
city1 <- "SEA"
city2 <- "SFO"
df1 <- getWeatherForYear(city1, 2015)
## Retrieving from: http://www.wunderground.com/history/airport/SEA/2015/1/1/CustomHistory.html?dayend=31&monthend=12&yearend=2015&req_city=NA&req_state=NA&req_statename=NA&format=1
## The following columns are available:
## [1] "PST" "Max_TemperatureF"
## [3] "Mean_TemperatureF" "Min_TemperatureF"
## [5] "Max_Dew_PointF" "MeanDew_PointF"
## [7] "Min_DewpointF" "Max_Humidity"
## [9] "Mean_Humidity" "Min_Humidity"
## [11] "Max_Sea_Level_PressureIn" "Mean_Sea_Level_PressureIn"
## [13] "Min_Sea_Level_PressureIn" "Max_VisibilityMiles"
## [15] "Mean_VisibilityMiles" "Min_VisibilityMiles"
## [17] "Max_Wind_SpeedMPH" "Mean_Wind_SpeedMPH"
## [19] "Max_Gust_SpeedMPH" "PrecipitationIn"
## [21] "CloudCover" "Events"
## [23] "WindDirDegrees"
## Checking Summarized Data Availability For SEA
## Found 365 records for 2015-01-01 to 2015-12-31
## Data is Available for the interval.
## Will be fetching these Columns:
## [1] "Date" "Max_TemperatureF" "Mean_TemperatureF"
## [4] "Min_TemperatureF"
df2 <- getWeatherForYear(city2, 2015)
## Retrieving from: http://www.wunderground.com/history/airport/SFO/2015/1/1/CustomHistory.html?dayend=31&monthend=12&yearend=2015&req_city=NA&req_state=NA&req_statename=NA&format=1
## The following columns are available:
## [1] "PST" "Max_TemperatureF"
## [3] "Mean_TemperatureF" "Min_TemperatureF"
## [5] "Max_Dew_PointF" "MeanDew_PointF"
## [7] "Min_DewpointF" "Max_Humidity"
## [9] "Mean_Humidity" "Min_Humidity"
## [11] "Max_Sea_Level_PressureIn" "Mean_Sea_Level_PressureIn"
## [13] "Min_Sea_Level_PressureIn" "Max_VisibilityMiles"
## [15] "Mean_VisibilityMiles" "Min_VisibilityMiles"
## [17] "Max_Wind_SpeedMPH" "Mean_Wind_SpeedMPH"
## [19] "Max_Gust_SpeedMPH" "PrecipitationIn"
## [21] "CloudCover" "Events"
## [23] "WindDirDegrees"
## Checking Summarized Data Availability For SFO
## Found 365 records for 2015-01-01 to 2015-12-31
## Data is Available for the interval.
## Will be fetching these Columns:
## [1] "Date" "Max_TemperatureF" "Mean_TemperatureF"
## [4] "Min_TemperatureF"
getDailyDifferences <- function(df1, df2){
Delta_Means <- df1$Mean_TemperatureF - df2$Mean_TemperatureF
Delta_Max <- df1$Max_TemperatureF - df2$Max_TemperatureF
Delta_Min <- df1$Min_TemperatureF - df2$Min_TemperatureF
diff_df <- data.frame(Date=df1$Date, Delta_Means, Delta_Max, Delta_Min)
return(diff_df)
}
plotDifferences <- function (differences, city1, city2) {
library(reshape2)
m.diff <- melt(differences, id.vars=c("Date"))
p <- ggplot(m.diff, aes(x=Date, y=value)) + geom_point(aes(color=variable)) +
facet_grid(variable ~ .) +geom_hline(yintercept=0)
p <- p + labs(title=paste0("Daily Temperature Differences: ", city1, " minus ",city2))
print(p)
}
To plot
differences<- getDailyDifferences(df1, df2)
plotDifferences(differences, city1, city2)
What month(s) of the year is Seattle warmer than San Fransisco?
What month(s) of the year is Seattle colder than San Fransisco?