knitr::opts_chunk$set(fig.width=6, fig.height=6, collapse = TRUE, comment = "#>" )
Installation of the package:
library(weatherstack)
Installing some other useful librares for plotting purposes.
library(ggplot2) library(hrbrthemes)
Calling the timeSeries function, retrieves the weather data between two specified days from the API. I want to retrieve all the weather data for Kelowna BC for the month of January 2015.
data <- weatherstack_timeSeries('ba435151893f4b833c9b27ca6f28044f',"Kelowna","2015-01-01","2015-01-31") head(data)
Compiling our retrieved data into a ggplot graph. I want to visualize the temperature trends, minimum maximum and average temperatures.
colors <- c('data$mintemp' = "#00AFBB", 'data$maxtemp' = "red", 'data$avgtemp' = "orange") ggplot(data = data, aes(x = data$date, y = data$mintemp, group = 1))+ geom_line(color = "#00AFBB", size = 2)+ xlab("") + expand_limits(y = c(-10, 10))+ ylab("Temperature (Celcius)")+ ggtitle("Temperature in Kelowna, BC - January 2015")+ theme(axis.text.x=element_text(angle=90, hjust=1))+ geom_line(data = data, aes(x = data$date, y = data$maxtemp), color = "red", size = 2)+ geom_line(data = data, aes(x = data$date, y = data$avgtemp), color = "orange")
The lookup function is useful to gather locations based on name. For example, I want to know information for all locations with query 'London' in the world.
data_lookup <- weatherstack_Lookup("ba435151893f4b833c9b27ca6f28044f","London") data_lookup
Now I can plot current and historical weather for the location I looked up.
data_history <- weatherstack_historical("ba435151893f4b833c9b27ca6f28044f","London","2015-02-06", "m") data_current <- weatherstack_current("ba435151893f4b833c9b27ca6f28044f","London", "m") history <- (data_history[30:43]) history <- as.list(history) temp <- as.numeric(history[11]) date <- as.character(history[1]) current <- as.list(data_current) temp_current <- as.numeric(current[15]) date_current <- as.character(current[11]) df <- data.frame(date=c(date, date_current),temp=c(temp, temp_current)) ggplot(df, aes(x=df$date, y=df$temp)) + geom_bar(stat = "identity")+ xlab("Date") + ylab("Average temperature (Celcius)")+ ggtitle("Temperature in London, UK, 2015 and Today")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.