
geom_polygon()geom_sf()Get the “World” shape file
#> long lat group order region subregion
#> 1 -69.89912 12.45200 1 1 Aruba <NA>
#> 2 -69.89571 12.42300 1 2 Aruba <NA>
#> 3 -69.94219 12.43853 1 3 Aruba <NA>
#> 4 -70.00415 12.50049 1 4 Aruba <NA>
#> 5 -70.06612 12.54697 1 5 Aruba <NA>
#> 6 -70.05088 12.59707 1 6 Aruba <NA>

Simple Features package: {sf}
Art by
Allison Horst
Library data from Natural Earth
#> Simple feature collection with 6 features and 1 field
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -73.36621 ymin: -22.40205 xmax: 109.4449 ymax: 41.9062
#> Geodetic CRS: WGS 84
#> name geometry
#> 1 Zimbabwe MULTIPOLYGON (((31.28789 -2...
#> 2 Zambia MULTIPOLYGON (((30.39609 -1...
#> 3 Yemen MULTIPOLYGON (((53.08564 16...
#> 4 Vietnam MULTIPOLYGON (((104.064 10....
#> 5 Venezuela MULTIPOLYGON (((-60.82119 9...
#> 6 Vatican MULTIPOLYGON (((12.43916 41...
Get the Continental “US States” shape file
Make the plot with geom_sf()

maps packagest_read()Read in the “World” shape file from Natural Earth
#> Reading layer `ne_50m_admin_0_countries' from data source `/Users/jhelvy/gh/teaching/EDA/2026-Fall/class/12-maps/data/natural_earth_countries/ne_50m_admin_0_countries.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 241 features and 94 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -180 ymin: -89.99893 xmax: 180 ymax: 83.59961
#> Geodetic CRS: WGS 84
st_read()Read in the “World” shape file
#> Reading layer `ne_50m_admin_0_countries' from data source `/Users/jhelvy/gh/teaching/EDA/2026-Fall/class/12-maps/data/natural_earth_countries/ne_50m_admin_0_countries.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 241 features and 94 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -180 ymin: -89.99893 xmax: 180 ymax: 83.59961
#> Geodetic CRS: WGS 84

st_read()Read in the “Central Park” shape file [source]
#> Reading layer `CentralPark' from data source `/Users/jhelvy/gh/teaching/EDA/2026-Fall/class/12-maps/data/central_park/CentralPark.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 2550 features and 6 fields
#> Geometry type: LINESTRING
#> Dimension: XY
#> Bounding box: xmin: -73.99249 ymin: 40.7625 xmax: -73.93922 ymax: 40.8051
#> Geodetic CRS: WGS 84

Use the rnaturalearth library to extract and plot the shape files for China and Africa:


Choropleth - from Greek:

From reddit: r/dataisbeautiful by u/GaudyAudi




Election maps from: http://www-personal.umich.edu/~mejn/election/2016/





Bottom map is actually this map of the 2012 election

Get the “fill” data
Get the “map” data
#> Simple feature collection with 6 features and 2 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -124.7346 ymin: 41.69681 xmax: -82.4146 ymax: 49.36949
#> Geodetic CRS: WGS 84
#> name milk_produced geometry
#> 1 Washington 6.526 MULTIPOLYGON (((-122.753 48...
#> 2 Idaho 14.627 MULTIPOLYGON (((-117.0382 4...
#> 3 Montana 0.288 MULTIPOLYGON (((-116.0482 4...
#> 4 North Dakota 0.345 MULTIPOLYGON (((-104.0476 4...
#> 5 Minnesota 9.864 MULTIPOLYGON (((-97.22609 4...
#> 6 Michigan 11.231 MULTIPOLYGON (((-84.4913 46...
Non-linear scale:







