方便地导出具有合理字体和图例大小的高分辨率地图
Conveniently exporting high resolution map with sensible font and legend sizes
我正在使用以下代码创建高分辨率地图:
# 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)
代码生成这张地图:
我想更改以下内容:
- 图例周围的轮廓线 - 我试图通过
theme
设置来控制它,但图例轮廓仍然大得离谱。
- 移除文本周围的白边,为什么尽管
plot.margin = unit(rep(5,4),"mm")
设置添加到 theme
情节周围有充足的白边?
您需要稍微调整一下图的边距和宽度。以下将为您提供您所需要的:
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()
部分。这会导致彩色部分未对齐(参见示例 )。
我正在使用以下代码创建高分辨率地图:
# 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)
代码生成这张地图:
我想更改以下内容:
- 图例周围的轮廓线 - 我试图通过
theme
设置来控制它,但图例轮廓仍然大得离谱。 - 移除文本周围的白边,为什么尽管
plot.margin = unit(rep(5,4),"mm")
设置添加到theme
情节周围有充足的白边?
您需要稍微调整一下图的边距和宽度。以下将为您提供您所需要的:
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()
部分。这会导致彩色部分未对齐(参见示例