[This article was first published on RClimate, 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.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The weatherData package, link , makes it very easy to retrieve detailed weather data from hundreds of stations across the US. I developed this script to retrieve and plot daily maximum and minimum temperatures and highlight days with 90+ max temps and 75+ minimum temps. Here’s the results of my script:
The user can specify the desired weather station and can change to the year to examine earlier times or the current year.
Here is the R script:
## Hot Days and nights this summer
## use weatherData package to retrieve Max & Min temperature data for summer months
## User can specify airport code, query will use this code to retrieve current yrs data
library(weatherData)
hot_days <- numeric()
hot_nights <- numeric()
### Enter airport Code and year ##############
airport <- "KPNE"
my_yr <- 2016
##############################################
my_start <- paste(my_yr,"-05-01",sep="")
## calc current date for data query
end_date <- format(Sys.Date() -1, "-%m-%d")
my_end <- paste(my_yr,end_date,sep="")
#### Retrieve data from Weather Underground
df <- getWeatherForDate(airport,start_date=my_start,end_date= my_end,opt_detailed=F, opt_all_columns=F)
hot_df <- subset(df, df$Max_TemperatureF>89)
hot_days <- nrow(hot_df)
hot_nights_df <- subset(df, df$Min_TemperatureF>75)
hot_nights <- nrow(hot_nights_df)
hot_day_note <- paste(hot_days, " days with 90+ temp through ",format(Sys.Date()-1,"%m/%d"), sep="")
hot_nights_note <- paste(hot_nights, " nights with 75+ temp")
y_lab <- expression(paste("Temperature - ", degree, "F"),sep="")
title <- paste("Hot Days and Nights This Summer \nMax & Min Temperatures @ ",airport, "\n ",my_yr)
## Use plot function to allow both png file and console output of plot
plot_func <- function() {
par(las=1)
plot(df$Date, df$Max_TemperatureF,type="l", ylim =c(50,100),main=title,
xlab="", ylab=y_lab)
abline(h=90, col="red")
abline(h = 75, col = "blue")
points(hot_df$Date, hot_df$Max_TemperatureF, type="p",pch=19, col = "red")
text (df$Date[10], 98, hot_day_note, col = "red", adj=0)
text (df$Date[10], 96, hot_nights_note, col = "blue", adj=0)
points(df$Date, df$Min_TemperatureF, col="blue", type="l")
points(hot_nights_df$Date, hot_nights_df$Min_TemperatureF, type="p", pch=19, col="blue")
legend(df$Date[75],67, c("Daily Max Temp", "Daily Min Temp", "Daily Max 90 or more", "Daily Min 75 or more" ),
col = c("black", "blue","red", "blue"),
text.col = "black", lty = c(1,1,0,0),lwd = c(2.5,2.5,0,0),seg.len=1, pch=c(0,0,19,19),pt.cex=c(0,0,1,1),
merge = T, bg = "", bty="n", box.col = "white", cex = .7, ncol=1)
}
# Output to png file and plot to console
png(file= "F:\\RClimate.us\\RClimate_Posts\\KPNE_Summer_hot_days.png", width=600, height=500)
plot_func()
dev.off()
plot_func()
To leave a comment for the author, please follow the link and comment on their blog: RClimate.
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.
