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

如何在R语言绘图的x轴、y轴刻度值上添加度符号及经纬度标识字母

解决方法

核心修改逻辑:关闭image.plot默认生成的坐标轴,自定义带南北/东西标识的经纬度刻度标签即可实现需求,完整修改后的代码如下:

# 原有依赖包加载逻辑不变,此处仅列必要依赖供参考
library(fields)
library(RColorBrewer)
library(maps)
library(Hmisc)

# 绘图主函数,新增axes = FALSE参数关闭默认坐标轴
image.plot(lon_baseline_temp, lat_baseline_temp, dat_baseline_temp, 
           col=rev(brewer.pal(11,"RdBu")), xlab="", 
           ylab="", 
           main="Global surface temperature (Baseline)", sub="Year 1970 ~ 1999", font.sub=2,
           legend.lab="K", legend.line=2.5, legend.mar=7, 
           xaxp=c(-180, 180, 6), yaxp=c(-90, 90, 6), las=1,
           axes = FALSE)

# 自定义X轴(经度)刻度标签
lon_ticks <- seq(-180, 180, 60)
gen_lon_label <- function(x) {
  if (x < 0) {
    return(bquote(.(abs(x))*degree*W))
  } else if (x > 0) {
    return(bquote(.(x)*degree*E))
  } else {
    return(bquote(0*degree))
  }
}
lon_labels <- lapply(lon_ticks, gen_lon_label)
axis(1, at = lon_ticks, labels = lon_labels, las = 1)

# 自定义Y轴(纬度)刻度标签
lat_ticks <- seq(-90, 90, 30)
gen_lat_label <- function(x) {
  if (x < 0) {
    return(bquote(.(abs(x))*degree*S))
  } else if (x > 0) {
    return(bquote(.(x)*degree*N))
  } else {
    return(bquote(0*degree))
  }
}
lat_labels <- lapply(lat_ticks, gen_lat_label)
axis(2, at = lat_ticks, labels = lat_labels, las = 1)

# 原有其余逻辑保持不变
title(ylab = expression(paste("Latitude  "(degree))), line = 2, cex.lab = 1)
title(xlab = expression(paste("Longitude "(degree))), line = 2.5, cex.lab = 1)
minor.tick(nx = 5, ny = 5, tick.ratio = 0.5)
map(database = 'world', add = T, lwd=1.5)

上述代码使用bquote动态生成格式符合要求的刻度标签,和你原有代码的表达式语法兼容,不会出现符号乱码问题,刻度间隔也和你原设置的xaxp、yaxp参数完全匹配。如果后续需要调整刻度密度,只需修改seq()函数中的步长参数即可,标签会自动适配生成对应格式。

内容的提问来源于stack exchange,提问作者Jia Jun Loh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 22:06:06