ggplot:Multi-panel/facet 使用一个常见的 y-axis 由多个变量分隔的散点图(而不是变量内的多个类别)

ggplot: Multi-panel/facet scatter plots separated by multiple variables (AND NOT by multiple categories within a variable) using one common y-axis

我的数据框 loopsubset_created 包含 45 个变量的 30 个观察值。 (您将在下面找到 str(loopsubset_created) 和一个 dput(loopsubset_created) 样本)。

现在我想创建 PdKeyT-变量 (y) 与 band-value-variables 中的五个(BLUEGREENRED, SWIR1, SWIR2) (x) 与

最后它基本上应该是这样的:
(我用 ggscatter 做了这个,但出于灵活性原因我更喜欢基本上使用 ggplot)

这是我的问题:
当尝试使用 ggplot 时,我没有找到上面显示的安排的正确方法,因为我无法找出正确的方法separating/grouping 的变量代码。我找到了数百个关于按变量中的多个分类值进行分面的教程,但不是按多个变量。

用下面的代码

ggplot(loopsubset_created, aes(y = PdKeyT)) + 
      geom_point(aes(x = BLUE, col = "BLUE")) + 
      geom_point(aes(x = GREEN, col = "GREEN")) +   
      geom_point(aes(x = RED, col = "RED")) +   
      geom_point(aes(x = SWIR1, col = "SWIR1")) +   
      geom_point(aes(x = SWIR2, col = "SWIR2"))

我得出了这个基本结果

这里是基本问题:
现在,我想按照上面描述的方式将 5 层分别排成一排 有人给我出主意吗?

加上问题的一些信息:
虽然以下方面不是我问题的直接部分,但我想描述一下我对情节的最终想法(以避免您的建议可能与进一步的要求发生冲突):

每个面板应包括

