GGPLOT2 来自包含向量和单个数值的 R 列表的线图

GGPLOT2 Line plots from an R list containing vectors and single numeric values

我有一个包含 2500 个列表的 R 列表。 2500 个列表中的每一个都包含 1 个向量和 2 个值。为了可重复性,我将数据的一个小版本子集化,因此它看起来像这样:

head(models, 1)
>$model_1
>$model_1$m
> [1] 0.01335775 0.01336487 0.01336805 0.01338025 0.01340532 0.01343117 0.01346120 0.01349530 0.01353788 > 0.01357654 0.01360668

>$model_1$Cab
>[1] 59.6774

>$model_1$LAI
>[1] 4.01739

>$model_2
>$model_2$m
> [1] 0.02367338 0.02360433 0.02352800 0.02346125 0.02339469 0.02333403 0.02325861 0.02317945 0.02310961 >0.02303802 0.02295710

>$model_2$Cab
>[1] 59.6774

>$model_2$LAI
>[1] 0.5523946

现在,我想绘制一个线图(使用 ggplot2),其 x 轴是 400 到 410 之间的值,y 轴是每个列表中的向量 (models$model_1$m, models$model_2$m等等。)因此,剧情中会有很多台词。我还想用各自的 models$model_2$Cab 值给每一行着色(连续着色),并有一个连续的图例显示每个 models$model_2$Cab 值及其颜色。

为了可重复性(请注意这是原始数据的极大简化版本):

> dput(head(models, 10))
list(model_1 = list(m = c(0.0133577497667816, 0.0133648693063468, 
0.0133680481888036, 0.01338024983382, 0.0134053218864944, 0.0134311717034271, 
0.0134612003419723, 0.0134953017438241, 0.0135378825635721, 0.0135765418166368, 
0.0136066826886183), Cab = 59.6773970406502, LAI = 4.01739045299768), 
    model_2 = list(m = c(0.023673375903171, 0.0236043348551818, 
    0.0235280045196734, 0.0234612496831449, 0.0233946873132861, 
    0.0233340349230324, 0.0232586128971129, 0.0231794538902946, 
    0.0231096074536893, 0.023038021285693, 0.0229570982021948
    ), Cab = 59.6773970406502, LAI = 0.552394618641403), model_3 = list(
        m = c(0.0138277418755234, 0.0138310132688916, 0.0138301891768216, 
        0.0138383905159343, 0.0138587906203227, 0.0138802253169266, 
        0.0139048786261519, 0.0139332011615252, 0.0139700189737812, 
        0.0140030367215791, 0.0140275202380309), Cab = 59.6773970406502, 
        LAI = 3.01987725977579), model_4 = list(m = c(0.017483089696901, 
    0.0174591709902523, 0.017429967081058, 0.0174099884420304, 
    0.0173976896061841, 0.0173882607103241, 0.0173752969257632, 
    0.0173632160871019, 0.0173599236031355, 0.0173536114293099, 
    0.0173384748063733), Cab = 59.6773970406502, LAI = 1.37503600459533), 
    model_5 = list(m = c(0.0182499047037402, 0.0182203724940146, 
    0.0181853063358603, 0.0181595102703982, 0.0181404648083386, 
    0.0181246681180869, 0.0181039409709977, 0.01808352264341, 
    0.0180719579429791, 0.018057532687598, 0.0180342187796566
    ), Cab = 59.6773970406502, LAI = 1.22529135635182), model_6 = list(
        m = c(0.0158200567917405, 0.0158083674745268, 0.0157919331298277, 
        0.0157846269346119, 0.0157870246965916, 0.0157914665730281, 
        0.0157954117645301, 0.0158014906653224, 0.0158162176575737, 
        0.0158275775312257, 0.0158302513933357), Cab = 59.6773970406502, 
        LAI = 1.81571552453658), model_7 = list(m = c(0.0133628950691214, 
    0.0133699680411211, 0.0133730986417069, 0.0133852517083498, 
    0.0134102666346747, 0.0134360623898904, 0.0134660252680654, 
    0.0135000559061319, 0.0135425658393117, 0.013581155812944, 
    0.013611227528355), Cab = 59.6773970406502, LAI = 3.99643688124574), 
    model_8 = list(m = c(0.0183501671255408, 0.0183199017377111, 
    0.0182840698901064, 0.0182575139774255, 0.0182375872739662, 
    0.0182209588085648, 0.0181992175650369, 0.0181777101462036, 
    0.0181650648958527, 0.0181495798700031, 0.0181251977995322
    ), Cab = 59.6773970406502, LAI = 1.20735517669905), model_9 = list(
        m = c(0.0143687162679524, 0.0143678440890305, 0.0143626995592654, 
        0.0143666036037224, 0.0143820089259476, 0.0143987279254991, 
        0.0144176359711743, 0.0144397860850458, 0.0144704682720878, 
        0.0144974726755733, 0.0145159061770205), Cab = 59.6773970406502, 
        LAI = 2.51320168699674), model_10 = list(m = c(0.0138736072820698, 
    0.0138765215672426, 0.0138753253418108, 0.0138831561248062, 
    0.0139031250366076, 0.0139241525443688, 0.0139483098566198, 
    0.0139760994306543, 0.0140123870383231, 0.0140448852992375, 
    0.0140688465774421), Cab = 59.6773970406502, LAI = 2.96397596054064))

我想要实现的是这样的(但是有一个更好看的ggplot2):

可以这样实现:

  1. 将您的列表列表转换为数据框列表。
  2. 将带有 x 轴变量的变量添加到每个 df
  3. 按行绑定数据框列表
  4. 情节,我利用scale_colour_gradientn(colors = rainbow(20))模仿你的彩虹色阶。
library(dplyr)
library(ggplot2)

models <- lapply(models, as.data.frame) %>% 
  lapply(function(x) { x$x <- 400:410; x}) %>% 
  bind_rows(.id = "id")

ggplot(models, aes(x = x, y = m, color = LAI, group = id)) +
  geom_line() +
  scale_x_continuous(breaks = scales::pretty_breaks()) +
  scale_colour_gradientn(colors = rainbow(20))