# A tibble: 3 × 7
region wknd_gross long lat group order continent
<chr> <dbl> <dbl> <dbl> <dbl> <int> <chr>
1 United Kingdom 19000000 -1.07 50.7 570 40057 Europe
2 United Kingdom 19000000 -1.15 50.7 570 40058 Europe
3 United Kingdom 19000000 -1.18 50.6 570 40059 Europe
Choropleth Country Plot w/ Labels
Example - Asia
Most of the plot code that follows is review
There are a few new details:
shadowtext labels (see below)
modifying size of text elements (mentioned but not emphasized)
NOTES:
The R package shadowtext includes the command geom_shadowtext
shadowtext is useful for creating visible labels for all countries regardless of map fill color
Deciding on units ($1000) and transformation (log) took some trial and error.
Managing Data for Asia Chropleth Map
This R code creates the Asia Map dataset.
asia_bxo_data <- world_bxo_data |># create asia box office dataset filter(continent=="Asia") |>mutate(Gross =as.integer(wknd_gross), wknd_gross = wknd_gross/1000) asia_nms <- asia_bxo_data |># create dataset of country names select(region, long, lat, group, continent) |># median lat and long # used for label positionsgroup_by(continent, region) |>summarize(nm_x=median(long, na.rm=T),nm_y=median(lat, na.rm=T)) |>filter(!is.na(nm_x) |!is.na(nm_y))asia_bxo_data <-full_join(asia_bxo_data, asia_nms) # merge datasets using an inner_join
R code for Asia Choropleth Map
Data are shown on log scale to improve interpretability.
asia_bxo_map <- asia_bxo_data |># Creates the map that followsggplot(aes(x=long, y=lat, group=group, fill=wknd_gross)) +geom_polygon(color="darkgrey") +theme_map() +coord_map("albers", lat0 =39, lat1 =45) +labs(fill="Gross ($1K)",title="Weekend Gross ($ Thousands) in Asian Countries",subtitle="Weekend Data Updated 4/7/25 - Data are Log-transformed",caption="Data Source: https://www.boxofficemojo.com") +scale_fill_continuous(type ="viridis", trans="log",breaks =c(1,10,100,1000,10000)) +geom_shadowtext(aes(x=nm_x, y=nm_y,label=region),color="white",check_overlap = T,show.legend = F, size=4) +theme(plot.title =element_text(size =20),plot.subtitle =element_text(size =15),plot.caption =element_text(size =10),legend.text =element_text(size =12),legend.title =element_text(size =15),plot.background =element_rect(colour ="darkgrey", fill=NA, linewidth=2))
Asia Map with Log (LN) Transformation
Europe Map Data
Creates data for Europe Map
euro_bxo_data <- world_bxo_data |># create Europe box office dataset filter(continent=="Europe"& region !="Russia") |>mutate(Gross =as.integer(wknd_gross), wknd_gross = wknd_gross/1000) euro_nms <- euro_bxo_data |># create dataset of country names select(region, long, lat, group, continent) |># median lat and long used for positiongroup_by(continent, region) |>summarize(nm_x=median(long, na.rm=T),nm_y=median(lat, na.rm=T)) |>filter(!is.na(nm_x) |!is.na(nm_y))euro_bxo_data <-full_join(euro_bxo_data, euro_nms) # merge datasets using an inner_join
R code for Europe Choropleth Map
Data are shown on log scale to improve interpretability.
Question 1. What option is used in geom_polygon() to create the outlines of each country?
Question 2. How many different geometries (geom_...) are used to create these multi-layer maps?
Question 3. When using multiple geometry layers, where do you place the aesthetic, (aes) so that it will apply to all of the geometries (all of the map layers)?
US State Data Example
Examples of Data that can be plotted by state
Average costs and expenditures by state of specific goods or services
Demographic data
Voting and tex information
Sports/Arts/Entertainment/Education investments and expenditures
Will also show a map of data filtered by region
US State Map Data
us_states <-map_data("state") |># state polygons (from R)select(long:region) |>rename("state"="region")state_abbr <- state_stats |># many useful variables in this datasetselect(state, abbr) |>mutate(state =tolower(state))state_pop <- county_2019 |># data by county (aggregated by state)select(state, pop) |>mutate(state=tolower(state),popM = pop/1000000) |>group_by(state) |>summarize(st_popM =sum(popM, na.rm=T)) |>full_join(state_abbr)statepop_map <-left_join(us_states, state_pop) # used left join to filter to lower 48 states# lat/long not available for Hi and AK
Adding State Midpoint (centroid) Lat and Long
In the previous maps (by country) country labels were added to the static map using each polygon’s (country) median latitude and longitude
Medians don’t work well for U.S. because many states are oddly shaped and small.
Question 4. What exploratory plot command (base R code shown) is good for checking if the variable you want to plot is right skewed and might need to be log transformed?
Question 5. Based on the histogram for the northeastern area of the U.S, which includes only 10 states, do these data appear skewed?
Add Education Data to Map Data
In the chunk below we start from scratch with state data. This chunk does not depend on the data being imported and managed in a previous chunk.
us_states <-map_data("state") |># state polygons (from R)select(long:region) |>rename("state"="region")state_abbr <- state_stats |># state abbreviations select(state, abbr) |>mutate(state =tolower(state))edu1 <-left_join(edu1, state_abbr) # left join to maintain filter to NE statesedu_NE_map <-left_join(edu1, us_states) # left join to maintain filter to NE statesstate_coords <-read_csv("data/state_coords.csv", show_col_types = F, # add in state midpoints (centroids)col_names =c("state", "m_lat", "m_long")) |>mutate(state =gsub(", USA", "", state, fixed=T),state =gsub(", the USA", "", state, fixed=T),state =gsub(", the US", "", state, fixed=T),state =tolower(state))edu_NE_map <-left_join(edu_NE_map, state_coords) # left join to maintain filter to NE states
You may submit an ‘Engagement Question’ about each lecture until midnight on the day of the lecture. A minimum of four submissions are required during the semester.