By Maksym Matsala


2018-11-07 19:18:29 8 Comments

I can make through transition_reveal appearing geom_points. Looks nice, but I also wanna add exponential regression and visualise both simultaneously. Transition_reveal makes render neither with both smooth and points, nor with just smooth. Its possible only for geom_points. Whats wrong?

My gif only with geom_points

static picture also with geom_smooth

Here is a code and error. Should be exponential visualisation of decomposition process of tree wood, where t - age of sampling, Pt - its density.

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

Data:

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 comments

@Maksym Matsala 2018-11-08 23:25:05

Thanks a lot for code above, exponential nls can be built just through

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

instead of mutate(predict=predict(loess(Pt ~ t, data=.), .)) %>%

@Jimbou 2018-11-08 13:21:11

You can try to calculate the regression before the plotting. I have no idea about nls. Thus I used the loess function. The challenge is the combination of independent points and line.

# 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

Then you can do

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

Related Questions

Sponsored Content

5 Answered Questions

[SOLVED] ggplot/mapping US counties — problems with visualization shapes in R

  • 2014-05-17 17:10:32
  • user3648073
  • 11586 View
  • 8 Score
  • 5 Answer
  • Tags:   r ggplot2 tmap

0 Answered Questions

Can't find where modified ggsurv function error comes from (non-numeric argument)

1 Answered Questions

[SOLVED] Timeseries and its visualisation

1 Answered Questions

[SOLVED] R error: "invalid type (NULL) for variable"

  • 2017-08-11 16:32:07
  • Snow Dickson
  • 2392 View
  • 0 Score
  • 1 Answer
  • Tags:   r

2 Answered Questions

[SOLVED] Add secondary X-axis with facets

  • 2017-08-02 18:22:47
  • Marty999
  • 227 View
  • 0 Score
  • 2 Answer
  • Tags:   r ggplot2 axis facet

1 Answered Questions

[SOLVED] plot graphs in R while showing all the x axis values

  • 2017-05-28 07:17:42
  • Dush
  • 455 View
  • 0 Score
  • 1 Answer
  • Tags:   r plot

2 Answered Questions

[SOLVED] filtering out lowest numbers in a column by group

  • 2016-02-01 03:12:48
  • iskandarblue
  • 80 View
  • -1 Score
  • 2 Answer
  • Tags:   r filter

1 Answered Questions

[SOLVED] "minimum count is not zero" error for zero inflated model

1 Answered Questions

[SOLVED] R - How to avoid loops when comparing two datasets?

1 Answered Questions

Looping subsets in plm

  • 2014-02-25 20:31:19
  • user3352474
  • 1272 View
  • 2 Score
  • 1 Answer
  • Tags:   r loops subset plm

Sponsored Content