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

使用ggplot2与sf绘制德国地图时经纬度度数符号显示异常

问题:ggplot2+sf绘制德国地图时经纬度度数符号显示为方框

首次使用R语言的ggplot2和sf包绘制德国地理地图,流程为:从shapefile导入地理数据、csv文件导入数值数据,合并为数据框后绘图。正常情况下经纬度应显示度数符号(如50°N),但运行代码后度数符号被方框替代(如54□N)。

使用的代码

shp_file <- st_read("GIS/NUTS_RG_01M_2010_3035.shp") 

de_shp_file <- themap %>%
  filter(shp_file$CNTR_CODE == "DE")

de_data <- read.csv("K....csv", sep=";")

de_df <- merge(de_shp_file, de_data, by = "NUTS_ID", rm.na=FALSE)

de_df %>%
  filter(period == "1949 - 1978") %>%
     ggplot(aes(fill = var1)) + geom_sf() + labs(title = "my title")

当前加载的包列表

[1] "sqldf"        "RSQLite"      "gsubfn"       "proto"        "MASS"
[6] "officer"      "officedown"   "flextable"    "remotes"      "sm"
[11] "reshape2"     "knitr"        "leaflet"      "tmaptools"    "tmap"
[16] "sf"         "RColorBrewer" "quantreg"     "SparseM"      "ggspectra"
[21] "photobiology" "fable"        "feasts"       "fabletools"   "tsibbledata"
[26] "tsibble"      "fpp3"         "writexl"      "psych"        "lubridate"
[31] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"
[36] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "readxl"
[41] "stats"        "graphics"     "grDevices"    "utils"        "datasets"
[46] "methods"      "base"

解决方法

  • 指定支持Unicode的字体
    度数符号属于Unicode字符,当前绘图字体不支持该字符是最常见原因。可以通过theme()强制使用支持特殊字符的字体,例如:

    de_df %>%
      filter(period == "1949 - 1978") %>%
      ggplot(aes(fill = var1)) + 
      geom_sf() + 
      labs(title = "my title") +
      theme(axis.text = element_text(family = "Arial Unicode MS")) # 替换为你的系统可用字体
    

    若不确定系统支持的字体,可使用extrafont包查看:

    install.packages("extrafont")
    library(extrafont)
    font_import() # 导入系统字体
    fonts() # 查看所有可用字体
    
  • 转换地图投影
    当前shapefile使用的是EPSG:3035(平面投影),可能导致坐标轴标签字符编码异常。尝试转换为WGS84(EPSG:4326,经纬度投影)后绘图:

    de_shp_file_wgs84 <- st_transform(de_shp_file, crs = 4326)
    de_df_wgs84 <- merge(de_shp_file_wgs84, de_data, by = "NUTS_ID", rm.na=FALSE)
    
    de_df_wgs84 %>%
      filter(period == "1949 - 1978") %>%
      ggplot(aes(fill = var1)) + 
      geom_sf() + 
      labs(title = "my title")
    
  • 调整R的字符编码
    确保R的字符编码设置为UTF-8,避免字符解析错误:

    # 查看当前编码
    Sys.getlocale()
    # 设置为UTF-8(Windows系统可尝试"Chinese (Simplified)_China.UTF-8")
    Sys.setlocale(category = "LC_ALL", locale = "en_US.UTF-8")
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 13:45:08