transition_reveal不适用于geom_point和geom_smooth

时间:2018-11-07 19:18:29

标签: r ggplot2 gganimate

我可以使transition_reveal出现在geom_points中。看起来不错,但我也想添加指数回归并同时可视化两者。 Transition_reveal使渲染既不具有平滑和点,也不能具有平滑。仅适用于geom_points。怎么了?

My gif only with geom_points

static picture also with geom_smooth

这是代码和错误。应该是树木分解过程的指数可视化,其中t-采样年龄,Pt-密度。

gif <- ggplot(data, aes(t,Pt)) + 
         geom_point(shape=21, aes(fill=group),col="black",size=3) + 
         stat_smooth(method="nls", data=decay, 
                     method.args = list(formula = y~a*exp(-b*x), 
                     start = list(a=570,b=2*log(2)/570)), 
                     se=F,size=0.5, 
                     col="black",linetype="dotted") + 
         transition_reveal(Pt,t)


Error in `[.data.frame`(df, , c("alpha", "colour", "size", "linetype")) : undefined columns are chosen

数据:

structure(list(Pt = c(450L, 430L, 321L, 250L, 560L, 550L, 221L,
221L, 117L, 400L, 420L, 460L, 490L, 430L, 480L, 290L, 330L, 190L,
321L, 540L, 520L, 400L, 560L, 550L, 561L, 540L, 333L, 567L, 580L,
228L, 290L, 190L, 340L, 440L, 311L, 287L, 510L, 500L, 470L, 431L,
417L, 420L, 300L, 400L, 390L, 380L, 350L, 375L, 435L, 317L, 220L,
500L, 490L, 270L, 267L, 298L, 207L, 180L, 205L, 287L, 275L, 520L,
500L, 404L, 392L, 220L, 291L, 300L, 325L, 283L, 310L, 219L, 430L,
290L, 350L, 344L, 523L, 500L, 344L, 570L, 560L, 520L, 87L, 550L,
534L, 400L, 390L, 380L, 350L, 375L, 440L, 303L, 210L, 490L),
    P0 = c(570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
    570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
    570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
    570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
    570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
    570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
    570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
    570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
    570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
    570L, 570L, 570L, 570L, 570L), t = c(12L, 13L, 27L, 29L,
    4L, 3L, 42L, 47L, 49L, 8L, 8L, 8L, 7L, 7L, 7L, 12L, 12L,
    27L, 27L, 3L, 3L, 11L, 2L, 2L, 2L, 2L, 29L, 1L, 1L, 36L,
    35L, 32L, 12L, 13L, 27L, 29L, 4L, 3L, 12L, 11L, 19L, 8L,
    18L, 8L, 7L, 7L, 7L, 22L, 13L, 27L, 28L, 3L, 3L, 9L, 38L,
    39L, 39L, 41L, 43L, 27L, 29L, 4L, 3L, 12L, 11L, 19L, 8L,
    8L, 8L, 7L, 7L, 35L, 7L, 35L, 12L, 27L, 3L, 3L, 11L, 2L,
    2L, 2L, 42L, 1L, 3L, 9L, 6L, 7L, 8L, 24L, 13L, 25L, 29L,
    5L), k = structure(c(18L, 25L, 23L, 45L, 3L, 7L, 29L, 21L,
    51L, 65L, 58L, 40L, 24L, 60L, 33L, 73L, 67L, 62L, 23L, 13L,
    49L, 50L, 6L, 12L, 5L, 41L, 14L, 4L, 1L, 38L, 17L, 54L, 63L,
    19L, 28L, 31L, 43L, 64L, 8L, 36L, 9L, 58L, 56L, 65L, 72L,
    74L, 77L, 16L, 22L, 26L, 52L, 64L, 71L, 80L, 20L, 10L, 39L,
    44L, 32L, 37L, 34L, 30L, 64L, 47L, 53L, 70L, 81L, 79L, 78L,
    83L, 82L, 42L, 60L, 17L, 61L, 15L, 46L, 64L, 69L, 2L, 6L,
    68L, 66L, 57L, 27L, 59L, 76L, 74L, 75L, 11L, 19L, 35L, 55L,
    48L), .Label = c("-0,017391743", "0", "0,004424894", "0,005277057",
    "0,007957728", "0,008849789", "0,011906028", "0,016075306",
    "0,016450007", "0,016629304", "0,017446264", "0,017859041",
    "0,018022407", "0,018534271", "0,018703508", "0,019032288",
    "0,019307298", "0,019699065", "0,019912433", "0,019957571",
    "0,020159014", "0,020791564", "0,02126649", "0,021604424",
    "0,021680858", "0,021730911", "0,021746841", "0,022438646",
    "0,022558897", "0,022951887", "0,023660488", "0,023782009",
    "0,024550037", "0,025133285", "0,025276142", "0,025411661",
    "0,025413116", "0,02545252", "0,025972245", "0,026801234",
    "0,027033611", "0,027330418", "0,027806409", "0,028114134",
    "0,028419843", "0,028684966", "0,028685124", "0,030246194",
    "0,030602516", "0,032197438", "0,03231556", "0,034000315",
    "0,034034047", "0,034331634", "0,034432029", "0,035658549",
    "0,035718083", "0,038172706", "0,039352424", "0,04026445",
    "0,040641934", "0,040689344", "0,043057562", "0,043676087",
    "0,044271477", "0,044755434", "0,045545309", "0,045903775",
    "0,045908609", "0,050105727", "0,050410323", "0,054212803",
    "0,056312953", "0,057923587", "0,060962901", "0,06324827",
    "0,069671887", "0,070226397", "0,080231736", "0,083023822",
    "0,084039137", "0,087009152", "0,100027066"), class = "factor"),
    year = c(2006L, 2005L, 1991L, 1989L, 2014L, 2015L, 1976L,
    1971L, 1969L, 2010L, 2010L, 2010L, 2011L, 2011L, 2011L, 2006L,
    2006L, 1991L, 1991L, 2015L, 2015L, 2007L, 2016L, 2016L, 2016L,
    2016L, 1989L, 2017L, 2017L, 1982L, 1983L, 1986L, 2006L, 2005L,
    1991L, 1989L, 2014L, 2015L, 2006L, 2007L, 1999L, 2010L, 2000L,
    2010L, 2011L, 2011L, 2011L, 1996L, 2005L, 1991L, 1990L, 2015L,
    2015L, 2009L, 1980L, 1979L, 1979L, 1977L, 1975L, 1991L, 1989L,
    2014L, 2015L, 2006L, 2007L, 1999L, 2010L, 2010L, 2010L, 2011L,
    2011L, 1983L, 2011L, 1983L, 2006L, 1991L, 2015L, 2015L, 2007L,
    2016L, 2016L, 2016L, 1976L, 2017L, 2015L, 2009L, 2012L, 2011L,
    2010L, 1994L, 2005L, 1993L, 1989L, 2013L)), .Names = c("Pt",
"P0", "t", "k", "year"), class = "data.frame", row.names = c(NA,
-94L))

