方便地导出具有合理字体和图例大小的高分辨率地图

时间:2015-10-28 12:31:26

标签: r ggplot2 gis shapefile ggmap

我正在使用以下代码创建高分辨率地图:

# Data sourcing -----------------------------------------------------------

# Download an read US state shapefiles
tmp_shps <- tempfile(); tmp_dir <- tempdir()
download.file("http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip",
              tmp_shps)
unzip(tmp_shps, exdir = tmp_dir)

# Libs
require(rgdal); require(ggplot2)

# Read
us_shps <- readOGR(dsn = tmp_dir, layer = "cb_2014_us_state_20m")
# Prepare data set for ggplot2
us_shps_frt <- fortify(us_shps, region = "NAME")

# Fit map -----------------------------------------------------------------

# Lib
require(ggmap)

# Get box for the
bbox <- make_bbox(lon = long, lat = lat,
                  data = us_shps_frt[grep("South", us_shps_frt$id),],
                  f = 0.5)
map_backgr  <- get_map(bbox, maptype = "roadmap", zoom = 5,
                       source = "google", scale = 2, messaging = TRUE)
map_backgr <- ggmap(map_backgr, extent = "normal", maprange = FALSE)

# Libs
require(grid)


# Generate and export -----------------------------------------------------

exp_map <- map_backgr +
  geom_polygon(data = us_shps_frt[grep("South", us_shps_frt$id),],
               aes(x = long, y = lat, group = group,
                   fill = id)) +
  coord_equal() +
  ggtitle("Odd Map with Background") +
  theme_map() +
  theme(legend.background = element_rect(colour = 'black'),
        plot.title = element_text(face = 'bold', size = 7),
        legend.position = c(0.8,0),
        legend.text = element_text(size = 5),
        legend.title = element_text(size = 5, face = 'bold'),
        legend.background = element_rect(size = 0.1, colour = 'black',
                                         linetype = 'solid'),
        plot.margin = unit(rep(5,4),"mm"))


## Export
ggsave(filename = "exp_map.png",
       plot = exp_map, width = 7, height = 7, units = 'cm',
       scale = 2, dpi = 600)

代码生成此地图:

Ugly map

我想更改以下内容:

  1. 图例周围的轮廓线 - 我尝试通过theme设置控制它,但图例轮廓仍然非常大。
  2. 删除文字周围的白边,为什么尽管plot.margin = unit(rep(5,4),"mm")设置已添加到theme,但情节周围仍有足够的白边?

1 个答案:

答案 0 :(得分:2)

您必须使用边距和绘图宽度。以下内容将为您提供所需:

exp_map <- map_backgr +
  geom_polygon(data = us_shps_frt[grep("South", us_shps_frt$id),],
               aes(x = long, y = lat, group = group, fill = id), alpha = 0.7) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  ggtitle("Odd Map with Background") +
  theme(legend.background = element_rect(colour = 'black'),
        plot.title = element_text(face = 'bold', size = 12),
        legend.position = c(0.2,0.2),
        legend.text = element_text(size = 8),
        legend.title = element_text(size = 10, face = 'bold'),
        legend.background = element_rect(colour = 'black', linetype = 'solid'),
        axis.ticks = element_blank(), axis.text = element_blank(), axis.title = element_blank(),
        plot.margin = unit(c(0,-0.4,-1,-1.2),"lines"))

ggsave(filename = "exp_map3.png",
       plot = exp_map, width = 6.7, height = 7, units = 'cm',
       scale = 2, dpi = 600)

如您所见,我还删除了coord_equal()部分。这会导致彩色部分未对齐(例如参见this explanation)。