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

如何在多尺度气候图中无失真嵌入温度折线图(ylim=27-30)

多尺度气候指标可视化解决方案

问题描述

需要绘制包含温度(Y轴范围27-30)、光照强度、相对湿度的气候图。因各指标Y轴尺度差异大,直接将温度折线插入已有双折线图(Y轴0-120)时,温度波动无法清晰显示。尝试用ggplot的wrap_elements融合组件,但温度图无法保留单独绘制时的波动效果。

原代码如下:

湿度与光照图代码

plot_humidity <- ggplot(df, aes(x = datetime, y = mean_humidity)) +
  geom_line(aes(y = mean_humidity, color = "Rel. humidity", group = 1), linewidth = 1) +
  geom_line(aes(y = mean_Light/100, color = "Light intensity", group = 2), linewidth = 1) +
  geom_line(aes(y = mean_Temperature, color = "Temperature", group = 3), linewidth = 1) +
  geom_ribbon(aes(ymin= mean_humidity - sd_humidity,
                  ymax= mean_humidity + sd_humidity,
                  fill = "Rel. humidity",  color = "Rel. humidity"),
              alpha=0.2, group = 1) +
  geom_ribbon(aes(ymin= (mean_Light/100) - (sd_Light/100),
                  ymax= (mean_Light/100) + (sd_Light/100),
                  fill = "Light intensity", color = "Light intensity"),
              alpha=0.2, group = 2) +
  geom_ribbon(aes(ymin= mean_Temperature - sd_Temperature,
                  ymax= mean_Temperature + sd_Temperature,
                  fill = "Temperature", color = "Temperature"),
              alpha=0.2, group = 3) +
  labs( x = "time", y = "relative humidity (in %)", color = "Variable") +
  scale_color_manual(values = c("Light intensity" = "green", "Temperature" = "red", "Rel. humidity" = "blue")) +
  scale_fill_manual(values = c("Light intensity" = "green", "Temperature" = "red", "Rel. humidity" = "blue")) +
  scale_x_datetime(breaks = "1 hour", date_labels = "%H",
                   limits = as.POSIXct(strptime(c("2022-12-23 01:00:00", "2022-12-24 01:00:00"),
                                                format = "%Y-%m-%d %H:%M:%S")),
                   expand = c(0, 0)) +
  theme(axis.line.y.left =  element_line( color = "blue"),
        axis.text.y.left = element_text(color = "blue"),
        plot.margin = margin(10, 10, 10, 30)) +
  guides(fill = "none") +
  scale_y_continuous(limits = c(), sec.axis = sec_axis(~ . * 100, name = "light intensity (in lux)")) +
  theme(axis.text.y.right = element_text(color = "green"),
        axis.line.y.right = element_line(color = "green"))


plot <- wrap_elements(get_plot_component(plot_temperature, "ylab-l")) +
  wrap_elements(get_y_axis(plot_temperature)) +
  plot_humidity +
  plot_layout(widths = c( 3, 1, 40))

温度图代码

plot_temperature <- ggplot(df, aes(x = datetime, y = mean_Temperature)) +
  geom_line(aes(y= mean_Temperature, color = "Temperature"), linewidth = 1) +
  geom_ribbon(aes(ymin= mean_Temperature - sd_Temperature,
                  ymax= mean_Temperature + sd_Temperature,
                  fill = "Temperature", color = "Temperature"), alpha = 0.2) +
  labs(y = "mean temperature (in °C) +/- standard deviation") +
  scale_color_manual(values = "red") +
  theme(axis.line.y.left =  element_line( color = "red"),
        axis.text.y.left = element_text(color = "red"),) +
  scale_x_datetime(breaks = "1 hour", date_labels = "%H",
                   limits = as.POSIXct(strptime(c("2022-12-23 01:00:00", "2022-12-24 01:00:00"),
                                                format = "%Y-%m-%d %H:%M:%S")),
                   expand = c(0, 0))

可行解决方案

推荐使用patchwork包拼接独立图表,既能保证温度图的波动清晰,又能对齐X轴,操作简单适合新手。

步骤1:安装并加载patchwork

install.packages("patchwork")
library(patchwork)

步骤2:修改温度图,保留Y轴并隐藏X轴

明确设置温度Y轴范围,同时隐藏X轴元素,避免重复:

plot_temperature <- ggplot(df, aes(x = datetime, y = mean_Temperature)) +
  geom_line(aes(y= mean_Temperature, color = "Temperature"), linewidth = 1) +
  geom_ribbon(aes(ymin= mean_Temperature - sd_Temperature,
                  ymax= mean_Temperature + sd_Temperature,
                  fill = "Temperature", color = "Temperature"), alpha = 0.2) +
  labs(y = "mean temperature (in °C) +/- standard deviation") +
  scale_color_manual(values = "red") +
  scale_fill_manual(values = "red") + # 补充fill刻度,消除警告
  scale_y_continuous(limits = c(27, 30)) + # 锁定温度Y轴范围,确保波动可见
  theme(
    axis.line.y.left = element_line(color = "red"),
    axis.text.y.left = element_text(color = "red"),
    axis.title.x = element_blank(), # 隐藏X轴标题
    axis.text.x = element_blank(), # 隐藏X轴刻度文本
    axis.ticks.x = element_blank(), # 隐藏X轴刻度线
    plot.margin = margin(10, 0, 10, 10) # 调整边距,与主图贴合
  ) +
  scale_x_datetime(
    limits = as.POSIXct(strptime(c("2022-12-23 01:00:00", "2022-12-24 01:00:00"),
                                 format = "%Y-%m-%d %H:%M:%S")),
    expand = c(0, 0)
  )

步骤3:修改湿度光照图,调整边距并移除温度元素

移除主图中的温度折线和误差带,调整边距与温度图对齐:

plot_humidity <- ggplot(df, aes(x = datetime, y = mean_humidity)) +
  geom_line(aes(y = mean_humidity, color = "Rel. humidity", group = 1), linewidth = 1) +
  geom_line(aes(y = mean_Light/100, color = "Light intensity", group = 2), linewidth = 1) +
  geom_ribbon(aes(ymin= mean_humidity - sd_humidity,
                  ymax= mean_humidity + sd_humidity,
                  fill = "Rel. humidity",  color = "Rel. humidity"),
              alpha=0.2, group = 1) +
  geom_ribbon(aes(ymin= (mean_Light/100) - (sd_Light/100),
                  ymax= (mean_Light/100) + (sd_Light/100),
                  fill = "Light intensity", color = "Light intensity"),
              alpha=0.2, group = 2) +
  labs(x = "time", y = "relative humidity (in %)", color = "Variable") +
  scale_color_manual(values = c("Light intensity" = "green", "Rel. humidity" = "blue")) +
  scale_fill_manual(values = c("Light intensity" = "green", "Rel. humidity" = "blue")) +
  scale_x_datetime(breaks = "1 hour", date_labels = "%H",
                   limits = as.POSIXct(strptime(c("2022-12-23 01:00:00", "2022-12-24 01:00:00"),
                                                format = "%Y-%m-%d %H:%M:%S")),
                   expand = c(0, 0)) +
  theme(
    axis.line.y.left = element_line(color = "blue"),
    axis.text.y.left = element_text(color = "blue"),
    plot.margin = margin(10, 10, 10, 0) # 调整左边距,消除间隙
  ) +
  guides(fill = "none") +
  scale_y_continuous(limits = c(0, 120), sec.axis = sec_axis(~ . * 100, name = "light intensity (in lux)")) +
  theme(
    axis.text.y.right = element_text(color = "green"),
    axis.line.y.right = element_line(color = "green")
  )

步骤4:拼接图表并输出

设置宽度比例,让温度图和主图对齐:

final_plot <- plot_temperature + plot_humidity + plot_layout(widths = c(1, 4))
print(final_plot)

原理说明

之前使用wrap_elements提取组件的方式会丢失温度图的绘图尺度信息,导致Y轴被压缩,波动无法展示。而直接拼接独立设置好尺度的图表,每个图的Y轴独立,同时通过相同的X轴范围保证对齐,完美解决尺度差异问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 05:14:51