ggplot2 - 为具有不同数据范围的两个图创建一个颜色条图例

ggplot2 - create one colorbar legend for two plots with different data-range

我有以下网格 dataset 从我创建两个不同的数据帧:

df_1=read.csv("example.csv")

df_2=df_1
df_2$var=df_2$var+50

然后我用 ggplot2 和 metR 绘制它们:

library(ggplot2)
library(metR)

plot_df_1=ggplot()+
  geom_contour_fill(data=df_1, aes(x=lon, y=lat, z=var), alpha=0.9) +
  scale_fill_steps(name = "", low = "#bdd7e7", high = "#08519c",
                   breaks = seq(0, 160, by = 10)) 

print(plot_df_1)

plot_df_2=ggplot()+
  geom_contour_fill(data=df_2, aes(x=lon, y=lat, z=var), alpha=0.9) +
  scale_fill_steps(name = "", low = "#bdd7e7", high = "#08519c",
                   breaks = seq(0, 160, by = 10)) 

print(plot_df_2)

有没有一种方法可以创建一个独特的颜色条图例,以便更好地比较两个图?

每个图的图例应具有相同的颜色和关联值。

在上面的两个图中,两个图中的最高(最低)值具有相同的颜色,但它们的大小不同。例如,是否可以为 plot_df_2 显示较深的颜色?

这是实现@Mamoun Benghezai 评论中描述的解决方案的一种方法:

library(tidyverse)
library(metR)

# Combine df1 and df2 to a single df ("df_table")
df_table <- map_df(.x = list("df_1" = df_1,
                             "df_2" = df_2),
                   .f = bind_rows,
                   .id = "src")

# Plot the combined dataset
plot_df_1 <- ggplot(df_table) +
  geom_contour_fill(aes(x=lon, y=lat, z=var), alpha=0.9) +
  scale_fill_steps(name = "", low = "#bdd7e7", high = "#08519c",
                   breaks = seq(0, 160, by = 10)) +
  facet_wrap(~src)

print(plot_df_1)