如何移动地图并在R中删除其边框

发布于 2025-02-12 07:09:45 字数 738 浏览 1 评论 0原文

我已经使用此代码创建了一个世界地图

graph_world <- 
  rnaturalearth::ne_countries(
    type = "countries",
    scale = "large",
    returnclass = "sf") %>% 
  filter(admin != "Antarctica") %>% 
  ggplot() +
  ggplot2::geom_sf(
    fill = "#264653", # Country colour
    size = 0          # Line size
    ) +
  ggplot2::coord_sf(
    expand = TRUE,
    default_crs = sf::st_crs(4326),
    xlim = c(-180, 180),
    ylim = c(-90, 90)) 

graph_world

“该图看起来像this”

我想:

  1. 卸下左侧和右侧的白色垂直边界。我希望他们要透明。
  2. 水平移动地图,以使左手边框穿过白令海峡,而不是将Chukotka从俄罗斯其他地区割下。

请注意,删除边界的一种方法是设置coord_sf(展开= f),但这也将删除我要保留的图的任何一侧的空白。

I've created a world map with this code

graph_world <- 
  rnaturalearth::ne_countries(
    type = "countries",
    scale = "large",
    returnclass = "sf") %>% 
  filter(admin != "Antarctica") %>% 
  ggplot() +
  ggplot2::geom_sf(
    fill = "#264653", # Country colour
    size = 0          # Line size
    ) +
  ggplot2::coord_sf(
    expand = TRUE,
    default_crs = sf::st_crs(4326),
    xlim = c(-180, 180),
    ylim = c(-90, 90)) 

graph_world

The graph looks like this!

I would like to:

  1. Remove the white vertical borders on the left and the right. I'd like them to be transparent.
  2. Shift the map horizontally so that the left hand border goes through the Bering Strait rather than cutting Chukotka off from the rest of Russia.

Note that one way of removing the borders is to set coord_sf(expand = F) but this would also remove the empty space either side of the graph which I want to keep.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情仇皆在手 2025-02-19 07:09:50

旋转的解决方案是使用ST_CRS()移动投影。我们可以将Prime子午线设置为格林威治以东10度的主要子午线,“+pm = 10”将旋转地图。

# Download map data
df_world <- 
  rnaturalearth::ne_countries(
    type = "countries",
    scale = "large",
    returnclass = "sf") %>%
  st_make_valid()

# Specify projection 
target_crs <- 
  st_crs(
    paste0(
      "+proj=robin ", # Projection
      "+lon_0=0 ",
      "+x_0=0 ",
      "+y_0=0 ",
      "+datum=WGS84 ",
      "+units=m ",
      "+pm=10 ", # Prime meridian 
      "+no_defs"
    )
  )

但是,如果我们绘制此图,它将在整个地图上创建“条纹”,该地图现在在地图边缘上破坏了以前的连续国家。

我们只需要根据与新地图边缘对齐的微小多边形切片解决方案在这里

# Specify an offset that is 180 - the lon the map is centered on
offset <- 180 - 10 # lon 10

# Create thin polygon to cut the adjusted border 
polygon <- 
  st_polygon(
    x = list(rbind(
      c(-0.0001 - offset, 90),
      c(0 - offset, 90),
      c(0 - offset, -90),
      c(-0.0001 - offset, -90),
      c(-0.0001 - offset, 90)))) %>%
  st_sfc() %>%
  st_set_crs(4326)


# Remove overlapping part of world
df_world <- 
  df_world %>% 
  st_difference(polygon)

# Transform projection
df_world <- 
  df_world %>% 
  st_transform(crs = target_crs)

# Clear environment 
rm(polygon, target_crs)

然后我们在上面运行上面的代码为了旋转地球而没有污迹。

# Create map 
graph_world <- 
  df_world %>% 
  ggplot() +
  ggplot2::geom_sf(
    fill = "#264653", # Country colour
    colour = "#FFFFFF", # Line colour
    size = 0          # Line size
  ) +
  ggplot2::coord_sf(
    expand = TRUE,
    default_crs = sf::st_crs(4326),
    xlim = c(-180, 180),
    ylim = c(-90, 90))

# Print map
graph_world

The solution to the rotation is to shift the the projection using st_crs(). We can set the Prime Meridian as 10 degrees east of Greenwich with "+pm=10 " which rotates the map.

# Download map data
df_world <- 
  rnaturalearth::ne_countries(
    type = "countries",
    scale = "large",
    returnclass = "sf") %>%
  st_make_valid()

# Specify projection 
target_crs <- 
  st_crs(
    paste0(
      "+proj=robin ", # Projection
      "+lon_0=0 ",
      "+x_0=0 ",
      "+y_0=0 ",
      "+datum=WGS84 ",
      "+units=m ",
      "+pm=10 ", # Prime meridian 
      "+no_defs"
    )
  )

However, if we graph this it will create 'streaks' across the map where countries that were previously contiguous are now broken across the map edge.

streaky map

We just need to slice our map along a tiny polygon aligned with our new map edge, based on the solution here

# Specify an offset that is 180 - the lon the map is centered on
offset <- 180 - 10 # lon 10

# Create thin polygon to cut the adjusted border 
polygon <- 
  st_polygon(
    x = list(rbind(
      c(-0.0001 - offset, 90),
      c(0 - offset, 90),
      c(0 - offset, -90),
      c(-0.0001 - offset, -90),
      c(-0.0001 - offset, 90)))) %>%
  st_sfc() %>%
  st_set_crs(4326)


# Remove overlapping part of world
df_world <- 
  df_world %>% 
  st_difference(polygon)

# Transform projection
df_world <- 
  df_world %>% 
  st_transform(crs = target_crs)

# Clear environment 
rm(polygon, target_crs)

Then we run the code above to get a rotated earth without smudges.

# Create map 
graph_world <- 
  df_world %>% 
  ggplot() +
  ggplot2::geom_sf(
    fill = "#264653", # Country colour
    colour = "#FFFFFF", # Line colour
    size = 0          # Line size
  ) +
  ggplot2::coord_sf(
    expand = TRUE,
    default_crs = sf::st_crs(4326),
    xlim = c(-180, 180),
    ylim = c(-90, 90))

# Print map
graph_world

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文