Plot two counties side by side while preserving scale

87 Views Asked by At

Is there a way to plot the two counties side by side (without changing actual scale) to compare their sizes.

I wish to plot San Diego and Santa Clara side by side to demonstrate their actual size.

Thanks

library(tigris)
library(ggplot2)

san_diego <- county_subdivisions(state = "CA", county = "San Diego County", cb = TRUE, year = NULL)

santa_clara <- county_subdivisions(state = "CA", county = "Santa Clara County", cb = TRUE, year = NULL)


gg_san_diego <- ggplot() + 
  geom_sf(data = san_diego, 
          color="black",
          fill="white", 
          size=0.25)

gg_santa_clara <- ggplot() + 
  geom_sf(data = santa_clara, 
          color="black",
          fill="white", 
          size=0.25)


gg_san_diego

gg_santa_clara

3

There are 3 best solutions below

5
Allan Cameron On BEST ANSWER

A straightforward way to do this is to move the counties to the same location by subtracting their centroids, then faceting by county name. Here's a fully reproducible example:

library(tigris)
library(tidyverse)
library(sf)

county_subdivisions(state = "CA", cb = TRUE, year = NULL,
                    county = c("San Diego County", "Santa Clara County")) %>%
  group_by(NAMELSADCO) %>%
  mutate(geometry = geometry - st_centroid(st_union(geometry))) %>%
  ggplot() + 
  geom_sf(color = "black", fill = "white") +
  facet_wrap(.~NAMELSADCO) +
  theme_void(base_size = 20) +
  theme(strip.text = element_text(margin = margin(20, 20, 20, 20)),
        panel.background = element_rect(fill = "gray95", color = NA))

enter image description here

2
jkatam On

is this the expected result


combined_data <- rbind(
  data.frame(County = "San Diego", geometry = san_diego$geometry),
  data.frame(County = "Santa Clara", geometry = santa_clara$geometry)
)

ggplot(combined_data, aes(geometry = geometry, fill = County)) +
  geom_sf(color = "black") +
  scale_fill_manual(values = c("blue", "red")) +
  labs(title = "San Diego County vs. Santa Clara County", fill = "County") +
  theme(legend.position = "right")

enter image description here

0
Jonathan V. Solórzano On

One solution is to obtain the centroids of the two objects and set the limits of both plots by the same value (i.e., a constant), using coord_sf. Then, you can put both plots side by side using patchwork.

library(tigris)
library(ggplot2)
library(sf)
library(dplyr)
library(patchwork)

# Constant to sum and subtract from centroid
# to set limits of the plots
constant <- 1

# Calculate centroids for both objects
centroidSD <- san_diego |>
  summarize() |>
  st_centroid() |>
  st_coordinates() |>
  as_tibble()

centroidSC <- santa_clara |>
  summarize() |>
  st_centroid()|>
  st_coordinates() |>
  as_tibble()

# Makes both plots and set x and y limits according to 
# centroid and + / - constant
gg_san_diego <- ggplot() + 
  geom_sf(data = san_diego, 
          color="black",
          fill="white", 
          size=0.25) + 
  coord_sf(xlim = c(centroidSD$X - constant,
                    centroidSD$X + constant),
           ylim = c(centroidSD$Y - constant,
                    centroidSD$Y + constant))

gg_santa_clara <- ggplot() + 
  geom_sf(data = santa_clara, 
          color="black",
          fill="white", 
          size=0.25) + 
  coord_sf(xlim = c(centroidSC$X - constant,
                    centroidSC$X + constant),
           ylim = c(centroidSC$Y - constant,
                    centroidSC$Y + constant))

# Use patchowrk to put both plots side by side
gg_san_diego + gg_santa_clara

twoplots