123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- title: 'Guided Project: New York Solar Resource Data'
- author: "Dataquest"
- date: "11/26/2020"
- output: html_document
- - Title: Analyzing New York solar data.
- - Using APIs gives us access to an incredible amount of data only available online. In this exercise, we want to extract New York City solar data. Such data can, for example, allow us to determine on average the most productive periods of the year for solar panel deployment.
- ```{r}
- the_key = ""
- url <- "https://developer.nrel.gov/api/solar/solar_resource/v1.json"
- parameters_list <- list(api_key = the_key, lat = 41, lon = -75)
- ```
- ```{r}
- library(httr)
- response <- GET(url, query = parameters_list)
- status <- status_code(response)
- status
- response_type <- http_type(response)
- response_type
- content <- content(response, "text")
- print(content)
- ```
- ```{r}
- json_lists <- jsonlite::fromJSON(content)
- str(json_lists)
- ```
- ```{r}
- outputs_list <- json_lists$outputs
- avg_dni <- outputs_list$avg_dni$monthly
- avg_ghi <- outputs_list$avg_ghi$monthly
- avg_lat_tilt <- outputs_list$avg_lat_tilt$monthly
- dataframe <- tibble::tibble("month" = month.abb,
- "avg_dni" = avg_dni,
- "avg_ghi" = avg_ghi,
- "avg_lat_tilt" = avg_lat_tilt)
- dataframe
- ```
- - (Instruction 4's answer)
- We can see that all the columns are still lists containing one item. For future use of this dataframe, it would likely be necessary to convert these columns to numeric data type.
- ```{r}
- outputs_list <- json_lists$outputs
- simplified_outputs_list <- unlist(outputs_list)
- data_matrix <- matrix(data = simplified_outputs_list, nrow = 13)
- data_matrix <- data_matrix[-1, ]
- another_dataframe <- as.data.frame(data_matrix)
- another_dataframe
- ```
- - (Instruction 6's answer)
- We can see that all the columns are numeric. However, we haven't appended the `month` column yet.
- ```{r}
- library(httr)
- library(dplyr)
- the_key = ""
- nrel_api_json_get_df <- function(endpoint, queries = list()) {
-
- url <- modify_url("https://developer.nrel.gov", path = endpoint)
-
- response <- GET(url, query = queries)
-
- if ( http_error(response) ){
- print(status_code(response))
- print(http_status(response))
- stop("Something went wrong.", call. = FALSE)
- }
- if (http_type(response) != "application/json") {
- stop("API did not return json", call. = FALSE)
- }
-
- json_text <- content(response, "text")
-
- table_lst <- jsonlite::fromJSON(json_text)
- dataframe <- tibble::tibble("month" = month.abb,
- "avg_dni" = as.numeric(table_lst$outputs$avg_dni$monthly),
- "avg_ghi" = as.numeric(table_lst$outputs$avg_ghi$monthly),
- "avg_lat_tilt" = as.numeric(table_lst$outputs$avg_lat_tilt$monthly))
-
- dataframe
- }
- solar_resource_df <- nrel_api_json_get_df("api/solar/solar_resource/v1.json", parameters_list)
- solar_resource_df
- ```
- ```{r}
- library(ggplot2)
- library(dplyr)
- ggplot(data = solar_resource_df,
- aes(x = month, y = avg_dni, group = 1)) +
- geom_line() +
- geom_point() +
- theme_bw()
- solar_resource_df <- solar_resource_df %>%
- mutate(month = factor(month, levels = month.abb))
- ggplot(data = solar_resource_df,
- aes(x = month, y = avg_dni, group = 1)) +
- geom_line() +
- geom_point() +
- theme_bw()
- ```
- - (Instruction 5's answer)
- The first plot x-axis is ordered alphabetically, while the second is ordered chronologically from January to December.
- This operation allows ordering the labels in the plot as we wish.
|