hadley / ggplot2-book

ggplot2: elegant graphics for data analysis
https://ggplot2-book.org/
1.57k stars 683 forks source link

Section 16.2.3 Out of Date, w/ broken-looking maps #363

Open jack-davison opened 1 year ago

jack-davison commented 1 year ago

Good afternoon, and thank you for all your work with {ggplot2} and the ggplot2 book.

I was going to refer someone to Chapter 16 of the ggplot2 book and noted that section 16.2.3 seems out of date, with some broken appearing maps.

Outdated info?

Maps are intrinsically displays of spherical data. Simply plotting raw longitudes and latitudes is misleading, so we must project the data. There are two ways to do this with ggplot2:

Nowadays there are three ways to do this in {ggplot2} w/ coord_sf(), so should this read three ways? Further, coord_map() and coord_quickmap() are superseded - a blog on RStudio Education suggested not teaching superseded functions. I appreciate this may be personal opinion for the author, but it may be useful for readers to know how to recreate the visualisations below using coord_sf().

Broken-looking graphics

At the bottom of this page, there are a couple of broken looking maps:

  1. On the left hand side, the world map has a load of bands cutting across the center of it.
  2. The far right plot looks like a little splodge rather than an actual map.

image

Pulling together

May it be appropriate to focus on coord_sf() in section 16.2.3 and make more passing mention to coord_map()/coord_quickmap()? Or at least provide more direct guidance on the recommended coordinate system for map coordinates, as some may be put off by the "superseded" label on coord_map() but have no idea how to properly use coord_sf().

library(ggplot2)
library(patchwork)

world <- map_data("world")
worldmap <- ggplot(world, aes(long, lat, group = group)) +
  geom_path() +
  theme_minimal()

latlng <- worldmap + coord_sf(default_crs = 4326)

# Some crazier projections
ortho <- worldmap +
  coord_sf(default_crs = 4326, 
           crs = "+proj=ortho")

stereo <- worldmap +
  coord_sf(default_crs = 4326, crs = 3995) +
  scale_y_continuous(limits = c(-40, 40))

latlng + ortho + stereo
#> Warning: Removed 49207 rows containing missing values (`geom_path()`).

Created on 2023-04-27 with reprex v2.0.2

Thanks again, Jack