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

制作含降水量与均值、极值温度的双轴气象图表

双轴温度-降水量图表解决方案

核心问题修正

你已经实现了降水量柱状图和均值温度散点,但无法绘制温度线图及最低/最高温度点,主要原因是geom_line需要明确分组规则,同时多温度系列的y值映射需统一转换逻辑。


单夜间图表修改代码

# 定义副轴转换系数
a <- 0.2
b <- 1

Night %>% 
  mutate(Night = fct_reorder(Night, Order)) %>% 
  ggplot(aes(x = Night)) +
  # 降水量柱状图
  geom_col(aes(y = Precip..Accum..Mm), fill = "steelblue") +
  # 均值温度:点+线
  geom_point(aes(y = a + Average.of.Temp*b), color = "red", size = 2) +
  geom_line(aes(y = a + Average.of.Temp*b, group = 1), color = "red", linewidth = 1) +
  # 最低温度:点+虚线
  geom_point(aes(y = a + min.of.Temp*b), color = "blue", size = 2) +
  geom_line(aes(y = a + min.of.Temp*b, group = 1), color = "blue", linewidth = 1, linetype = "dashed") +
  # 最高温度:点+点线
  geom_point(aes(y = a + max.of.Temp*b), color = "darkgreen", size = 2) +
  geom_line(aes(y = a + max.of.Temp*b, group = 1), color = "darkgreen", linewidth = 1, linetype = "dotted") +
  # 双轴配置
  scale_y_continuous(
    name = "Precipitation (mm)",
    sec.axis = sec_axis(~ (. - a)/b, name = "Temperature (°C)")
  ) +
  # 标题与主题
  ggtitle("Night time temperatures and precipitation during monitoring period") +
  theme_bw() +
  theme(
    axis.line.y.right = element_line(color = "red"), 
    axis.ticks.y.right = element_line(color = "red"),
    axis.text.y.right = element_text(color = "red"), 
    axis.title.y.right = element_text(color = "red"),
    axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)
  )

分面图表优化代码

a <- 0.2
b <- 1

Day_Night %>% 
  mutate(Night = fct_reorder(Night, Order)) %>% 
  ggplot(aes(x = Night)) +
  geom_col(aes(y = Precip..Accum..Mm), fill = "steelblue") +
  # 均值温度点线
  geom_point(aes(y = a + Mean*b), color = "red", size = 2) +
  geom_line(aes(y = a + Mean*b, group = 1), color = "red", linewidth = 1) +
  # 最低温度点线
  geom_point(aes(y = a + min.of.Temp*b), color = "blue", size = 2) +
  geom_line(aes(y = a + min.of.Temp*b, group = 1), color = "blue", linewidth = 1, linetype = "dashed") +
  # 最高温度点线
  geom_point(aes(y = a + max.of.Temp*b), color = "darkgreen", size = 2) +
  geom_line(aes(y = a + max.of.Temp*b, group = 1), color = "darkgreen", linewidth = 1, linetype = "dotted") +
  # 双轴与辅助线
  scale_y_continuous(
    name = "Precipitation (mm)",
    sec.axis = sec_axis(~ (. - a)/b, name = "Temperature (°C)")
  ) +
  geom_hline(yintercept = 10, color = "red", linetype = "dashed") +
  # 分面与主题
  facet_grid(~DN) +
  ggtitle("Day and Night time temperatures and precipitation during monitoring period") +
  theme_bw() +
  theme(
    axis.line.y.right = element_line(color = "red"), 
    axis.ticks.y.right = element_line(color = "red"),
    axis.text.y.right = element_text(color = "red"), 
    axis.title.y.right = element_text(color = "red"),
    axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)
  )

关键修改说明

  1. 分组规则:给geom_line添加group = 1,因为x轴是分类变量,ggplot需要明确分组才能连接不同x值的点
  2. 统一转换:最低/最高温度的y值转换逻辑和均值保持一致,确保所有温度数据映射到副轴对应范围
  3. 样式区分:用颜色、线型区分不同温度系列,提升图表可读性
  4. 主题顺序:将theme_bw()放在自定义主题前,避免基础样式被覆盖

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 15:47:03