0

Inspired from this website (https://thetruesize.com) to compare countries

How do I plot road_orlando and roads_miami over each other (SAME SCALE) to visually compare the road network sprawl.

Edit - I had earlier asked to plot side by side in small scale.

library(tigris)

roads_orlando <- roads(state = "FL",
               county = c("Orlando"))

roads_miami <- roads(state = "FL",
               county = c("Miami-Dade"))

class(roads_orlando)
plot(roads_orlando$geometry)

class(roads_miami)
plot(roads_miami$geometry)

SiH
  • 1,378
  • 4
  • 18

1 Answers1

1

You can consider {cowplot}; it works the best with ggplots.

For an example consider this code:

library(tigris)

roads_orlando <- roads(state = "FL",
                       county = c("Orange")) # Orlando does not seem to be a legit county name :(

roads_miami <- roads(state = "FL",
                     county = c("Miami-Dade"))

# Miami is bigger of the two; we need to enlarge Orlando a bit

bbox_miami <- st_bbox(roads_miami) # bouding box of Miami

width_miami <- bbox_miami[3] - bbox_miami[1]
height_miami <- bbox_miami[4] - bbox_miami[2]

# centroid of Orlando; to this we will add the extent of Miami
orlando_centroid <- st_centroid(st_bbox(roads_orlando) %>%
                                  st_as_sfc())

library(ggplot2)

gg_or <- ggplot(data = roads_orlando) +
  geom_sf() +
  coord_sf(xlim = c(st_coordinates(orlando_centroid)[1] - width_miami / 2,
                    st_coordinates(orlando_centroid)[1] + width_miami / 2), 
           ylim = c(st_coordinates(orlando_centroid)[2] - height_miami / 2,
                    st_coordinates(orlando_centroid)[2] + height_miami / 2))

gg_md <- ggplot(data = roads_miami) +
  geom_sf() 

cowplot::plot_grid(gg_or, gg_md)

enter image description here

Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44
  • Thanks @Jindra Lacko, But I want to plot them in the same scale to compare the sprawl of network. – SiH Aug 02 '21 at 20:23
  • Oki, I get that. Consider the updated plot (and code) - it forces the plot of Orlando road network (or rather of the Orange cnty) over the same extent as Miami Dade. The plots are then drawn side to side. – Jindra Lacko Aug 02 '21 at 21:20