Load the continental US shape file
Read in the coffee shop data
#> # A tibble: 6 × 8
#> name lat long unique_id city state_abb zip state
#> <chr> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr>
#> 1 Baskin Robbins 40.8 -73.4 3304448 Huntington Station NY 11746 New York
#> 2 Baskin Robbins 42.1 -88.0 11342048 Rolling Meadows IL 60008 Illinois
#> 3 Baskin Robbins 34.0 -84.5 3304169 Marietta GA 30066 Georgia
#> 4 Baskin Robbins 29.8 -95.6 3304006 Houston TX 77079 Texas
#> 5 Baskin Robbins 36.4 -89.5 3303959 Tiptonville TN 38079 Tennessee
#> 6 Baskin Robbins 40.7 -73.6 3304507 Merrick NY 11566 New York
Plot coffee shop locations over map
ggplot() +
geom_sf(data = us_states_cont) +
geom_point(
data = us_coffee_shops,
aes(x = long, y = lat, color = name),
size = 0.3
) +
theme_void(base_size = 15) +
theme(legend.position = 'bottom') +
guides(color = guide_legend(
# Move legend title to top
title.position = "top",
# Increase legend point size
override.aes = list(size = 3))) +
labs(
color = 'Coffee shop',
title = 'Coffee Shops in the US'
)
Create this map of squirrels in NYC’s Central Park using this data from the Squirrel Census:
CentralPark.shp file in the data/central_park folder.nyc_squirrels.csv file in the data folder.Hint: The color is mapped to the primary_fur_color variable
(More about the Squirrel Census here)


To modify the projection of a map, use coord_sf(crs = st_crs(XXXX))
ggplot layer:
World
| Code | Projection |
|---|---|
"ESRI:54030" |
Robinson |
"ESRI:54002" |
Equidistant cylindrical |
"ESRI:54004" |
Mercator |
"ESRI:54008" |
Sinusoidal |
"ESRI:54009" |
Mollweide |
United States
| Code | Projection |
|---|---|
"ESRI:102003" |
Albers |
"ESRI:102004" |
Lambert Conformal Conic |
4269 |
NAD 83 |
milk_2017 <- milk_production %>%
filter(year == 2017) %>%
select(name = state, milk_produced) %>%
mutate(milk_produced = milk_produced / 10^9)
us_states <- ne_states(
country = 'united states of america',
returnclass = 'sf') %>%
filter(! name %in% c('Alaska', 'Hawaii')) %>%
left_join(milk_2017, by = 'name')
ggplot(us_states) +
geom_sf(aes(fill = milk_produced)) +
scale_fill_viridis(
option = "plasma",
limits = c(0, 40)) +
theme_void(base_size = 15) +
theme(legend.position = 'bottom') +
labs(
fill = 'Milk produced\n(billions lbs)',
title = 'Milk Production by State in 2017'
)
milk_2017 <- milk_production %>%
filter(year == 2017) %>%
select(name = state, milk_produced) %>%
mutate(milk_produced = milk_produced / 10^9)
us_states <- ne_states(
country = 'united states of america',
returnclass = 'sf') %>%
filter(! name %in% c('Alaska', 'Hawaii')) %>%
left_join(milk_2017, by = 'name')
ggplot(us_states) +
geom_sf(aes(fill = milk_produced)) +
scale_fill_viridis(
option = "plasma",
limits = c(0, 40)) +
theme_void(base_size = 15) +
theme(legend.position = 'bottom') +
labs(
fill = 'Milk produced\n(billions lbs)',
title = 'Milk Production by State in 2017'
) +
coord_sf(crs = "ESRI:102003")Albers Projection

