|
| 1 | +#' @title Plot Temperature Geographic Maps by States in USA |
| 2 | +#' |
| 3 | +#' @description This function plots temperature geographic maps for States in USA in specific year |
| 4 | +#' @param data A data.frame. The defalt dataset is GlobalLandTemperaturesByState. |
| 5 | +#' @param year numeric |
| 6 | +#' @examples |
| 7 | +#' temp_state(year=2012) |
| 8 | +#' |
| 9 | +#' @export |
| 10 | + |
| 11 | +# Loading Packages |
| 12 | +# library(choroplethr) |
| 13 | +# library(choroplethrMaps) |
| 14 | +# library(plotly) |
| 15 | +# library(countrycode) |
| 16 | + |
| 17 | +temp_state<-function(data=GlobalLandTemperaturesByState,year){ |
| 18 | + |
| 19 | +map <- data %>% |
| 20 | + mutate(Month=as.numeric(format(data$dt,"%m")), # Create new column month (decimal number) |
| 21 | + Month.String=format(data$dt,"%B"), # Create string month (full name) |
| 22 | + Year=as.numeric(format(data$dt,"%Y"))) %>% # Create new column year (4 digit) |
| 23 | + na.omit() %>% filter(Country=="United States") |
| 24 | + |
| 25 | +map$State <- as.character(map$State) |
| 26 | +map$State[map$State=="Georgia (State)"] <- "Georgia" # Changing Georgia (State) |
| 27 | +map$State<- as.factor(map$State) |
| 28 | + |
| 29 | +#' select columns of interest |
| 30 | +map_select <- map %>% |
| 31 | + select(Year,AverageTemperature,State) %>% |
| 32 | + group_by(Year, State) %>% |
| 33 | + summarise(value=mean(AverageTemperature)) |
| 34 | + |
| 35 | +#Data frame must have a column named region (all lower case) and another one value. |
| 36 | +colnames(map_select)[2]<- "region" |
| 37 | +map_select$region<-tolower(map_select$region) |
| 38 | + |
| 39 | +map_state<-map_select %>% |
| 40 | + filter(Year==year) |
| 41 | + |
| 42 | +map_state<-map_state[,2:3] |
| 43 | + |
| 44 | +print(state_choropleth(map_state, |
| 45 | + title = paste("Land Temperature",year," "), |
| 46 | + num_colors = 8, |
| 47 | + legend = "Degrees"),reference_map=TRUE) |
| 48 | +} |
| 49 | + |
| 50 | +#' @title Plot Temperature Geographic Maps by Country |
| 51 | +#' |
| 52 | +#' @description This function plots temperature geographic maps for countries in specific year. You can use this |
| 53 | +#' function to get a temperature geographic map showing the temperature change from the start year to end year. |
| 54 | +#' @param data A data.frame. The defalt dataset is GlobalLandTemperaturesByCountry. |
| 55 | +#' @param year A numeric. You can get temperature geographic maps for countries in this year. |
| 56 | +#' @param start A numeric. The start year you want to do temperature comparison. |
| 57 | +#' @param end A numeric. Then end year you want to do temperature comparison. |
| 58 | +#' @param diff A character. If diff=="TRUE", you will get a temperature geographic map showing the temperature |
| 59 | +#' change from the start year to end year. (Default value is "FALSE") |
| 60 | +#' @examples |
| 61 | +#' temp_country(year=2012) |
| 62 | +#' temp_country(start=1990,end=2000,diff="TRUE") |
| 63 | + |
| 64 | +temp_country<-function(data=GlobalLandTemperaturesByCountry, year, start, end, diff="FALSE"){ |
| 65 | + # light grey boundaries |
| 66 | + l <- list(color = toRGB("grey"), width = 0.5) |
| 67 | + |
| 68 | + # specify map projection/options |
| 69 | + g <- list( |
| 70 | + showframe = FALSE, |
| 71 | + showcoastlines = FALSE, |
| 72 | + projection = list(type = 'Mercator') |
| 73 | + ) |
| 74 | + |
| 75 | + map_country <- data %>% |
| 76 | + mutate(Month=as.numeric(format(data$dt,"%m")), # Create new column month (decimal number) |
| 77 | + Month.String=format(data$dt,"%B"), # Create string month (full name) |
| 78 | + Year=as.numeric(format(data$dt,"%Y"))) %>% # Create new column year (4 digit) |
| 79 | + na.omit()%>% |
| 80 | + select(Year,AverageTemperature,Country) %>% |
| 81 | + group_by(Year, Country) %>% |
| 82 | + summarise(AvgTemp=mean(AverageTemperature)) |
| 83 | + |
| 84 | + code<-countrycode(map_country$Country,'country.name', 'iso3c') # Converts long country name into country codes |
| 85 | + |
| 86 | + map_country$CODE<-code # Create new column in map_country named "CODE" |
| 87 | + |
| 88 | + if(diff=="FALSE"){ |
| 89 | + temp<-map_country%>%filter(Year==year) |
| 90 | + |
| 91 | + map_temp <- plot_geo(temp) %>% |
| 92 | + add_trace( |
| 93 | + z = ~AvgTemp, color = ~AvgTemp, colors = 'Reds', |
| 94 | + text = ~Country, locations = ~CODE, marker = list(line = l) |
| 95 | + ) %>% |
| 96 | + colorbar(title = 'Temperature') %>% |
| 97 | + layout( |
| 98 | + title = paste(year,"Temperature Map",sep=" "), |
| 99 | + geo = g |
| 100 | + ) |
| 101 | + |
| 102 | + map_temp |
| 103 | + } else if (diff=="TRUE") { |
| 104 | + |
| 105 | + temp_diff<-map_country %>% |
| 106 | + filter(Year==start | Year==end) %>% |
| 107 | + tidyr::spread(Year, AvgTemp) |
| 108 | + |
| 109 | + temp_diff$Difference<-unlist(temp_diff[,4]-temp_diff[,3]) # Calculate temperature variation from start year to end year |
| 110 | + |
| 111 | + map_temp <- plot_geo(temp_diff) %>% |
| 112 | + add_trace( |
| 113 | + z = ~Difference, color = ~Difference, colors = 'Reds', |
| 114 | + text = ~Country, locations = ~CODE, marker = list(line = l) |
| 115 | + ) %>% |
| 116 | + colorbar(title = 'Temperature Variation') %>% |
| 117 | + layout( |
| 118 | + title = paste(start,"-",end,"Temperature Variation Map", sep=" "), |
| 119 | + geo = g |
| 120 | + ) |
| 121 | + print(map_temp) |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments