如何移除ggplot2绘制地图时显示的经纬度坐标信息
代码修改方法
要移除地图上所有经纬度坐标相关的显示元素,仅保留行政边界和点位要素,只需要在现有绘图代码的theme_bw()后追加主题配置,屏蔽所有坐标轴类渲染元素即可,不需要改动其他数据处理、图层绘制的逻辑。
修改后的完整可复现代码如下:
library(rgdal) library(ggplot2) library(sf) temp <- tempfile() temp2 <- tempfile() download.file("https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2015/UFs/PR/pr_municipios.zip",temp) unzip(zipfile = temp, exdir = temp2) shp <- sf::read_sf(temp2) shp_subset <- shp[shp$NM_MUNICIP == "CASTRO",] # 点位数据 Points_properties<-structure(list(Latitude = c(-24.781624, -24.775017, -24.769196, -24.761741, -24.752019, -24.748008, -24.737312, -24.744718, -24.751996, -24.724589, -24.8004, -24.796899, -24.795041, -24.780501, -24.763376, -24.801715, -24.728005, -24.737845, -24.743485, -24.742601, -24.766422, -24.767525, -24.775631, -24.792703, -24.790994, -24.787275, -24.795902, -24.785587, -24.787558, -24.799524), Longitude = c(-49.937369, -49.950576, -49.927608, -49.92762, -49.920608, -49.927707, -49.922095, -49.915438, -49.910843, -49.899478, -49.901775, -49.89364, -49.925657, -49.893193, -49.94081, -49.911967, -49.893358, -49.903904, -49.906435, -49.927951, -49.939603, -49.941541, -49.94455, -49.929797, -49.92141, -49.915141, -49.91042, -49.904772, -49.894034, -49.86651), cluster = c("1", "1", "1", "1", "2", "2", "2", "2", "2", "2", "1", "1", "1", "1", "1", "1", "1")), row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame")) ggplot() + geom_sf(data = shp_subset) + geom_point(data = Points_properties, aes(x = Longitude, y = Latitude))+ coord_sf(xlim = c(min(Points_properties$Longitude)-0.1, max(Points_properties$Longitude)+0.1), ylim = c(min(Points_properties$Latitude)-0.1, max(Points_properties$Latitude)+0.1), expand = FALSE) + theme_bw() + # 新增配置:移除所有坐标相关显示元素 theme( axis.text = element_blank(), axis.ticks = element_blank(), axis.title = element_blank() )
配置说明
新增的主题参数作用如下:
axis.text = element_blank():隐藏x、y轴上的经纬度刻度文字标签axis.ticks = element_blank():隐藏x、y轴上对应刻度的短标记线axis.title = element_blank():隐藏x、y轴的轴标题(即经纬度名称标注)
如果不需要绘图区最外侧的默认边框,仅保留shp数据自带的行政边界,只需要在上述theme参数中追加一行panel.border = element_blank()即可。
内容的提问来源于stack exchange,提问作者Antonio
相关产品推荐
相关产品推荐

