这个作业是来自澳洲的关于运用R语言实现国家地图的空间可视化代码代写,以下是作业具体详情:

 

Data Science Capstone

DATA3888: Data Science Capstone Lecture 3 – Spatial visualization Jean Yang 12 March, 2020 2 / 19 Representing spatial information There are several different R spatial formats to choose from. Your choice of format will largely be dictated by the package(s) and or function(s) used in your workBow. – Since 2000, there is an increase of packages and tools to enable R to interface with geographic data. There are now many packages listed in the CRAN task view. It is a good overview of the variety of packages that deal with GIS.for spatial data.

https://cran.rproject.org/web/views/Spatial.html- Two of the key packages are (i) sf: Simple Features for R (newer) and (2) sp: Classes and Methods for Spatial Data.

The sf package potentially will supersede sp that integrate with the tidyverse workBow of the packages. The R Spatial blog is a good place to keep up on developments of sf and complimentary packages that have adopted the use of sf objects. 3 / 19 oz() nsw() Simple map data – Oz library(oz) 4 / 19 Map data library(maps) ## careful there are some legacy maps here world_map <- map_data(“world”) ## low resolution map head(world_map) ## long lat group order region subregion ## 1 -69.89912 12.45200 1 1 Aruba ## 2 -69.89571 12.42300 1 2 Aruba ## 3 -69.94219 12.43853 1 3 Aruba ## 4 -70.00415 12.50049 1 4 Aruba ## 5 -70.06612 12.54697 1 5 Aruba ## 6 -70.05088 12.59707 1 6 Aruba dim(world_map) ## [1] 99338 6 The function map_data turns data from the maps package in to a data frame suitable for plotting with ggplot2. 5 / 19 Map data (in Week 3 lab) ggplot(world_map, aes(x = long, y = lat)) + geom_point() + theme_minimal() + theme(legend.position = “bottom”, aspect.ratio = ggtitle(“Map of World”) ggplot(world_map, aes(x = long, y = lat, group = group)) + geom_polygon(fill = “white”, colour = theme_minimal() + ggtitle(“Map of World” theme(legend.position = “bottom”, aspect.ratio = 6 / 19 Map data – Australia df <- world.cities[world.cities$country.etc == “Australia”,] plot(df[, c(“long”, “lat”)]) 7 / 19 Using Google Maps – extension There are a series of functions that are based on the Google Geocoding API. To use Google’s Geocoding API, you must Xrst enable the API in the Google Cloud Platform Console. See ?register_google.- To obtain an API key and enable services, go to https://cloud.google.com/maps-platform/. The mutate_geocode() function uses Google Maps to Xnd the longitude and latitude of each location. The get_map() function is a smart wrapper that queries the Google Maps, OpenStreetMap, Stamen Maps or Naver Map servers for a map. ## not tested library(ggmap) myMap <- get_map(location = “Australia”, zoom = 4) ggmap(myMap) + geom_point(data = df[, c(“long”,”lat”, “pop”)], aes(x=long, y = lat, colour = pop > 8 / 19 Applicatino to COVID-19 data setdiff(world_history_data_summary$Country.Region, world_map$region) ## [1] “Mainland China” “Iran (Islamic Republic of)” ## [3] “Republic of Korea” “US” ## [5] “Others” “Hong Kong SAR” ## [7] “Taipei and environs” “Viet Nam” ## [9] “occupied Palestinian territory” “Russian Federation” ## [11] “Macao SAR” “North Macedonia” ## [13] “Republic of Moldova” “Channel Islands” ## [15] “Gibraltar” “Holy See” unique(grep(“US”, world_map$region, value = TRUE)) ## [1] “USA” unique(grep(“China”, world_map$region, value = TRUE)) ## [1] “China” unique(grep(“Macedonia”, world_map$region, value = TRUE)) 9 / 19 world_map <- world_map %>% mutate(region = replace(region, region == “China”, “Mainland China”)) %>% mutate(region = replace(region, subregion == “Hong Kong”, “Hong Kong”)) %>% mutate(region = replace(region, subregion == “Macao”, “Macau”)) %>% mutate(region = replace(region, region == “USA”, “US”)) %>% mutate(region = replace(region, region == “Macedonia”, “North Macedonia”)) world_map_with_data <- merge(world_map, world_history_data_summary, by.x = “region”, by.y = “Country.Region”, all.x = TRUE) world_map_with_data <- world_map_with_data[order(world_map_with_data$order), ] 10 / 19 Color code breaks <- c(0, 1, 10, 50, 100, 500, 1000, 5000, 10000, 100000) reds_col <- RColorBrewer::brewer.pal(length(breaks) – 1, “Reds”) names(reds_col) <- levels(cut(0:max(breaks), breaks = breaks, include.lowest = T, right = F)) reds_col ## [0,1) [1,10) [10,50) [50,100) [100,500) [500,1e+03) ## “#FFF5F0” “#FEE0D2” “#FCBBA1” “#FC9272” “#FB6A4A” “#EF3B2C” ## [1e+03,5e+03) [5e+03,1e+04) [1e+04,1e+05] ## “#CB181D” “#A50F15” “#67000D” 11 / 19 Map data with ggplot – R Code world_map_with_data$Confirmed[is.na(world_map_with_data$Confirmed)] <- 0 world_map_with_data$Comfirmed_number <- cut(as.numeric(world_map_with_data$Confirmed), breaks, include.lowest = T, right = F) ggplot(world_map_with_data, aes(x = long, y = lat, group = group, fill = Comfirmed_number)) + geom_polygon() + theme_minimal() + ggtitle(“Map of World”) + scale_fill_manual(values = reds_col) + theme_void() + theme(legend.position = “bottom”, aspect.ratio = 0.6) + xlab(“”) + ylab(“”) + labs(title = paste(‘COVID19’), subtitle = paste(“Date:”, latest_date), fill = “Total number of confirmed”) 12 / 19 Map data with ggplot 13 / 19 LeaTet – basic usage Here is another option. Create a LeaBet map with these basic steps: Create a map widget by calling leaflet(). Add layers (i.e., features) to the map by using layer functions (e.g. addTiles, addMarkers, addPolygons) to modify the map widget. Repeat previous steps as required. Print the map widget to display it. 14 / 19 Map data – Australia population library(leaflet) pal <- colorNumeric(palette = “YlOrRd”, domain = df$pop) leaflet(data = df) %>% addTiles() %>% addCircleMarkers(lat = ~lat, lng = ~long, popup = ~name, color = ~pal(pop), stroke = FALSE, fillOpacity = 0.6) %>% addLegend(position = “bottomleft”, pal = pal, values = ~pop) 15 / 19 + − 500,000 1,000,000 1,500,000 2,000,000 pop Application to COVID-19 pal <- colorNumeric(palette = “YlOrRd”, domain = australia_history_data$Confirmed) leaflet(data = australia_history_data) %>% addTiles() %>% addCircleMarkers(lat = ~Lat, lng = ~Long, popup = ~Province.State, color = ~pal(Confirmed), fillOpacity = 0.6) %>% addLegend(position = “bottomleft”, pal = pal, values = ~Confirmed) 16 / 19 + − 500,000 1,000,000 1,500,000 2,000,000 2,500,000 pop Application to COVID-19 – world data world_history_data_all <- world_history_data %>% filter(Date == latest_date) ## breaks <- c(0, 1, 10, 50, 100, 500, 1000, 5000, 10000, 100000) world_history_data_all <- world_history_data_all %>% mutate(Comfirmed_level = cut(as.numeric(Confirmed), breaks, include.lowest = T, right = F)) pal <- colorNumeric(palette = “YlOrRd”, domain = as.numeric(world_history_data_all$Comfirmed_level)) leaflet(data = world_history_data_all) %>% addTiles() %>% addCircleMarkers(lat = ~Lat, lng = ~Long, popup = ~Country.Region, color = ~pal(as.numeric(Comfirmed_level)), stroke = FALSE, fillOpacity = 0.6, radius = ~log(Confirmed) * 1.5) 17 / 19 Application to COVID-19 – world data 18 / 19 + − Leaflet | © OpenStreetMap contributors, CC-BY-SA References Geocomputation with R by Robin Lovelace, Jakub Nowosad, Jannes Muenchow https://geocompr.robinlovelace.net Check out https://www.jessesadler.com/post/geocoding-with-r/ or help Xle for examples. 19 / 19