Below is the in class example that shows the way in which you can use world data (accessed through tmap). The map generated shows the countries of the world and symbology for each country based on the GDP.
#Class example map
#plot2
## here we are using a simple dataset of the world
# tmap_mode("plot")
library(ggplot2)
library(sf)
## Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.4 ✔ tibble 3.2.1
## ✔ purrr 1.0.4 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tmap)
data("World")
tm_shape(World) +
tm_polygons("gdp_cap_est", style='quantile', legend.title = "GDP Per Capita Estimate")
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_polygons()`: instead of `style = "quantile"`, use fill.scale =
## `tm_scale_intervals()`.
## ℹ Migrate the argument(s) 'style' to 'tm_scale_intervals(<HERE>)'[tm_polygons()] Argument `legend.title` unknown.[tip] Consider a suitable map projection, e.g. by adding `+ tm_crs("auto")`.
## the view mode creates an interactive map
tmap_mode("view")
## ℹ tmap modes "plot" - "view"
## ℹ toggle with `tmap::ttm()`
My idea for the personal map part of the assignment goes back to the daily geography games I like to play. One game in specific, GeoGrid, asks you to name a country that lies in the intersection of two catergories (flag with two colors, high GDP, touches the Atlantic ocean…etc). One category, “Borders X amount of countries”, I always find quite difficult. I thought making a map that shows the countries of the world with symbology based on the number of bordering countries would help me with my daily GeoGrid-ing!
In order to create the map, I had to find information for the number of bordering countries. I found a Wikipedia page that contained this information and turned it into a google sheet. I cleaned up that data to make sure the country names matched those that appeared in the “World” data that came from tmap. I then used a attribute join to combine these two datasets based on the “name” column that contained the country names.
Finally, I used some attributes of the tm_shape function in order to get a map that had an accurate legend and easy to read information about the number and names of the bordering countries.
# Personal Map
library(dplyr)
library(tmap)
library(sf)
data("World")
# Read borders CSV
borders <- st_read("borderfull.csv")
## Reading layer `borderfull' from data source
## `/Users/shamangarcia/Desktop/648/versionControl1/lab1/borderfull.csv'
## using driver `CSV'
## Warning: no simple feature geometries present: returning a data.frame or tbl_df
# Join world data with borders
world_joined <- left_join(World, borders, by = "name") %>%
select(name, Number.of.Bordering.Countries, Bordering.countries) %>%
mutate(
# Set factor order for the legend
Number.of.Bordering.Countries = factor(
Number.of.Bordering.Countries,
levels = c(1,2,3,4,5,6,7,8,9,10,11,14,NA),
exclude = NULL
),
# Add HTML line breaks for better popup readability
Bordering.countries = gsub(" (?=[A-Z][a-z]+:)", "<br>", Bordering.countries, perl = TRUE)
)
# Switch to interactive mode
tmap_mode("view")
## ℹ tmap modes "plot" - "view"
# Create the map
tm_shape(world_joined) +
tm_polygons(
"Number.of.Bordering.Countries",
style = "cat",
palette = "Set1",
title = "Number of Bordering Countries",
colorNA = "grey90",
textNA = "Missing",
id = "name",
popup.vars = c(
"Number of Borders" = "Number.of.Bordering.Countries",
"Bordering Countries" = "Bordering.countries"
),
popup.format = list(html.escape = FALSE)
) +
tm_layout(
legend.position = c("right", "bottom"),
legend.text.size = 0.6, # shrink text
legend.title.size = 0.8, # shrink title
legend.bg.alpha = 0.7 # slightly transparent background
)
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_polygons()`: instead of `style = "cat"`, use fill.scale =
## `tm_scale_categorical()`.
## ℹ Migrate the argument(s) 'palette' (rename to 'values'), 'colorNA' (rename to
## 'value.na'), 'textNA' (rename to 'label.na') to
## 'tm_scale_categorical(<HERE>)'
## For small multiples, specify a 'tm_scale_' for each multiple, and put them in a
## list: 'fill'.scale = list(<scale1>, <scale2>, ...)'[v3->v4] `tm_polygons()`: migrate the argument(s) related to the legend of the
## visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'[cols4all] color palettes: use palettes from the R package cols4all. Run
## `cols4all::c4a_gui()` to explore them. The old palette name "Set1" is named
## "brewer.set1"
An open data science approach means sharing data, code, and results so others can use and build on them. This helps make research more transparent, reproducible, and collaborative. It also speeds up discoveries because people don’t have to start from scratch. However, it can be challenging to protect privacy, keep data accurate, and give everyone equal access to tools and information. For example, this week’s reading showed how open climate data lets scientists worldwide study weather patterns together, but it also creates issues with data quality and consistency.
Completed above.