class: center, middle, inverse, title-slide .title[ # Creating a Map Using ggplot2 ] .author[ ###
Ying-Ju Tessa Chen, PhD
Associate Professor
Department of Mathematics
University of Dayton
@ying-ju
ying-ju
ychen4@udayton.edu
] --- ## Learning Objectives .small[ In this session, we will talk about how to create a map using the R package .red[ggplot2]. In order to create a map, we will need the following two key functions. - .blue[map_data()] from the package .red[maps] to access the map data related to longitude and latitude. - .blue[geom_polygon()] from the package .red[ggplot2] to create a map. ``` r library(pacman) p_load(tidyverse, maps, viridis) ``` Let's take a look at the usage of the function .blue[map_data()]. ```{} map_data(map, region = ".", exact = FALSE, ...) ``` Arguments: - .orange[map]: name of map provided by the package. These include county, france, italy, nz, state, usa, world, world2 - .orange[region]: name of subregions. "." means all subregions are included. - .orange[exact]: the region is treated as a regular expression (FALSE) or as a fixed string (TRUE). **Note**: The package .red[viridis] provides color maps designed to improve graph readability for readers with common forms of color blindness and/or color vision deficiency. ] --- ## A Simple World Map Let's create a simple world map. .pull-left[ ``` r map <- map_data("world") ggplot(map, aes(x = long, y=lat, group = group)) + geom_polygon(fill="lightblue", colour = "white") + coord_fixed(1.3) + theme_void() ``` ] .pull-right[ <img src="data:image/png;base64,#ggplot2_maps_files/figure-html/unnamed-chunk-2-1.png" width="100%" style="display: block; margin: auto;" /> ] --- ## A Map for Specific Regions .pull-left[ ``` r North_Asia <- c("China", "Japan", "Mongolia", "North Korea", "South Korea", "Taiwan") North_Asia_map <- map_data("world", region = North_Asia) # Compute the centroid as the mean longitude # and latitude region.data <- North_Asia_map %>% group_by(region) %>% summarise(long = mean(long), lat = mean(lat)) # Ready to plot the map ggplot(North_Asia_map, aes(x = long, y = lat)) + geom_polygon(aes(group = group, fill = region)) + geom_text(aes(label = region), data = region.data, size = 5, hjust = 0.5, col = "#808080", fontface='bold') + scale_fill_viridis_d() + theme_void() + theme(legend.position = "none") ``` ] .pull-right[ <img src="data:image/png;base64,#ggplot2_maps_files/figure-html/asia-1.png" width="90%" style="display: block; margin: auto;" /> ] --- ## A Choropleth Map (Continued) .pull-left[ In many situations, we want to create a map including some statistics. Here we use the [Alcohol Consumption by Country](https://www.kaggle.com/datasets/justmarkham/alcohol-consumption-by-country) to show an example. ``` r drinks <- read_csv("../Data/drinks.csv") drinks_map <- drinks %>% left_join(map, by = c("country"="region")) ggplot(drinks_map, aes(long, lat, group = group)) + geom_polygon(aes(fill = total_litres_of_pure_alcohol), colour = "white") + scale_fill_viridis_c(option = "D") + labs(fill = "Total Litres of Pure Alcohol") + coord_fixed(1.3) + theme_void() + theme(legend.position="bottom") ``` ] .pull-right[ <img src="data:image/png;base64,#ggplot2_maps_files/figure-html/unnamed-chunk-5-1.png" width="100%" style="display: block; margin: auto;" /> ] --- ## Taiwan Map .pull-left[ ``` r taiwan_map <- read_csv("../data/taiwan_map.csv") taiwan_map_center <- taiwan_map |> group_by(county_EN) |> summarise(long = mean(long), lat = mean(lat)) taiwan_map |> ggplot(aes(x = long, y = lat)) + geom_polygon(aes(group = group), fill = "#0096a9", colour = "lightgrey") + geom_text(aes(label = county_EN), data = taiwan_map_center) + theme_void() + theme(legend.position = "none") ``` ] .pull-right[ <img src="data:image/png;base64,#ggplot2_maps_files/figure-html/TW_map_show-1.png" width="100%" style="display: block; margin: auto;" /> ] --- ## Taiwan Map with Renewable Energy Data .pull-left[ ``` r df <- read_csv("../data/renewable_energy.csv") df <- df |> rename( year = `年別`, county = `縣市`, wind = `風力`, solar = `太陽光電`, other = `其他(含水力)` ) df_112 <- df |> filter(year == 112) df_map <- df_112 |> left_join(taiwan_map, by = c("county"="county_CN")) df_map |> ggplot(aes(x = long, y = lat)) + geom_polygon(aes(group = group, fill = solar, text = paste0(county, ":\n", solar, " 瓩")), colour = "darkgrey") + scale_fill_gradient(low="#ffe0b3", high="#ff9900") + theme_void() + theme(legend.position = "none") -> p p ``` ] .pull-right[ <img src="data:image/png;base64,#ggplot2_maps_files/figure-html/TW_map_data_show-1.png" width="100%" style="display: block; margin: auto;" /> ] --- ## Dynamic Figure .pull-left[ ``` r font <- list( family = "Arial", size = 15, color = "white" ) label <- list( bgcolor = "#232F34", bordercolor = "transparent", font = font ) library(plotly) ggplotly(p, tooltip = "text") %>% style(hoverlabel = label) %>% layout(font = font) ``` ] .pull-right[
] --- ## In-Class Activity You can download the worksheet [here](https://bitbucket.org/tessachen/tessachen.bitbucket.io/src/main/worksheets/ggplot2_maps_activity.docx). <br> ## Thank you! .pull-left-2[ Please do not hesitate to contact me (Tessa Chen) at <a href="mailto:ychen4@udayton.edu"><i class="fa fa-paper-planee fa-fw"></i? : ychen4@udayton.edu</a> for questions or further discussions. ] .pull-right-2[ <img src="data:image/png;base64,#../figs/Tessa_grey_G.gif" width="50%" style="display: block; margin: auto;" /> ]