R `gganimate`使用“过渡”时,保留不需要的图层的过去数据`

R `gganimate`使用“过渡”时,保留不需要的图层的过去数据`,r,animation,ggplot2,gganimate,R,Animation,Ggplot2,Gganimate,我正在使用gganimate在绘图区域中需要4个元素: 一条geom\u线() Ageom_点() 具有静态斜率和颜色的geom\u abline(),变化截距 另一个geom_abline()具有不同的斜率,颜色,和截距 前3个元素按照我的要求设置动画: 其中只有geom\u line()逐渐显示,其中geom\u line()和第一个geom\u abline()仅显示该帧的数据 但当我以这种方式添加第四层时(我需要手动调色板),过去的geom_abline()不会消失: 我尝试添加参数

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

  • 一条
    geom\u线()
  • A
    geom_点()
  • 具有静态
    斜率
    颜色
    geom\u abline()
    ,变化
    截距
  • 另一个
    geom_abline()
    具有不同的
    斜率
    颜色
    ,和
    截距
  • 前3个元素按照我的要求设置动画:

    其中只有
    geom\u line()
    逐渐显示,其中
    geom\u line()
    和第一个
    geom\u abline()
    仅显示该帧的数据

    但当我以这种方式添加第四层时(我需要手动调色板),过去的
    geom_abline()
    不会消失:

    我尝试添加参数
    transition\u reveal(keep\u last=F)
    ,还尝试使用多个
    shadow\u 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)
    

    假设
    tab\u temp$trend\u color
    tab\u ani$trend\u color
    (您没有提供)相同,我通过将
    color
    参数从第二个
    geom\u abline
    的美学中移出,并将color设置为
    trend\u color
    ,得到了您想要的结果。那么就不需要使用
    比例\u颜色\u手册

    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))
    

    temp\u ani是否需要
    tab\u ani
    ?您没有在数据中提供它,但相关列似乎在
    tab\u temp
    我的错误中。你在回答中的假设是正确的。
    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))