如何将折线图 (ylim = 27. 30) 插入另一个具有 2 个折线图 (ylim=0,120) 的图表而不失真?

问题描述 投票:0回答:1

我需要将不同特征的数据(温度、光强度、相对湿度)可视化以形成一种气候图。我认为,我的问题在于不同的 ylims,这阻碍了将图表组合成一个图表的简单转换。如果我像下面的代码一样输入温度图,由于绘图湿度图的比例不同,温度的波动变得不可见。

未经转换插入的温度图不足以显示随时间的变化:

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))

我希望它看起来像下面这样并与其他图表重叠,而不是上图中的红色图表。

single temperature graph

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))

我最初的计划是使用ggplot和wrap_elements将图表的不同部分(y轴、ylab、geom_line、geom_ribbon)融合成一个图表。这对于光强度图来说不是问题,但是对于温度图来说是个问题。 我不能只转换数据,或者可以吗?我还没有找到一种方法从单个温度图中提取 geom_line 和 geom_ribbon 并将其放置在plot_humidity上,但如果没有更优雅的解决方案,我也可以接受。

有什么明显我遗漏的东西吗?由于我仍在学习 R,并且包含此图的工作是我的第一次重大努力,因此我感谢您能给我的任何提示!

编辑: 这是我的df

r ggplot2 transformation linegraph
1个回答
0
投票

使用一些模拟数据,如果您尝试转换图 1 中的值并反向转换图 2 中的比例(具有相同的限制),那么这应该可行:

library(ggplot2)

df <- tibble::tibble(
  t = 1:10,
  temp = rnorm(10, 20, 1),
  humidity = c(0, 0, 0, rnorm(7, 50, 10)),
  lum = rnorm(10, 6000, 1000)
)


g1 <- df |>
  ggplot(aes(t)) +
  geom_line(aes(y = humidity, colour = "Rel. humidity")) +
  geom_line(aes(y = lum / 100, colour = "Light intensity")) +
  geom_line(aes(y = (temp * 10) - 180, colour = "Temperature")) +
  scale_y_continuous(
    sec.axis = sec_axis(~ . * 100, name = "light intensity (in lux)"),
    limits = c(0, max(df$humidity, df$lum / 100))
  ) +
  scale_color_manual(values = c(
    "Light intensity" = "green",
    "Temperature" = "red",
    "Rel. humidity" = "blue"
  )) +
  theme(
    axis.line.y.left =  element_line(color = "blue"),
    axis.text.y.left = element_text(color = "blue"),
    plot.margin = margin(10, 10, 10, 30),
    axis.text.y.right = element_text(color = "green"),
    axis.line.y.right = element_line(color = "green")
  )

g2 <- df |>
  ggplot(aes(t, temp)) +
  geom_line(aes(y = (temp * 10) - 180, colour = "Temperature")) +
  theme(
    axis.line.y.left =  element_line(color = "red"),
    axis.text.y.left = element_text(color = "red")
  ) +
  scale_y_continuous(
    sec.axis = sec_axis(transform =  ~ (. + 180) / 10, name = "Temperature"),
    position = "right",
    limits = c(0, max(df$humidity, df$lum / 100))
  )

library(patchwork)
library(cowplot)

wrap_elements(get_plot_component(g2, "ylab-l")) +
  wrap_elements(get_y_axis(g2, position = "left")) +
  g1 +
  plot_layout(widths = c(3, 1, 40))

© www.soinside.com 2019 - 2024. All rights reserved.