参考您的回答编辑 2021 年 10 月 31 日:

  1. 是否可以进一步使用你们各自的方法来单独限制 x-axes,如我问题的 'further requirements' 中所述(B-G-R 范围从 500 到 3000,SWIR 从 0到 1500) 使用 coord_cartesian(xlim = c(min,max))
    我之所以问,是因为我阅读了一些关于根据 'faceting approach' 限制轴的问题的讨论。但我想控制 x-axes,因为我会将许多这样的图堆叠在一起(我的样本只反映了 300 个采样点中的一个采样点的数据)。如果让它们对齐,我会很高兴。
  2. 同时,我更喜欢仅通过灰度颜色(对于所有波段都相同)来离散点和 reglines,而不是通过 theme(panel.background = element_rect(fill = "#xxxxxx") 离散地为面板着色。你认为这有问题吗?

最后是一些信息和我的数据样本

    > str(loopsubset_created)
'data.frame':   30 obs. of  45 variables:
 $ Site_ID                      : chr  "A" "A" "A" "A" ...
 $ Spot_Nr                      : chr  "1" "1" "1" "1" ...
 $ Transkt_Nr                   : chr  "2" "2" "2" "2" ...
 $ Point_Nr                     : chr  "4" "4" "4" "4" ...
 $ n                            : int  30 30 30 30 30 30 30 30 30 30 ...
 $ rank                         : int  3 3 3 3 3 3 3 3 3 3 ...
 $ Tile                         : chr  "1008" "1008" "1008" "1008" ...
 $ Date                         : int  20190208 20190213 20190215 20190218 20190223 20190228 20190302 20190305 20190315 20190320 ...
 $ id                           : chr  "22" "22" "22" "22" ...
 $ Point_ID                     : chr  "1022" "1022" "1022" "1022" ...
 $ Site_Nr                      : chr  "1" "1" "1" "1" ...
 $ Point_x                      : num  356251 356251 356251 356251 356251 ...
 $ Point_y                      : num  5132881 5132881 5132881 5132881 5132881 ...
 $ Classification               : num  7 7 7 7 7 7 7 7 7 7 ...
 $ Class_Derived                : chr  "WW" "WW" "WW" "WW" ...
 $ BLUE                         : num  1112 1095 944 1144 1141 ...
 $ GREEN                        : num  1158 1178 1009 1288 1265 ...
 $ RED                          : num  599 708 613 788 835 ...
 $ REDEDGE1                     : num  359 520 433 576 665 761 618 598 881 619 ...
 $ REDEDGE2                     : num  83 82 65 169 247 404 116 118 532 162 ...
 $ REDEDGE3                     : num  73 116 81 142 233 391 56 171 538 131 ...
 $ BROADNIR                     : num  44 93 60 123 262 349 74 113 560 125 ...
 $ NIR                          : num  37 70 66 135 215 313 110 135 504 78 ...
 $ SWIR1                        : num  187 282 184 225 356 251 240 216 507 197 ...
 $ SWIR2                        : num  142 187 155 197 281 209 192 146 341 143 ...
 $ Quality.assurance.information: num  26664 10272 10272 10272 8224 ...
 $ Q00_VAL                      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Q01_CS1                      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Q02_CSS                      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Q03_CSH                      : num  1 0 0 0 0 0 0 0 1 0 ...
 $ Q04_SNO                      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Q05_WAT                      : num  1 1 1 1 1 1 1 1 1 1 ...
 $ Q06_AR1                      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Q07_AR2                      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Q08_SBZ                      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Q09_SAT                      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Q10_ZEN                      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Q11_IL1                      : num  1 1 1 1 0 0 0 0 0 0 ...
 $ Q12_IL2                      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Q13_SLO                      : num  1 1 1 1 1 1 1 1 1 1 ...
 $ Q14_VAP                      : num  1 0 0 0 0 0 0 0 1 0 ...
 $ Q15_WDC                      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ PdMax                        : int  -7 -19 -20 -22 -24 -25 -26 -25 -21 -15 ...
 $ PdMin                        : int  -13 -23 -24 -26 -28 -29 -29 -28 -24 -20 ...
 $ PdKeyT                       : int  -10 -20 -22 -22 -27 -26 -26 -27 -22 -17 ...

loopsubset_created <- structure(list(Site_ID = c("A", "A", "A", "A", "A", "A", "A", 
    "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
    "A", "A", "A", "A", "A", "A", "A", "A", "A", "A"), Spot_Nr = c("1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1"), Transkt_Nr = c("2", "2", "2", "2", "2", "2", 
    "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
    "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2"), Point_Nr = c("4", 
    "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", 
    "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", 
    "4", "4", "4"), n = c(30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
    30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
    30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L), rank = c(3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), Tile = c("1008", 
    "1008", "1008", "1008", "1008", "1008", "1008", "1008", "1008", 
    "1008", "1008", "1008", "1008", "1008", "1008", "1008", "1008", 
    "1008", "1008", "1008", "1008", "1008", "1008", "1008", "1008", 
    "1008", "1008", "1008", "1008", "1008"), Date = c(20190208L, 
    20190213L, 20190215L, 20190218L, 20190223L, 20190228L, 20190302L, 
    20190305L, 20190315L, 20190320L, 20190322L, 20190325L, 20190330L, 
    20190401L, 20190416L, 20190419L, 20190421L, 20190501L, 20190506L, 
    20190524L, 20190531L, 20190603L, 20190620L, 20190625L, 20190630L, 
    20190705L, 20190710L, 20190809L, 20190814L, 20190903L), id = c("22", 
    "22", "22", "22", "22", "22", "22", "22", "22", "22", "22", "22", 
    "22", "22", "22", "22", "22", "22", "22", "22", "22", "22", "22", 
    "22", "22", "22", "22", "22", "22", "22"), Point_ID = c("1022", 
    "1022", "1022", "1022", "1022", "1022", "1022", "1022", "1022", 
    "1022", "1022", "1022", "1022", "1022", "1022", "1022", "1022", 
    "1022", "1022", "1022", "1022", "1022", "1022", "1022", "1022", 
    "1022", "1022", "1022", "1022", "1022"), Site_Nr = c("1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1"), Point_x = c(356250.781, 356250.781, 356250.781, 356250.781, 
    356250.781, 356250.781, 356250.781, 356250.781, 356250.781, 356250.781, 
    356250.781, 356250.781, 356250.781, 356250.781, 356250.781, 356250.781, 
    356250.781, 356250.781, 356250.781, 356250.781, 356250.781, 356250.781, 
    356250.781, 356250.781, 356250.781, 356250.781, 356250.781, 356250.781, 
    356250.781, 356250.781), Point_y = c(5132880.701, 5132880.701, 
    5132880.701, 5132880.701, 5132880.701, 5132880.701, 5132880.701, 
    5132880.701, 5132880.701, 5132880.701, 5132880.701, 5132880.701, 
    5132880.701, 5132880.701, 5132880.701, 5132880.701, 5132880.701, 
    5132880.701, 5132880.701, 5132880.701, 5132880.701, 5132880.701, 
    5132880.701, 5132880.701, 5132880.701, 5132880.701, 5132880.701, 
    5132880.701, 5132880.701, 5132880.701), Classification = c(7, 
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
    7, 7, 7, 7, 7, 7, 7, 7), Class_Derived = c("WW", "WW", "WW", 
    "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", 
    "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", 
    "WW", "WW", "WW", "WW", "WW"), BLUE = c(1112, 1095, 944, 1144, 
    1141, 1010, 968, 1023, 1281, 1124, 1215, 1154, 1188, 1177, 1622, 
    1305, 1215, 2282, 2322, 2337, 2680, 2473, 1143, 1187, 1165, 1040, 
    1290, 1112, 1474, 1131), GREEN = c(1158, 1178, 1009, 1288, 1265, 
    1208, 1122, 1146, 1416, 1298, 1379, 1345, 1379, 1366, 1714, 1446, 
    1354, 2417, 2417, 2500, 2967, 2587, 1469, 1522, 1544, 1253, 1514, 
    1371, 1875, 1416), RED = c(599, 708, 613, 788, 835, 852, 726, 
    729, 1044, 816, 905, 908, 948, 970, 1206, 944, 935, 1648, 1741, 
    2004, 2109, 2032, 1241, 1290, 1419, 1206, 1424, 1339, 1969, 1321
    ), REDEDGE1 = c(359, 520, 433, 576, 665, 761, 618, 598, 881, 
    619, 722, 771, 829, 823, 937, 725, 759, 1327, 1395, 1756, 1718, 
    1753, 1533, 1528, 1683, 1335, 1605, 1499, 2016, 1592), REDEDGE2 = c(83, 
    82, 65, 169, 247, 404, 116, 118, 532, 162, 183, 218, 285, 200, 
    514, 182, 230, 568, 531, 1170, 780, 1101, 1192, 1174, 1250, 949, 
    1121, 1127, 1382, 1159), REDEDGE3 = c(73, 116, 81, 142, 233, 
    391, 56, 171, 538, 131, 205, 137, 321, 253, 503, 193, 214, 564, 
    527, 1192, 698, 1177, 1203, 1259, 1341, 1049, 1146, 1216, 1416, 
    1188), BROADNIR = c(44, 93, 60, 123, 262, 349, 74, 113, 560, 
    125, 121, 211, 325, 221, 480, 184, 178, 461, 435, 1067, 570, 
    1023, 961, 966, 964, 844, 764, 993, 1197, 834), NIR = c(37, 70, 
    66, 135, 215, 313, 110, 135, 504, 78, 115, 216, 197, 163, 462, 
    113, 165, 392, 349, 1006, 574, 1092, 1153, 1143, 1128, 961, 1033, 
    1027, 1164, 1086), SWIR1 = c(187, 282, 184, 225, 356, 251, 240, 
    216, 507, 197, 306, 260, 298, 290, 400, 190, 300, 275, 204, 678, 
    528, 1087, 1091, 1049, 1310, 935, 1199, 1169, 984, 1139), SWIR2 = c(142, 
    187, 155, 197, 281, 209, 192, 146, 341, 143, 271, 220, 246, 232, 
    387, 168, 217, 193, 173, 540, 374, 764, 766, 799, 869, 724, 827, 
    794, 745, 848), Quality.assurance.information = c(26664, 10272, 
    10272, 10272, 8224, 8224, 8224, 8224, 24616, 8224, 8224, 8224, 
    32, 8224, 8288, 24616, 8224, 8240, 48, 8208, 8240, 8192, 8192, 
    24648, 8192, 8192, 8192, 8192, 0, 8224), Q00_VAL = c(0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0), Q01_CS1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
        Q02_CSS = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q03_CSH = c(1, 
        0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
        0, 0, 0, 1, 0, 0, 0, 0, 0, 0), Q04_SNO = c(0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 
        0, 0, 0, 0, 0, 0), Q05_WAT = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
        0, 1), Q06_AR1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0), Q07_AR2 = c(0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q08_SBZ = c(0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0), Q09_SAT = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0), Q10_ZEN = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q11_IL1 = c(1, 
        1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q12_IL2 = c(0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0), Q13_SLO = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        0, 1), Q14_VAP = c(1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
        0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0), Q15_WDC = c(0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0), PdMax = c(-7L, -19L, -20L, 
        -22L, -24L, -25L, -26L, -25L, -21L, -15L, -19L, -17L, -23L, 
        -22L, -4L, -7L, -8L, 55L, 57L, 47L, 67L, 44L, 21L, 18L, 13L, 
        16L, 16L, 9L, 12L, 11L), PdMin = c(-13L, -23L, -24L, -26L, 
        -28L, -29L, -29L, -28L, -24L, -20L, -22L, -22L, -26L, -26L, 
        -7L, -11L, -11L, 46L, 47L, 36L, 52L, 37L, 17L, 14L, 9L, 11L, 
        9L, 5L, 5L, 2L), PdKeyT = c(-10L, -20L, -22L, -22L, -27L, 
        -26L, -26L, -27L, -22L, -17L, -19L, -19L, -23L, -23L, -5L, 
        -9L, -9L, 54L, 53L, 40L, 60L, 43L, 20L, 15L, 13L, 15L, 13L, 
        7L, 9L, 6L)), row.names = 198:227, class = "data.frame")

