ggplot2绘制北美地图时纬度轴标签不显示,如何解决?
解决ggplot2绘制北美地图时纬度轴标签不显示的问题
我在用ggplot2绘制北美地图时,经度轴标签能正常显示,但纬度轴标签不出现,怎么设置才能让它显示出来?
library(ggplot2) library(ggspatial) library(dplyr) library(geodata) library(sf) library(tidyr) library(lubridate) # 北美国家 shp 文件 ---- can <- gadm(country = 'CAN', level = 0, path=tempdir()) %>% st_as_sf() usa <- gadm(country = 'USA', level = 0, path=tempdir()) %>% st_as_sf() mex <- gadm(country = 'MEX', level = 0, path=tempdir()) %>% st_as_sf() # 北美周边国家 shp 文件 ---- cc <- country_codes() cca <- cc %>% filter(UNREGION1 %in% c('Caribbean','Central America')) cca <- cca$ISO3 cca <- gadm(country = cca, level = 0, path=tempdir()) %>% st_as_sf() green <- gadm(country = 'GRL', level = 0, path=tempdir()) %>% st_as_sf() rus <- gadm(country = 'RUS', level = 0, path=tempdir()) %>% st_as_sf() # 绘制北美地图 ---- ggplot() + geom_sf(data = green, mapping = aes(geometry = geometry), color = "black") + geom_sf(data = rus, mapping = aes(geometry = geometry), color = "black") + geom_sf(data = can, mapping = aes(geometry = geometry), color = "black") + geom_sf(data = usa, mapping = aes(geometry = geometry), color = "black") + geom_sf(data = cca, mapping = aes(geometry = geometry), color = "black") + geom_sf(data = mex, mapping = aes(geometry = geometry), color = "black") + coord_sf(xlim = c(-180, -48), ylim = c(12, 84), expand = F) + annotation_scale(location = "bl", width_hint = 0.2, text_cex = 1) + annotation_north_arrow(location = "bl", which_north = "true", pad_x = unit(0.15, "in"), pad_y = unit(0.3, "in"), style = north_arrow_fancy_orienteering) + theme_bw() + theme(plot.margin = grid::unit(c(0,2,0,2), "mm"), text = element_text(size = 16), axis.text.x = element_text(size = 14, color = "black"), axis.text.y = element_text(size = 14, color = "black"), panel.grid.major = element_blank(), panel.background = element_rect(fill = "lightblue"))
问题原因
你设置的plot.margin左侧边距仅为2mm,宽度不足以容纳纬度轴标签,导致标签被挤出绘图区域;另外左下角的指北针也可能和标签产生重叠,进一步影响显示。
解决方案
- 增大左侧边距:修改
plot.margin的左侧参数,给纬度标签留出足够空间,比如将grid::unit(c(0,2,0,2), "mm")改为grid::unit(c(0,2,0,10), "mm") - 调整指北针位置:可以把指北针移到右上角(
location = "tr"),或者调整pad_x/pad_y的值,避免和标签重叠
修改后的完整代码
library(ggplot2) library(ggspatial) library(dplyr) library(geodata) library(sf) library(tidyr) library(lubridate) # 北美国家 shp 文件 ---- can <- gadm(country = 'CAN', level = 0, path=tempdir()) %>% st_as_sf() usa <- gadm(country = 'USA', level = 0, path=tempdir()) %>% st_as_sf() mex <- gadm(country = 'MEX', level = 0, path=tempdir()) %>% st_as_sf() # 北美周边国家 shp 文件 ---- cc <- country_codes() cca <- cc %>% filter(UNREGION1 %in% c('Caribbean','Central America')) cca <- cca$ISO3 cca <- gadm(country = cca, level = 0, path=tempdir()) %>% st_as_sf() green <- gadm(country = 'GRL', level = 0, path=tempdir()) %>% st_as_sf() rus <- gadm(country = 'RUS', level = 0, path=tempdir()) %>% st_as_sf() # 绘制北美地图 ---- ggplot() + geom_sf(data = green, mapping = aes(geometry = geometry), color = "black") + geom_sf(data = rus, mapping = aes(geometry = geometry), color = "black") + geom_sf(data = can, mapping = aes(geometry = geometry), color = "black") + geom_sf(data = usa, mapping = aes(geometry = geometry), color = "black") + geom_sf(data = cca, mapping = aes(geometry = geometry), color = "black") + geom_sf(data = mex, mapping = aes(geometry = geometry), color = "black") + coord_sf(xlim = c(-180, -48), ylim = c(12, 84), expand = F) + annotation_scale(location = "bl", width_hint = 0.2, text_cex = 1) + # 调整指北针到右上角避免重叠 annotation_north_arrow(location = "tr", which_north = "true", pad_x = unit(0.15, "in"), pad_y = unit(0.3, "in"), style = north_arrow_fancy_orienteering) + theme_bw() + theme(plot.margin = grid::unit(c(0,2,0,10), "mm"), # 增大左侧边距 text = element_text(size = 16), axis.text.x = element_text(size = 14, color = "black"), axis.text.y = element_text(size = 14, color = "black"), panel.grid.major = element_blank(), panel.background = element_rect(fill = "lightblue"))
内容的提问来源于stack exchange,提问作者tassones
相关产品推荐
相关产品推荐

