0

受此网站启发 ( https://thetruesize.com ) 比较国家

我如何绘制road_orlandoroads_miami相互重叠(SAME 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)

4

1 回答 1

1

你可以考虑{cowplot};它最适合 ggplots。

例如,考虑以下代码:

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)

在此处输入图像描述

于 2021-08-02T20:17:24.843 回答