简体   繁体   English

裁剪用 geom_polygon 制作的 map

[英]Cropping a map made with geom_polygon

I am creating a map using geom_polygon :我正在使用geom_polygon

## libraries
library(tidyverse)

## render map
map_data("state") %>% 
  ggplot(mapping = aes(x = long, y = lat, group = group)) +
  geom_polygon(color = "gray") +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        panel.grid = element_blank(),
        panel.background = element_blank())

The map renders correctly, however, as you will see, there is a ton of white space on either side of the map: map 可以正确渲染,但是,正如您将看到的,map 的两侧都有大量空白区域:

在此处输入图像描述

Is there anyway to automatically crop the map so as shown below?有没有自动裁剪 map 如下所示?

在此处输入图像描述

You need to add limits to your coord_map call.您需要为coord_map调用添加限制。

map_data("state") %>% 
  ggplot(mapping = aes(x = long, y = lat, group = group)) +
  geom_polygon(color = "gray") +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45,
            xlim = c(-117,-75), ylim = c(26,49)) +
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        panel.grid = element_blank(),
        panel.background = element_blank())

Then make sure your graphics device has dimensions that match your plot:然后确保您的图形设备的尺寸与您的 plot 相匹配:

ggsave("~/temp.png",width = 1.55 * 5, height = 5)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM