Last active
December 17, 2015 02:49
-
-
Save jasonneylon/5538770 to your computer and use it in GitHub Desktop.
R script to read Air Quality Egg NO2 data from Cosm and plot it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages('RCurl') | |
install.packages("ggplot2") | |
library(ggplot2) | |
library(RCurl) | |
# grab an api key from cosm and put it here | |
api_key = 'YOUR_API_KEY' | |
# your feed id | |
feed_id = '106267' | |
csv_data = "" | |
start_date = as.POSIXct("2013/05/07","GMT") | |
end_date = as.POSIXct("2013/05/08","GMT") | |
while (start_date < end_date) { | |
start_date_as_str = format(start_date, format="%Y-%m-%dT%H:%M:00Z") | |
next_date = start_date + (3 * 60 * 60) | |
next_date_as_str = format(next_date, format="%Y-%m-%dT%H:%M:00Z") | |
cosm_url = paste("http://api.cosm.com/v2/feeds/", feed_id, "/datastreams/NO2_00-04-a3-37-cc-cb_0.csv?start=", start_date_as_str, "&end=", next_date_as_str,"&interval=0", sep="") | |
csv_data_for_period = getURL(cosm_url, httpheader = c('X-ApiKey' = api_key) ) | |
csv_data = paste(csv_data, csv_data_for_period, "\n") | |
start_date = next_date | |
} | |
data = read.table(textConnection(csv_data), sep = ",", col.names=c("at", "value")) | |
data$timestamp <- strptime(data$at, "%FT%T", tz = "UTC") | |
# strip out outliers | |
data = data[which(data$value < 1000),] | |
#summary(data) | |
ggplot(data, aes(timestamp,value)) + geom_point() + geom_smooth() + xlab("Time") + ylab("NO2 PPB") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment