将不同数据帧的多个图放入同一个图中

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

ggplot 用户!我有三个数据框,它们是 T_N_6190、T_N_9120、T_N_2123

T_N_6190:

structure(list(Hydrojahr = c(1961, 1962, 1963, 1964, 1965, 1966,
    1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 
    1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 
    1989, 1990), T_m = c(9.75963470319635, 8.18401826484018, 7.7496803652968, 
    8.69225865209472, 8.31525114155251, 8.71534246575342, 9.7797514619883, 
    9.14398907103825, 8.04849315068493, 7.43212328767123, 8.92219178082192, 
    8.7323087431694, 8.82506849315068, 8.85869863013699, 9.93952054794521, 
    8.61632513661202, 8.57938356164384, 8.77034246575342, 7.97739726027397, 
    7.94153005464481, 8.97431506849315, 8.87732876712329, 10.1515753424658, 
    8.39637978142077, 8.23602739726027, 8.06349315068493, 7.46054794520548, 
    9.38825136612022, 9.97842465753425, 9.99150684931507), N_s = c(874.4, 
    619.1, 435.1, 506.2, 597.5, 711.6, 705.8, 577.8, 651.8, 583.1, 
    545.8, 553.3, 420.1, 562.7, 624.7, 385.7, 619.5, 603.3, 584.7, 
    661.6, 694.5, 506, 586.6, 564.7, 513.4, 644.8, 714.2, 568.5, 
    415.6, 673.4)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
    -30L))

T_N_9120:

structure(list(Hydrojahr = c(1991, 1992, 1993, 1994, 1995, 1996, 
1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 
2019, 2020), T_m = c(8.95930918281382, 10.3787795992714, 8.99625570776256, 
9.60173515981735, 10.0106849315068, 7.54653916211293, 9.4441095890411, 
9.93287671232877, 10.0211872146119, 10.2435336976321, 10.1421917808219, 
10.8183561643836, 9.99534246575342, 9.76693989071038, 9.99506849315068, 
10.0458447488584, 11.9687671232877, 10.6469034608379, 10.3304109589041, 
9.38356164383562, 9.81753424657534, 10.4704007285975, 9.48785388127854, 
11.4050228310502, 11.1631050228311, 11.5071038251366, 10.1428310502283, 
11.8284931506849, 11.8727853881279, 11.7700364298725), N_s = c(530.9, 
515.4, 662.4, 784.5, 624.3, 418.7, 462.8, 534.6, 501, 552.6, 
562.3, 735.4, 469.6, 619.3, 635, 534.1, 790, 503.9, 525.7, 685.9, 
673.8, 588, 632.4, 610.6, 515, 516.5, 722.5, 451.9, 485.5, 514.6
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-30L))   

T_N_2123:

structure(list(Hydrojahr = c(2021, 2022, 2023), T_m = c(10.6240182648402, 
11.513698630137, 12.0459437751004), N_s = c(586.7, 437.7, 585.2
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-3L))

我想在 r 中的同一个图上绘制三个数据帧,这是我的代码:

p <- ggplot()+
geom_point(aes(x = T_m, y = N_s), T_N_6190, color = "gray") +
geom_point(aes(x = T_m, y = N_s), T_N_9120, color = "gray35") +
geom_point(aes(x = T_m, y = N_s), T_N_2123 %>% filter(Hydrojahr == 2021), color = "firebrick4", size = 10.6)+
geom_point(aes(x = T_m, y = N_s), T_N_2123 %>% filter(Hydrojahr == 2022), color = "firebrick1", size = 11.5)+
geom_point(aes(x = T_m, y = N_s), T_N_2123 %>% filter(Hydrojahr == 2023), color = "darkorange", size = 12)+
geom_vline(aes(xintercept = mean(T_N_6190$T_m)), linewidth = 0.5, color = "gray", linetype ="dashed")+
geom_hline(aes(yintercept = mean(T_N_6190$N_s)), linewidth = 0.5, color = "gray", linetype ="dashed")+
geom_vline(aes(xintercept = mean(T_N_9120$T_m)), linewidth = 0.5, color = "gray35", linetype ="dashed")+
geom_hline(aes(yintercept = mean(T_N_9120$N_s)), linewidth = 0.5, color = "gray35", linetype ="dashed")+
geom_text(data = T_N_2123, aes(x = T_m, y = N_s, label = Hydrojahr),hjust =-0.5, vjust =-0.5)+
labs(title = "Thermopluviogramm der Stadt xxx", subtitle = "Hydrologisches Jahr",
x = "Mittlere Temperatur [°C]", y = "Niederschlagssumme [mm]",
caption = "Daten: xxx, Erstellt: xxx") +
theme_linedraw()

情节如下: 但不知怎的,没有图例,而且 2021 年、2022 年、2023 年的点大小仅在我输入数字时才起作用,而不是

size = T_m
size = T_N_2123$Tm

如果我在

color
内添加
aes()
,则点的颜色将不是我定义的颜色,绘图显示如下:

r ggplot2
1个回答
0
投票

在这种方法中,我首先将两个数据帧中的数据

bind
合并为一个,并在绘图之前计算
means

library(dplyr)
library(ggplot2)
library(ggnewscale)

df <- bind_rows(T_N_6190 = T_N_6190,
                T_N_9120 = T_N_9120,
                .id = 'source')

means <- df |>
  summarise(T_m = mean(T_m),
            N_s = mean(N_s),
            .by = source)

glimpse(means)
#> Rows: 2
#> Columns: 3
#> $ source <chr> "T_N_6190", "T_N_9120"
#> $ T_m    <dbl> 8.750039, 10.256452
#> $ N_s    <dbl> 590.1833, 578.6400

我选择将

T_N_2123
数据框保留为单独的对象,并在
ggnewscale::new_scale_color()
的帮助下添加单独的色标。

df |>
  ggplot(aes(x = T_m, y = N_s)) +
  geom_point(aes(color = source)) +
  geom_vline(data = means, aes(xintercept = T_m, color = source),
             linewidth = 0.5, linetype = 'dashed') +
  geom_hline(data = means, aes(yintercept = N_s, color = source),
             linewidth = 0.5, linetype = 'dashed') +
  scale_color_manual(values = c('gray', 'gray35')) +
  labs(color = '') +
  new_scale_color() +
  geom_point(data = T_N_2123, aes(color = factor(Hydrojahr), size = factor(Hydrojahr))) +
  scale_color_manual(values = c('firebrick4', 'firebrick1', 'darkorange')) +
  scale_size_manual(values = c(10.6, 11.5, 12)) +
  geom_text(data = T_N_2123, aes(label = Hydrojahr), hjust =-0.5, vjust =-0.5) +
  theme_linedraw() +
  labs(title = "Thermopluviogramm der Stadt xxx", subtitle = "Hydrologisches Jahr",
       x = "Mittlere Temperatur [°C]", y = "Niederschlagssumme [mm]",
       caption = "Daten: xxx, Erstellt: xxx", 
       color = 'Jahr', size = 'Jahr')

创建于 2024-04-18,使用 reprex v2.1.0

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