Mapping data to projections - points
us_states_cont <- ne_states(
country = 'united states of america',
returnclass = 'sf') %>%
filter(! name %in% c('Alaska', 'Hawaii'))
us_coffee_shops <- us_coffee_shops %>%
filter(lat > 22, lat < 50,
long > -150, long < -66)
ggplot() +
geom_sf(data = us_states_cont) +
geom_point(
data = us_coffee_shops,
aes(x = long, y = lat, color = name),
size = 0.3) +
theme_void(base_size = 15) +
theme(legend.position = 'bottom') +
guides(color = guide_legend(
# Move legend title to top
title.position = "top",
# Increase legend point size
override.aes = list(size = 3))) +
labs(
color = 'Coffee shop',
title = 'Coffee Shops in the US'
)
Mapping data to projections - points
us_states_cont <- ne_states(
country = 'united states of america',
returnclass = 'sf') %>%
filter(! name %in% c('Alaska', 'Hawaii'))
us_coffee_shops <- us_coffee_shops %>%
filter(lat > 22, lat < 50,
long > -150, long < -66)
ggplot() +
geom_sf(data = us_states_cont) +
geom_point(
data = us_coffee_shops,
aes(x = long, y = lat, color = name),
size = 0.3) +
theme_void(base_size = 15) +
theme(legend.position = 'bottom') +
guides(color = guide_legend(
# Move legend title to top
title.position = "top",
# Increase legend point size
override.aes = list(size = 3))) +
labs(
color = 'Coffee shop',
title = 'Coffee Shops in the US'
) +
coord_sf(crs = "ESRI:102003")
First match us_coffee_shops crs to us_states_cont
us_states_cont <- ne_states(
country = 'united states of america',
returnclass = 'sf') %>%
filter(! name %in% c('Alaska', 'Hawaii'))
us_coffee_shops <- us_coffee_shops %>%
filter(lat > 22, lat < 50,
long > -150, long < -66)
us_coffee_shops_sf <- st_as_sf(us_coffee_shops,
coords = c("long", "lat"),
crs = st_crs(us_states_cont))#> Simple feature collection with 6 features and 6 fields
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -95.60337 ymin: 29.77006 xmax: -73.41174 ymax: 42.05709
#> Geodetic CRS: WGS 84
#> # A tibble: 6 × 7
#> name unique_id city state_abb zip state geometry
#> <chr> <dbl> <chr> <chr> <chr> <chr> <POINT [°]>
#> 1 Baskin Robbins 3304448 Huntington Station NY 11746 New York (-73.41174 40.81772)
#> 2 Baskin Robbins 11342048 Rolling Meadows IL 60008 Illinois (-88.0044 42.05709)
#> 3 Baskin Robbins 3304169 Marietta GA 30066 Georgia (-84.52889 34.02124)
#> 4 Baskin Robbins 3304006 Houston TX 77079 Texas (-95.60337 29.77006)
#> 5 Baskin Robbins 3303959 Tiptonville TN 38079 Tennessee (-89.46582 36.37781)
#> 6 Baskin Robbins 3304507 Merrick NY 11566 New York (-73.55707 40.67515)
Plot coffee shop locations over map with geom_sf()
ggplot() +
geom_sf(data = us_states_cont) +
geom_sf(
data = us_coffee_shops_sf,
aes(color = name),
size = 0.3) +
theme_void(base_size = 15) +
theme(legend.position = 'bottom') +
guides(color = guide_legend(
# Move legend title to top
title.position = "top",
# Increase legend point size
override.aes = list(size = 3))) +
labs(
fill = 'Coffee shop',
title = 'Coffee Shops in the US'
)
Plot coffee shop locations over map with geom_sf()
ggplot() +
geom_sf(data = us_states_cont) +
geom_sf(
data = us_coffee_shops_sf,
aes(color = name),
size = 0.3) +
theme_void(base_size = 15) +
theme(legend.position = 'bottom') +
guides(color = guide_legend(
# Move legend title to top
title.position = "top",
# Increase legend point size
override.aes = list(size = 3))) +
labs(
fill = 'Coffee shop',
title = 'Coffee Shops in the US'
) +
coord_sf(crs = "ESRI:102003")Albers Projection

Plot coffee shop locations over map with geom_sf()
ggplot() +
geom_sf(data = us_states_cont) +
geom_sf(
data = us_coffee_shops_sf,
aes(color = name),
size = 0.3) +
theme_void(base_size = 15) +
theme(legend.position = 'bottom') +
guides(color = guide_legend(
# Move legend title to top
title.position = "top",
# Increase legend point size
override.aes = list(size = 3))) +
labs(
fill = 'Coffee shop',
title = 'Coffee Shops in the US'
) +
coord_sf(crs = "ESRI:102004")LCC Projection

Use the internet_users_country.csv data and the world data frame from the rnaturalearth library to create these two versions of internet access by country in 2015.
Hints:
iso_a3 variable in the worlds data frame corresponds with the code variable in the internet_users_country.csv data frame (use this for joining).scale_fill_gradient() to fill the color:Robinson Projection

Mercator Projection

Use the us_states_cont data frame and the state_abbs data frame to create a labeled map of the U.S.:
