使用 `transition_reveal` 时,将保留不需要的层的 `gganimate` 过去数据

`gganimate` past data of unwanted layers are kept when using `transition_reveal`

我正在使用 gganimate,我需要在绘图区域中使用 4 个元素:

  1. 一个geom_line()
  2. 一个geom_point()
  3. A geom_abline() 静态 slopecolor,变化 intercept
  4. 另一个 geom_abline() slopecolorintercept

前 3 个元素按照我的意愿进行了动画处理:

其中只有geom_line()逐渐显露出来,其中geom_line()和第一个geom_abline()只显示该帧的数据

但是当我这样添加第四层时(需要手动调色),过去的geom_abline()并没有消失:

我尝试添加参数 transition_reveal(keep_last = F),并尝试使用多个 shadow_mark() 为不同的层定义不同的规则,但到目前为止没有任何效果。

要确保第四层只显示当前帧吗?


当前代码:

temp_ani <- 
  ggplot(tab_temp, aes(tot, new_loess)) + 
  geom_line(size = 1.25) +
  geom_point(size = 2) + 
  geom_abline(aes(slope = ref_log10m, intercept = ref_log10c), size = 1.1, linetype = "dashed", alpha = 0.3) +
  geom_abline(aes(slope = trend_log10m, intercept = trend_log10c, color = ID_factor), size = 1.1, linetype = "dashed", alpha = 0.7,
              show.legend = F) +
  scale_color_manual(values = tab_temp[, trend_color]) +
  transition_reveal(t) +
  scale_x_continuous(name = "tot", trans = "log10", limits = c(24, 786.5)) + 
  scale_y_continuous(name = "new_loess", trans = "log10", limits = c(21, 601.7)) +
  theme_classic() +
  theme(axis.title = element_text(size = 18),
    axis.text = element_text(size = 16))
animate(temp_ani)

示例数据(由dput生成):

tab_temp <- 
  structure(list(tot = c(24L, 26L, 26L, 36L, 42L, 49L, 50L, 53L, 
                         56L, 56L, 57L, 60L, 62L, 65L, 69L, 69L, 69L, 75L, 81L, 85L, 91L, 
                         93L, 94L, 95L, 100L, 101L, 101L, 105L, 105L, 108L, 110L, 115L, 
                         116L, 121L, 130L, 132L, 138L, 142L, 149L, 158L, 168L, 193L, 209L, 
                         257L, 274L, 318L, 357L, 387L, 411L, 454L, 519L, 583L, 642L, 683L, 
                         715L),
                 new = c(22L, 21L, 21L, 28L, 34L, 41L, 40L, 41L, 43L, 42L, 
                         42L, 44L, 45L, 44L, 45L, 43L, 43L, 39L, 39L, 36L, 41L, 40L, 38L, 
                         39L, 43L, 41L, 39L, 40L, 36L, 39L, 41L, 40L, 35L, 36L, 39L, 39L, 
                         44L, 47L, 49L, 57L, 67L, 88L, 104L, 149L, 164L, 203L, 241L, 266L, 
                         281L, 322L, 381L, 441L, 493L, 525L, 547L),
                 t = 1:55,
                 new_loess = c(20.4718824559981, 
                               21.7890308359201, 21.7890308359201, 29.230438378301, 34.1819985150492, 
                               39.5079831823911, 40.0755079303131, 41.4099675429299, 42.370529571607, 
                               42.370529571607, 42.8201044925118, 43.8252925663542, 44.3125327660056, 
                               44.5559375847947, 43.3892881478697, 43.3892881478697, 43.3892881478697, 
                               40.6214119319565, 38.4905820730386, 37.9720281923537, 39.2781675720742, 
                               39.6852276451529, 39.9358369585547, 40.1843988106463, 39.903890056883, 
                               39.8282043659178, 39.8282043659178, 39.5872985292962, 39.5872985292962, 
                               39.1703139824658, 38.8033157618615, 37.6950388015054, 37.5718391291946, 
                               37.3871340774544, 38.9365752067175, 39.8424253760653, 42.8908232704117, 
                               45.2358046101902, 49.8927271106793, 56.9673605996446, 65.3666082472634, 
                               88.2333199821777, 103.004387148103, 148.220525358668, 163.942873635871, 
                               204.371579248934, 238.230045236249, 264.200021490433, 285.078351674107, 
                               323.515728645106, 381.682707746736, 438.213805930832, 489.137706154487, 
                               523.921712190664, 550.558894652666),
                 ID_factor = structure(1:55, .Label = c("1", 
                                                        "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
                                                        "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
                                                        "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", 
                                                        "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", 
                                                        "47", "48", "49", "50", "51", "52", "53", "54", "55"), class = "factor"), 
                 ref_log10m = c(NA, 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, 1, 1, 
                                1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                                1, 1, 1),
                 ref_log10c = c(NA, -0.0767354344741067, -0.0767354344741067, 
                                -0.0904671721067551, -0.0894518394244057, -0.09351121980074, 
                                -0.096090968882282, -0.107170979497638, -0.12112413490539, 
                                -0.12112413490539, -0.124227132918044, -0.136426426370142, 
                                -0.145865115840727, -0.16400776974052, -0.201466565659624, 
                                -0.201466565659624, -0.201466565659624, -0.266306248714822, 
                                -0.323130540124996, -0.349955131070593, -0.364890173692428, 
                                -0.369854072675033, -0.371765062973866, -0.373666129954109, 
                                -0.398984764767179, -0.404130646875006, -0.404130646875006, 
                                -0.423633432928999, -0.423633432928999, -0.440466702695637, 
                                -0.452523847307499, -0.484413645662749, -0.489595535070859, 
                                -0.510063194866713, -0.523585603228512, -0.520228163317121, 
                                -0.507514703991367, -0.496806024856726, -0.475149025512369, 
                                -0.443030988676679, -0.409953330914954, -0.339924688141996, 
                                -0.307290563603587, -0.239024775067351, -0.223058019569702, 
                                -0.192006619096867, -0.17567168290562, -0.165778116414225, 
                                -0.15887758283895, -0.147160452867149, -0.133464873299646, 
                                -0.123982498866305, -0.118103885501835, -0.115154306972075, 
                                -0.113502258448684),
                 trend_log10m = c(NA, 0.779013043966055, 
                                  0, 0.902838605051119, 1.01516626395308, 0.939364077071771, 
                                  0.705975457374834, 0.562156421073622, 0.41648278860048, 0, 
                                  0.596322729780414, 0.452366794075921, 0.337191122084057, 
                                  0.115926899508026, -0.444292873406387, 0, 0, -0.790549373381475, 
                                  -0.700116895372846, -0.281394397911645, 0.495820425082167, 
                                  0.474249915989644, 0.588583559834837, 0.586342537885251, 
                                  -0.136567884586587, -0.190797733928004, 0, -0.156205385180462, 
                                  0, -0.375890288287152, -0.513019327466591, -0.651879927019231, 
                                  -0.378107511884984, -0.116780389727616, 0.566005001030829, 
                                  1.50635698066896, 1.6585525574429, 1.86295886106405, 2.03632300098696, 
                                  2.26097387810847, 2.24108569839695, 2.16233918241475, 1.94348336584128, 
                                  1.76030953589316, 1.57398365165524, 1.48010286623756, 1.32513096747946, 
                                  1.28232849305659, 1.26407605433669, 1.27114179598159, 1.23567759206272, 
                                  1.18776534049548, 1.14041338808179, 1.10970817963632, 1.08307872430731
                 ),
                 trend_log10c = c(NA, 0.235955218563125, 1.33823791349671, 
                                  0.0607453498302259, -0.114070466624236, 0.00897537944146443, 
                                  0.403447909576491, 0.647792138304764, 0.898973667615296, 
                                  1.62706389210081, 0.584579635667037, 0.837348243495363, 1.04214800866147, 
                                  1.43874016239004, 2.45437007136201, 1.63738252507763, 1.63738252507763, 
                                  3.09108352150306, 2.92151708503656, 2.1223914715644, 0.622818482734461, 
                                  0.665076002900152, 0.440012174544851, 0.444433997374107, 
                                  1.874151004406, 1.98261070308883, 1.60019072690764, 1.91327651912479, 
                                  1.59755586614094, 2.35730129445125, 2.63614274028582, 2.91961175246926, 
                                  2.35544952785373, 1.81595066271427, 0.393855239776779, -1.59399557640782, 
                                  -1.91673754895948, -2.35414232320695, -2.72727194089704, 
                                  -3.21548014224428, -3.17175285497492, -2.99651750205615, 
                                  -2.49630999086779, -2.07131990960092, -1.62228698944198, 
                                  -1.39342905195201, -1.00562316966424, -0.896362653634025, 
                                  -0.849130617820197, -0.867599348834218, -0.773368978244672, 
                                  -0.643279196748284, -0.512319390951289, -0.426113442696479, 
                                  -0.350634363184159),
                 trend_color = c(NA, "#a7ad06", "#00b51b", 
                                 "#c1ac03", "#d7a800", "#c9ac02", "#97ae08", "#78af0c", "#59b110", 
                                 "#00b51b", "#80af0b", "#61b00f", "#48b212", "#19b418", "#00a980", 
                                 "#00b51b", "#00b51b", "#009fcf", "#00a1bb", "#00ad5b", "#6ab00e", 
                                 "#65b00e", "#7eaf0b", "#7daf0b", "#00b13a", "#00b047", "#00b51b", 
                                 "#00b13f", "#00b51b", "#00aa71", "#00a790", "#00a3b0", "#00aa71", 
                                 "#00b236", "#79af0c", "#eb5400", "#f13a00", "#f91700", "#fb0006", 
                                 "#df002b", "#e20028", "#eb001b", "#fd0a00", "#f52900", "#ee4900", 
                                 "#ea5900", "#e37300", "#e27b00", "#e17e00", "#e17d00", "#e08300", 
                                 "#de8b00", "#dc9300", "#da9800", "#d99d00")),
            row.names = c(NA, -55L), class = c("data.table", "data.frame"))

假设 tab_temp$trend_colortab_ani$trend_color 相同(您未提供),我通过将 color 参数从美学中移出得到了您想要的结果第二个 geom_abline 并将颜色设置为 trend_color。那么 scale_color_manual 是不必要的:

temp_ani <- 
  ggplot(tab_temp, aes(tot, new_loess)) + 
  geom_line(size = 1.25) +
  geom_point(size = 2) + 
  geom_abline(aes(slope = ref_log10m, intercept = ref_log10c), size = 1.1, linetype = "dashed", alpha = 0.3) +
  geom_abline(aes(slope = trend_log10m, intercept = trend_log10c), color = tab_temp$trend_color, size = 1.1, linetype = "dashed", alpha = 0.7,
              show.legend = F) +
  transition_reveal(t) +
  scale_x_continuous(name = "tot", trans = "log10", limits = c(24, 786.5)) + 
  scale_y_continuous(name = "new_loess", trans = "log10", limits = c(21, 601.7)) +
  theme_classic() +
  theme(axis.title = element_text(size = 18),
        axis.text = element_text(size = 16))