我认为这可以满足您的大部分要求,但相关性注释除外。如果,正如您在问题中提到的那样,您希望每个面板有 3 个回归(PdkeyT 的三个范围中的每一个),您还需要每个面板有 3 个相关系数和 p 值,这将是混乱的。

您之所以没有看到每个变量具有不同方面的教程,是因为这不是 的方面。分面是一种显示具有相同 x 轴和 y 轴但因某些其他分类变量不同而不同的数据的方法。它们并非旨在作为针对同一 y 变量绘制不同 x 变量的方法。您所描述的是并排的 5 个不同的图,而不是分面。

话虽如此,仍然可以通过创造性地使用构面来创建您正在寻找的情节。您首先需要将数据整形为长格式,以便不同 x 轴列的值堆叠到一个名为 value 的列中,并创建一个名为 name 的新列来标记每个值它最初来自哪个栏目。

然后我们可以使用新的 value 列作为我们的 x 轴变量,并根据 name 列进行分面。

为了让这个看起来更真实,我们做了一些 theme 调整以确保小平面条类似于轴标签:

library(dplyr)
library(tidyr)
library(ggplot2)

loopsubset_created %>% 
  select(PdKeyT, BLUE, GREEN, RED, SWIR1, SWIR2) %>%
  pivot_longer(-1) %>%
  mutate(range = cut(PdKeyT, c(-Inf, -10, 30, Inf), c("Low", "Mid", "High"))) %>%
  ggplot(aes(value, PdKeyT, color = name)) +
  geom_point(aes(alpha = range)) +
  geom_smooth(aes(group = range), size = 0.1,
              method = "lm", formula = y ~ x, color = "black") +
  labs(x = "") +
  facet_grid(.~name, switch = "x", scales = "free_x") +
  scale_color_manual(values = c("blue", "green", "red", "magenta", "violet")) +
  theme_bw() +
  theme(strip.placement = "outside",
        strip.background = element_blank(),
        plot.margin = margin(120, 10, 120, 10),
        legend.position = "none")

