David
David

Reputation: 3

Display animated plot using gganimate in knitr Markdown for ioslides presentation: root directory

I want to embed a plot produced with ggplot and gganimate into a slideshow using knitr. I can produce animation when data and .Rmd file in same folder.

Here is a reproducible example for animation.

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(ggplot2)
library(gganimate)
library(gapminder)
```

## Static plot create
```{r, }
ranim <- ggplot(gapminder, aes(x = gdpPercap,y=lifeExp, 
                                  size = pop,
                                  colour = country)) +
      geom_point(show.legend = FALSE, alpha = 0.7) +
      scale_color_viridis_d() +
      scale_size(range = c(2, 12)) +
      scale_x_log10() +
   labs(x = "GDP per capita", y = "Life expectancy")
```

## Static plot
```{r, }
ranim
```

## Build animate
```{r, }
ranim2 <- ranim + 
transition_time(year) + 
labs(title = "Year: {frame_time}")
```

## View animate
```{r, }
animate(ranim2)
```

However, the problem occurs when I used local data saved into a subfolder. I open a project in a folder 'Project1'. I save data in a subfolder 'Data'. I set the options to root directory into the data folder.

knitr::opts_knit$set(root.dir = './Data')

My .Rmd file is saved in the folder Project1. The code is below and produces a blank slide when I compile. I can produce the animated file in the Viewer by running the code chunk manually from the .Rmd. But when the html compiles it's blank.

Is there a recommended setup for organizing local data in project subfolder and producing markdown slides from .Rmd saved in main Project1 folder?

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_knit$set(root.dir = './Data')
library(ggplot2)
library(gganimate)
library(gapminder)
```

## slide 1
```{r, }
datain <- read.csv("table1.csv")
panim <- ggplot(datain, aes(x, y, frame = year)) + geom_point()
```

## Static plot view
```{r, }
panim
```

## Static plot add animate
```{r, }
panim2 <- panim + transition_time(year) + 
labs(title = "Year: {frame_time}")
```

## Activate animate 
```{r, }
animate(panim2)
```

Upvotes: 0

Views: 1738

Answers (1)

L Smeets
L Smeets

Reputation: 942

Not sure what exactly your problem is, or what slide output styles you use, but this code works fine for for me (I get one slide with a static image and one slide with an animation).

---

title: "presentation"

author: "Me"

date: "20 FEB 2019"

output: ioslides_presentation

---

## slide 1

```{r,warning=F}

    library(ggplot2)
    library(gganimate)
    library(gapminder)

```

## slide 2

```{r}

ranim <- ggplot(gapminder, aes(x = gdpPercap,y=lifeExp, 
                                  size = pop,
                                  colour = country)) +
      geom_point(show.legend = FALSE, alpha = 0.7) +
      scale_color_viridis_d() +
      scale_size(range = c(2, 12)) +
      scale_x_log10() +
   labs(x = "GDP per capita", y = "Life expectancy")

```

## slide 3

```{r}

ranim

```

## slide 4

```{r}

 ranim2 <- ranim + 
transition_time(year) + 
labs(title = "Year: {frame_time}")

```

## slide 5

```{r}

    animate(ranim2)

```

Upvotes: 1

Related Questions