2 个答案:

答案 0 :(得分:1)

您可以尝试在绘制之前计算回归。我对nls一无所知。因此,我使用了loess函数。挑战是独立点和线的组合。

# the static plot 
library(tidyverse)
library(gganimate)
d %>% 
  mutate(gr=factor(case_when(Pt<200 ~ 1,
                   Pt<300 ~ 2,
                   Pt<400 ~ 3,
                   Pt<500 ~ 4, 
                   TRUE ~ 5))) %>% 
ggplot(aes(t,Pt)) + 
  geom_point(aes(fill=gr),
             shape=21,col="black",
             size=3, show.legend = F) + 
  geom_smooth(method = "loess",se = FALSE,
              col="black",linetype="dotted")

enter image description here

那你就可以做

d %>% 
  mutate(gr=factor(case_when(Pt<200 ~ 1,
                             Pt<300 ~ 2,
                             Pt<400 ~ 3,
                             Pt<500 ~ 4, 
                             TRUE ~ 5))) %>% 
  arrange(t) %>% 
  rowid_to_column() %>% # arrange and add rowid for the transition_reveal
  mutate(predict=predict(loess(Pt ~ t, data=.), .)) %>% # calculate the predicted values
  mutate(predict_lag=lag(predict,default = max(predict)), # add the next values for segments
         t_lag=lag(t,default = min(t))) %>% 
  ggplot(aes(t,Pt)) + 
  geom_point(aes(fill=gr),
             shape=21,col="black",
             size=3, show.legend = F) + 
   geom_segment(aes(x=t, y=predict, xend=t_lag, yend=predict_lag),
                col="black",linetype="dotted") +
   transition_reveal(rowid, rowid)

enter image description here

答案 1 :(得分:0)

非常感谢上面的代码,指数nls可以通过以下方式构建

mutate(predict=predict(nls(Pt ~ a*exp(-b*t),start = list(a=570,b=2*log(2)/570), data=.), .)) %>%

代替mutate(predict=predict(loess(Pt ~ t, data=.), .)) %>%