You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ggplot绘制底图时角度符号显示为方框的问题求助

问题:ggplot结合basemaps绘制底图时,经纬度坐标轴角度符号显示为方框

我用ggplot搭配basemaps包绘制带叠加点的底图,底图默认应该显示带N/W角度符号的经纬度坐标轴(虽然用的是Web Mercator投影),但角度符号显示成了方框。尝试用expression自定义XY轴标签后问题依旧,附上代码和效果,求解决思路。

初始代码

library(sf)
library(basemaps)
library(ggplot2)
library(ggstar)

pt_df = data.frame(X = c(-10069438, -10827213),
                   Y = c(4014585, 4013104),
                   Type = c("MS", "OK"))
pt_df = st_as_sf(pt_df, coords = c("X", "Y"), crs = 3857)
bbox = st_box(pt_df)
bbox["xmin"]<- -11500000
bbox["xmax"]<- -9900000
bbox["ymin"] <- 3000000
bbox["ymax"] <- 4500000 

basemap_ggplot(bbox, map_service="osm_stamen", map_type="terrain")+
geom_star(data = pt_df, aes(x=X, y=Y, fill="red"), size=5)+
xlab("")+
ylab("")

效果图1:坐标轴角度符号显示为方框

自定义标签后的代码

basemap_ggplot(bbox, map_service="osm_stamen", 
               map_type="terrain")+
geom_star(data = pt_df, aes(x=X, y=Y, fill="red"), size=5)+
xlab("")+
ylab("")+
scale_y_continuous(breaks = c(28, 30, 32, 34, 36),
                     labels = c(expression(28~degree~N),
                                expression(30~degree~N),
                                expression(32~degree~N),
                                expression(34~degree~N),
                                expression(36~degree~N)))+
  scale_x_continuous(breaks = c(-100, -95, -90, -85),
                     labels = c(expression(-100~degree~W),
                                expression(-95~degree~W),
                                expression(-90~degree~W),
                                expression(-85~degree~W)))

效果图2:自定义expression标签后,角度符号仍为方框


解决思路

1. 切换支持Unicode的字体

角度符号(°)显示方框核心原因是当前绘图字体不支持该Unicode字符,直接切换到支持的字体即可:

basemap_ggplot(bbox, map_service="osm_stamen", map_type="terrain")+
  geom_star(data = pt_df, aes(x=X, y=Y, fill="red"), size=5)+
  xlab("")+
  ylab("")+
  theme(text = element_text(family = "Arial Unicode MS")) # Windows系统示例
  • Windows可选用"Arial Unicode MS",macOS用"Noto Sans",Linux需提前安装对应字体包。

2. 用Unicode字符替代expression

放弃expression(degree)写法,直接使用Unicode的°符号(输入"°"或"\u00B0"),避免expression带来的字体渲染冲突:

basemap_ggplot(bbox, map_service="osm_stamen", map_type="terrain")+
  geom_star(data = pt_df, aes(x=X, y=Y, fill="red"), size=5)+
  xlab("")+
  ylab("")+
  scale_y_continuous(breaks = c(28, 30, 32, 34, 36),
                     labels = paste0(c(28, 30, 32, 34, 36), "°N"))+
  scale_x_continuous(breaks = c(-100, -95, -90, -85),
                     labels = paste0(c(-100, -95, -90, -85), "°W"))

3. 统一投影坐标系

手动将bbox转换为WGS84(EPSG:4326)后传入basemap_ggplot,确保坐标轴标签渲染环境一致:

bbox_wgs84 = st_transform(bbox, crs = 4326)
basemap_ggplot(bbox_wgs84, map_service="osm_stamen", map_type="terrain", crs=3857)+
  geom_star(data = pt_df, aes(x=X, y=Y, fill="red"), size=5)+
  xlab("")+
  ylab("")

4. 更新相关包

旧版本的sf、basemaps或ggplot2可能存在字体渲染bug,更新到最新版本尝试:

update.packages(c("sf", "basemaps", "ggplot2", "ggstar"))

内容的提问来源于stack exchange,提问作者Jade131621

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 09:55:22