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

如何基于Beta系数绘制多时段不同土地覆被类型的选择效应图

解决方案

核心思路

利用ggplot2的分面功能按Time_period拆分三个时段的子图,通过颜色映射区分两种土地覆被类型,同时保留原有的可视化风格(误差棒、参考线、主题样式)。

完整绘图代码

library(ggplot2)

# 加载数据集(替换为你实际的数据集名称)
beta_data <- structure(list(Variable = c("mottes", "root-plowed", "mottes", 
"root-plowed", "mottes", "root-plowed"), Time_period = c("midday", 
"midday", "night", "night", "crepuscular", "crepuscular"), Estimate = c(0.112762883871621, 
0.978136075960821, 0.330800972033945, -0.274796168643408, 0.39961563211462, 
-0.216926744117051), SE = c(0.19717904127417, 0.126627496764976, 
0.152794807096571, 0.155299309443016, 0.121171489524617, 0.0985049367369206
), `z value` = c(0.571880678306114, 7.72451561430032, 2.16500140495527, 
-1.76946162625559, 3.29793447024876, -2.20219159874598), `Pr(>|z|)` = c(0.567402814817547, 
1.12279478002282e-14, 0.0303875776790643, 0.0768168691956359, 
0.000973988621992874, 0.0276517772569329), CI = c(0.386470920897372, 
0.248189893659353, 0.299477821909279, 0.30438664650831, 0.237496119468249, 
0.193069676004364)), row.names = c("land_typemottes", "land_typeroot-plowed", 
"land_typemottes1", "land_typeroot-plowed1", "land_typemottes2", 
"land_typeroot-plowed2"), class = "data.frame")

ggplot(beta_data, aes(x = Variable, y = Estimate, color = Variable)) +
  # 绘制误差棒,位置偏移确保与散点对齐
  geom_errorbar(aes(ymin = Estimate - CI, ymax = Estimate + CI),
                width = 0.2, position = position_dodge(0.9)) +
  # 绘制散点,大小可按需调整
  geom_point(size = 4, position = position_dodge(0.9)) +
  # 添加y=0参考线,保留原样式
  geom_hline(yintercept = 0, linetype = "dashed", color = "red", size = 0.5) +
  # 按时段分面,垂直排列子图
  facet_wrap(~ Time_period, ncol = 1) +
  # 统一y轴范围,与原代码一致
  ylim(-0.6, 1.3) +
  # 自定义颜色:橙色对应mottes,绿色对应root-plowed
  scale_color_manual(values = c("mottes" = "#FFA500", "root-plowed" = "#008000")) +
  # 坐标轴和图例标题
  labs(y = "Selection", x = "Landcover Type", color = "Landcover Type") +
  # 主题设置:保留原简洁风格,优化元素可读性
  theme(
    axis.text = element_text(size = 30),
    axis.title = element_text(size = 30, face = "bold"),
    strip.text = element_text(size = 25),
    legend.text = element_text(size = 20),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    axis.line = element_line(colour = "black")
  )

关键修改说明

  1. 数据整合:直接使用包含所有时段的完整数据框,无需拆分单个时段数据集
  2. 颜色映射:通过scale_color_manual指定土地覆被类型的颜色,完全匹配需求
  3. 分面布局:facet_wrap(~ Time_period, ncol = 1)实现三个时段子图的垂直排列,与目标图结构一致
  4. 元素对齐:position_dodge(0.9)确保同一类别下的散点和误差棒对齐,避免视觉重叠
  5. 主题优化:保留原无网格线、黑色坐标轴的简洁风格,同时调整分面标题和图例的字体大小提升可读性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 01:32:04