class: center, middle, inverse, title-slide .title[ # Introduction to Data Science ] .subtitle[ ## Week 7: Maps (cont’d) ] .author[ ### Ugur Aytun ] .institute[ ### METU, Department of Economics | ECON 413 ] --- --- # Maps -- - This week we proceed with the topic of maps. -- - Last week we uploaded our own map of Turkey, which was a shapefile containing the administrative borders of Turkey's provinces. -- - New packages are introduced, such as `rnaturalearth` and `rnaturalearthdata` for geographical data without requiring shapefiles, and `sf` for handling spatial data in R. --- # Loading Required Libraries, set the working directory and load the map of the world .scrollable[ ``` r library(sf) ``` ``` ## Warning: package 'sf' was built under R version 4.3.3 ``` ``` ## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE ``` ``` r library("rnaturalearth") ``` ``` ## Warning: package 'rnaturalearth' was built under R version 4.3.3 ``` ``` r library("rnaturalearthdata") ``` ``` ## Warning: package 'rnaturalearthdata' was built under R version 4.3.3 ``` ``` ## ## Attaching package: 'rnaturalearthdata' ``` ``` ## The following object is masked from 'package:rnaturalearth': ## ## countries110 ``` ``` r library("ggplot2") ``` ``` ## Warning: package 'ggplot2' was built under R version 4.3.3 ``` ``` r library(data.table) ``` ``` ## Warning: package 'data.table' was built under R version 4.3.3 ``` ``` r world <- ne_countries(scale = "medium", returnclass = "sf") class(world) ``` ``` ## [1] "sf" "data.frame" ``` ] --- # World map .scrollable[ ``` r # plot the world map ggplot(data = world) + geom_sf() ``` <!-- --> ``` r # plot the world map with a black border ggplot(data = world) + geom_sf() + theme_bw() ``` <!-- --> ``` r # plot the world map with a black border and no background ggplot(data = world) + geom_sf() + theme_void() ``` <!-- --> ``` r # plot the world map with a black border and no background ggplot(data = world) + geom_sf() + theme_bw() + labs(title = "World map", subtitle = paste0(length(unique(world$sov_a3)), " countries"), x = "Longitude", y = "Latitude") ``` <!-- --> ``` r # create titles object ctitles <- labs(title = "World map", subtitle = paste0(length(unique(world$sov_a3)), " countries"), x = "Longitude", y = "Latitude") # add a title ggplot(data = world) + geom_sf() + theme_bw() + ctitles ``` <!-- --> ``` r # fill the countries with a color ggplot(data = world) + geom_sf(color = "black", fill = "lightblue") + theme_bw() + ctitles ``` <!-- --> ``` r # add coordinate numbers ggplot(data = world) + geom_sf(color = "black", fill = "lightblue") + coord_sf(ylim = c(-65, 90), expand = FALSE) + theme_bw() + ctitles ``` <!-- --> ] --- # World and Turkey map .scrollable[ ``` r # color the countries by economic development ggplot(data = world) + geom_sf(aes(fill = economy)) + coord_sf(ylim = c(-65, 90), expand = FALSE) + theme_bw() + ctitles + scale_fill_viridis_d(option = "plasma") ``` <!-- --> ``` r # color the countries by population ggplot(data = world) + geom_sf(aes(fill = pop_est)) + coord_sf(ylim = c(-65, 90), expand = FALSE) + theme_bw() + ctitles + scale_fill_viridis_c(option = "plasma", trans = "sqrt") ``` <!-- --> ``` r # color the countries by population (inverse color scale) ggplot(data = world) + geom_sf(aes(fill = pop_est)) + coord_sf(ylim = c(-65, 90), expand = FALSE) + theme_bw() + ctitles + scale_fill_viridis_c(option = "plasma", trans = "sqrt", direction = -1) ``` <!-- --> ``` r # color the countries by population (inverse color scale) with a different projection ggplot(data = world) + geom_sf(aes(fill = pop_est)) + theme_bw() + ctitles + scale_fill_viridis_c(option = "plasma", trans = "sqrt", direction = -1) + coord_sf(crs = st_crs(3035)) ``` <!-- --> ``` r # Turkey map turkey <- ne_states(country = "turkey", returnclass = "sf") class(turkey) ``` ``` ## [1] "sf" "data.frame" ``` ``` r ggplot(data = turkey) + geom_sf() + theme_void() + labs(title = "Turkey map") ``` <!-- --> ``` r # add province name ggplot(data = turkey) + geom_sf() + geom_sf_text(aes(latitude, longitude, label = name), size = 2) + theme_void() + labs(title = "Turkey map") ``` ``` ## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not ## give correct results for longitude/latitude data ``` <!-- --> ``` r # different color ggplot(data = turkey) + geom_sf(color = "black", fill = "red") + geom_sf_text(aes(latitude, longitude, label = name), size = 2) + theme_void() + labs(title = "Turkey map") ``` ``` ## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not ## give correct results for longitude/latitude data ``` <!-- --> ]