You need to enable JavaScript to run this app. R语言马赛克图标签修改、坐标轴设置及数值标注求助
优惠活动
大模型
产品
解决方案
定价
更多

R语言马赛克图标签修改、坐标轴设置及数值标注求助

解决方案

原有代码的核心问题是数据读取格式错误+mosaicplot函数用法不符合需求:你读入的数值列因为带千位分隔符、NA识别错误被转成了因子类型,且基础包的mosaicplot默认针对列联表计数数据做可视化,不适合你这种国家-灾害对应的数值型数据展示。

下面用tidyverse+ggmosaic包实现你要的效果:x轴为灾害类型、y轴为国家、色块大小对应灾害数值、色块上标注数值。

步骤1:数据预处理

先把数据修正为正确的数值格式:

# 未安装依赖包先运行 install.packages(c("tidyverse","ggmosaic"))
library(tidyverse)
library(ggmosaic)

# 如果读本地csv,用下面的代码替换即可:
# trial_one <- read.csv("trial_one.csv", na.strings = "NA") |> 
#   mutate(across(-ISO, ~as.numeric(str_remove_all(.x, ","))))

# 直接用你提供的结构生成正确数据集
trial_one <- structure(list(ISO = structure(1:9, .Label = c("AGO", "BDI",  "BEN", "BFA", "BWA", "CAF", "CIV", "CMR", "COD"), class = "factor"),  
                            Flood = structure(c(1L, 5L, 3L, 9L, 2L, 8L, 6L, 4L, 7L), .Label = c("5,000",  "833", "260", "655", "500", "987", "300",  "5,000", "6,248"), class = "factor"), 
                            Storm = structure(c(4L,  2L, 4L, 4L, 4L, 1L, 4L, 4L, 3L), .Label = c("4,000", "500",  "50", "NA"), class = "factor"), 
                            Cyclone = structure(c(2L,  2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L), .Label = c("400", "NA" ), class = "factor"), 
                            Heavy.rain = structure(c(4L, 3L, 4L,  4L, 4L, 1L, 4L, 4L, 2L), .Label = c("644", "254",  "800", "NA"), class = "factor"), 
                            Drought = structure(c(2L,  2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("2,500", "NA" ), class = "factor"), 
                            Landslides = structure(c(5L, 4L, 5L,  5L, 5L, 5L, 1L, 2L, 3L), .Label = c("4,000", "520", "40",  "3600", "NA"), class = "factor")), 
                       class = "data.frame", row.names = c(NA,  -9L)) |> 
  # 把除了ISO的所有列转成数值,去掉千位逗号,NA转成缺失值
  mutate(across(-ISO, ~as.numeric(str_remove_all(.x, ","))))

步骤2:绘制目标马赛克图(色块大小对应数值)

# 先把宽数据转成长数据
df_long <- trial_one |> 
  pivot_longer(cols = -ISO, names_to = "灾害类型", values_to = "数值") |> 
  # 过滤掉NA的行,不需要过滤可以删除这一行
  filter(!is.na(数值))

ggplot(data = df_long) +
  geom_mosaic(aes(weight = 数值, x = product(灾害类型), fill = ISO)) +
  # 标签调整
  labs(x = "灾害类型", y = "国家(ISO)",
       title = "Enviromental changes by Country",
       sub = "SSA",
       fill = "国家代码") +
  # 添加数值标签
  geom_text(mapping = aes(x = product(灾害类型), y = product(ISO),
                          label = 数值),
            size = 3) +
  # 主题调整,x轴标签倾斜避免重叠
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

可选:等大格子样式(不需要色块按数值缩放)

如果你只需要每个国家-灾害组合为等大格子、仅标注数值,用geom_tile实现更简单:

ggplot(df_long, aes(x = 灾害类型, y = ISO)) +
  geom_tile(fill = "skyblue2", color = "chocolate") +
  geom_text(aes(label = 数值), size = 3.5) +
  labs(x = "灾害类型", y = "国家(ISO)",
       title = "Enviromental changes by Country",
       sub = "SSA") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.08 11:21:03