要绘制面板图,请使用 facet_wrapfacet_grid。此外,通常 ggplot2 在数据为 long 格式时效果更好。这允许您将变量分配给审美,而不是像您那样手动执行。

library(ggplot2)
library(tidyr)
library(purrr)
library(dplyr)
library(tibble)

# lengthen your data so variable names are in a column
df <- loopsubset_created %>% 
  pivot_longer(cols = c(BLUE:RED, starts_with("SWIR")))

# get correlation coef and pvalue
r <- map(split(df, ~ name), ~ with(.x, c(cor(PdKeyT, value, method = "spearman"), 
                                         cor.test(PdKeyT, value, method = "spearman")$p.value))) %>%
  bind_rows() %>% 
  rownames_to_column("i") %>% # first row is coef, second row is p value
  pivot_longer(-i) %>% 
  mutate(lab = ifelse(i == 1, 
                      # formatted so will be parsed by geom_text
                      sprintf("italic(R) == %0.5f", value),
                      sprintf("italic(p) == %0.5f", value)),
         x = -Inf, # left of panel
         y = Inf, # top of panel,
         vjust = ifelse(i == 1, 0.75, 2)) # put p-value below

df %>% 
  ggplot(aes(x = value, y = PdKeyT, color = name)) + 
  geom_point() + 
  geom_text(data = r, 
            aes(x = x, y = y, 
                label = lab,
                vjust = vjust),
            size = 3,
            parse = T,
            inherit.aes = F) + 
  geom_smooth(method = "lm", 
              se = T, 
              formula = y ~ x,
              show.legend = F) + 
  facet_grid(~ name,
             scales = "free_x") +
  labs(color = element_blank(),
       x = "XLAB")

