Introduction

api_key <- "SZehIKahDqdrXTQv3qCMWjfK0vDCDahIPowe87Ov"
url <- "https://developer.nrel.gov/api/solar/solar_resource/v1.json"
parameters <- list(api_key=api_key, lat=41, lon=-75)

Getting the Data

You can also embed plots, for example:

response <- GET(url=url, query=parameters)

status <- status_code(response)
status
## [1] 200
response_type <- http_type(response)
response_type
## [1] "application/json"
content <- content(response, "text")
print(content)
## [1] "{\"version\":\"1.0.0\",\"warnings\":[],\"errors\":[],\"metadata\":{\"sources\":[\"Perez-SUNY/NREL, 2012\"]},\"inputs\":{\"lat\":\"41\",\"lon\":\"-75\"},\"outputs\":{\"avg_dni\":{\"annual\":3.69,\"monthly\":{\"jan\":3.12,\"feb\":3.36,\"mar\":4.1,\"apr\":4.07,\"may\":4.15,\"jun\":4.17,\"jul\":4.6,\"aug\":4.14,\"sep\":4.02,\"oct\":3.26,\"nov\":2.58,\"dec\":2.72}},\"avg_ghi\":{\"annual\":3.87,\"monthly\":{\"jan\":1.97,\"feb\":2.69,\"mar\":3.86,\"apr\":4.7,\"may\":5.45,\"jun\":5.78,\"jul\":5.98,\"aug\":5.14,\"sep\":4.23,\"oct\":2.94,\"nov\":1.99,\"dec\":1.67}},\"avg_lat_tilt\":{\"annual\":4.52,\"monthly\":{\"jan\":3.55,\"feb\":4.04,\"mar\":4.86,\"apr\":4.97,\"may\":5.18,\"jun\":5.24,\"jul\":5.58,\"aug\":5.24,\"sep\":5.0,\"oct\":4.11,\"nov\":3.26,\"dec\":3.13}}}}"

Parsing and Examining the Data

json_data <- jsonlite::fromJSON(content)
json_data
## $version
## [1] "1.0.0"
## 
## $warnings
## list()
## 
## $errors
## list()
## 
## $metadata
## $metadata$sources
## [1] "Perez-SUNY/NREL, 2012"
## 
## 
## $inputs
## $inputs$lat
## [1] "41"
## 
## $inputs$lon
## [1] "-75"
## 
## 
## $outputs
## $outputs$avg_dni
## $outputs$avg_dni$annual
## [1] 3.69
## 
## $outputs$avg_dni$monthly
## $outputs$avg_dni$monthly$jan
## [1] 3.12
## 
## $outputs$avg_dni$monthly$feb
## [1] 3.36
## 
## $outputs$avg_dni$monthly$mar
## [1] 4.1
## 
## $outputs$avg_dni$monthly$apr
## [1] 4.07
## 
## $outputs$avg_dni$monthly$may
## [1] 4.15
## 
## $outputs$avg_dni$monthly$jun
## [1] 4.17
## 
## $outputs$avg_dni$monthly$jul
## [1] 4.6
## 
## $outputs$avg_dni$monthly$aug
## [1] 4.14
## 
## $outputs$avg_dni$monthly$sep
## [1] 4.02
## 
## $outputs$avg_dni$monthly$oct
## [1] 3.26
## 
## $outputs$avg_dni$monthly$nov
## [1] 2.58
## 
## $outputs$avg_dni$monthly$dec
## [1] 2.72
## 
## 
## 
## $outputs$avg_ghi
## $outputs$avg_ghi$annual
## [1] 3.87
## 
## $outputs$avg_ghi$monthly
## $outputs$avg_ghi$monthly$jan
## [1] 1.97
## 
## $outputs$avg_ghi$monthly$feb
## [1] 2.69
## 
## $outputs$avg_ghi$monthly$mar
## [1] 3.86
## 
## $outputs$avg_ghi$monthly$apr
## [1] 4.7
## 
## $outputs$avg_ghi$monthly$may
## [1] 5.45
## 
## $outputs$avg_ghi$monthly$jun
## [1] 5.78
## 
## $outputs$avg_ghi$monthly$jul
## [1] 5.98
## 
## $outputs$avg_ghi$monthly$aug
## [1] 5.14
## 
## $outputs$avg_ghi$monthly$sep
## [1] 4.23
## 
## $outputs$avg_ghi$monthly$oct
## [1] 2.94
## 
## $outputs$avg_ghi$monthly$nov
## [1] 1.99
## 
## $outputs$avg_ghi$monthly$dec
## [1] 1.67
## 
## 
## 
## $outputs$avg_lat_tilt
## $outputs$avg_lat_tilt$annual
## [1] 4.52
## 
## $outputs$avg_lat_tilt$monthly
## $outputs$avg_lat_tilt$monthly$jan
## [1] 3.55
## 
## $outputs$avg_lat_tilt$monthly$feb
## [1] 4.04
## 
## $outputs$avg_lat_tilt$monthly$mar
## [1] 4.86
## 
## $outputs$avg_lat_tilt$monthly$apr
## [1] 4.97
## 
## $outputs$avg_lat_tilt$monthly$may
## [1] 5.18
## 
## $outputs$avg_lat_tilt$monthly$jun
## [1] 5.24
## 
## $outputs$avg_lat_tilt$monthly$jul
## [1] 5.58
## 
## $outputs$avg_lat_tilt$monthly$aug
## [1] 5.24
## 
## $outputs$avg_lat_tilt$monthly$sep
## [1] 5
## 
## $outputs$avg_lat_tilt$monthly$oct
## [1] 4.11
## 
## $outputs$avg_lat_tilt$monthly$nov
## [1] 3.26
## 
## $outputs$avg_lat_tilt$monthly$dec
## [1] 3.13

Splitting and Transforming the Data

outputs <- json_data$outputs

avg_dni <- as.numeric(outputs$avg_dni$monthly)
avg_ghi <- as.numeric(outputs$avg_ghi$monthly)
avg_lat_tilt <- as.numeric(outputs$avg_lat_tilt$monthly)

df <- tibble::tibble("month"=month.abb, "avg_dni"=avg_dni,
                     "avg_ghi"=avg_ghi, "avg_lat_tilt"=avg_lat_tilt)
head(df)
## # A tibble: 6 × 4
##   month avg_dni avg_ghi avg_lat_tilt
##   <chr>   <dbl>   <dbl>        <dbl>
## 1 Jan      3.12    1.97         3.55
## 2 Feb      3.36    2.69         4.04
## 3 Mar      4.1     3.86         4.86
## 4 Apr      4.07    4.7          4.97
## 5 May      4.15    5.45         5.18
## 6 Jun      4.17    5.78         5.24

Visualizing Data

library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
ggplot(data = df,
       aes(x = month, y = avg_dni, group = 1)) +
  geom_line() +
  geom_point() +
  theme_bw()

df <- df %>% 
  mutate(month = factor(month, levels = month.abb))


dni_plot <- ggplot(data = df,
       aes(x = month, y = avg_dni, group = 1, color=avg_ghi)) +
  geom_line() +
  geom_point() +
  theme_linedraw()

print(dni_plot + ggtitle("Average Direct Irradiation over Months"))

ghi_plot <- ggplot(data = df,
       aes(x = month, y = avg_ghi, group = 1, color=avg_dni)) +
  geom_line() +
  geom_point() +
  theme_linedraw()

print(ghi_plot + ggtitle("Average Global Horizontal Irradiation over Months"))