更新: 为了完成你的最后一个任务,我可以使用来自 Allan Cameron 的代码:添加另一列来设置削减 mutate(range = cut(PdKeyT, c(-Inf, -10, 30, Inf), c("Low", "Mid", "High"))) %>%(此代码由 Allan Cameron 提供)

library(tidyverse)
library(ggpubr)

df_long_list <- loopsubset_created %>%
  select(PdKeyT, BLUE, GREEN, RED, SWIR1, SWIR2) %>% 
  pivot_longer(
    cols = -PdKeyT
  ) %>% 
  mutate(color = case_when(name=="BLUE" ~ "blue",
                           name=="GREEN" ~ "green",
                           name=="RED" ~ "red",
                           name=="SWIR1" ~ "magenta",
                           name=="SWIR2" ~ "violet"))%>% 
  mutate(range = cut(PdKeyT, c(-Inf, -10, 30, Inf), c("Low", "Mid", "High"))) %>%
  group_split(name)
  
  p <- ggplot()
  for (i in 1:5) p <- p + geom_point(data=df_long_list[[i]], aes(value, PdKeyT, color=color, alpha=range))+
    geom_smooth(data=df_long_list[[i]], aes(value, PdKeyT, group=range), method = lm, se=TRUE)+
    theme(legend.position="none") +
    stat_cor(data=df_long_list[[i]], aes(value, PdKeyT, 
                                         label=paste("Spearman",..r.label.., ..p.label.., sep = "~`,`~")), method="spearman",
             # label.x.npc="left", label.y.npc="top", hjust=0) +
             label.x = 3, label.y = 70)+
    stat_cor(data=df_long_list[[i]], aes(value, PdKeyT,
                                         label=paste("Pearson",..r.label.., ..p.label.., sep = "~`,`~")), method="pearson",
             # label.x.npc="left", label.y.npc="top", hjust=0) +
             label.x = 3, label.y = 65)+
    facet_grid(.~name, scales = "free") +
    theme_bw()+
    theme(panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          plot.margin = margin(120, 10, 120, 10),
          panel.border = element_rect(fill = NA, color = "black"))
  p

以下是您的操作方法:

  1. select 所有相关列
  2. 引入长格式
  3. 向数据框添加颜色列
  4. 使用 group_split
  5. 制作数据帧列表
  6. 使用 for 循环遍历列表中的 5 个数据帧
  7. 在循环中为 ggpubr 包中的 pearson 和 spearman 添加 stat_cor
  8. 切面并进行一些格式化
library(tidyverse)
library(ggpubr)

df_long_list <- loopsubset_created %>%
  select(PdKeyT, BLUE, GREEN, RED, SWIR1, SWIR2) %>% 
  pivot_longer(
    cols = -PdKeyT
  ) %>% 
  mutate(color = case_when(name=="BLUE" ~ "blue",
                           name=="GREEN" ~ "green",
                           name=="RED" ~ "red",
                           name=="SWIR1" ~ "magenta",
                           name=="SWIR2" ~ "violet"))%>% 
  group_split(name)
  
  p <- ggplot()
  for (i in 1:5) p <- p + geom_point(data=df_long_list[[i]], aes(value, PdKeyT, color=color))+
    geom_smooth(data=df_long_list[[i]], aes(value, PdKeyT), method = lm, se=TRUE)+
    theme(legend.position="none") +
    stat_cor(data=df_long_list[[i]], aes(value, PdKeyT, 
                                         label=paste("Spearman",..r.label.., ..p.label.., sep = "~`,`~")), method="spearman",
             # label.x.npc="left", label.y.npc="top", hjust=0) +
             label.x = 3, label.y = 70)+
    stat_cor(data=df_long_list[[i]], aes(value, PdKeyT,
                                         label=paste("Pearson",..r.label.., ..p.label.., sep = "~`,`~")), method="pearson",
             # label.x.npc="left", label.y.npc="top", hjust=0) +
             label.x = 3, label.y = 65)+
    facet_grid(.~name, scales = "free_y") +
    theme_bw()+
    theme(panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          plot.margin = margin(120, 10, 120, 10),
          panel.border = element_rect(fill = NA, color = "black"))
  p