我想把一个用ggplot和gganimate生成的图嵌入到一个幻灯片放映中。当数据和.Rmd文件在同一文件夹中时,我可以制作动画。
这是一个可重现的动画示例。
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo =假)
库(Ggplot2)
库(Gganimate)
库(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() +
实验室(x=“人均GDP”,y=“预期寿命”)
## Static plot
```{r, }
拉尼姆
## Build animate
```{r, }
ranim2 <- ranim +
Transition_time(年份)+
实验(标题=“年份:{frame_time}")
## View animate
```{r, }
动画(Ranim2)
然而,当我使用保存在子文件夹中的本地数据时,问题就出现了。我在文件夹'Project1‘中打开了一个项目。我将数据保存在“data”子文件夹中。我将选项设置为将根目录设置为data文件夹。
knitr::opts_knit$set(root.dir = './Data')
我的.Rmd文件保存在文件夹Project1中。下面的代码在我编译时生成了一个空白幻灯片。通过从.Rmd手动运行代码块,我可以在查看器中生成动画文件。但是当html编译时,它就是空白的。
对于组织项目子文件夹中的本地数据和从保存在主Project1文件夹中的.Rmd制作标记幻灯片,是否有推荐的设置?
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo =假)
数据(root.dir=‘./knitr::opts_knit$set’)
库(Ggplot2)
库(Gganimate)
库(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(年)+
实验(标题=“年份:{frame_time}")
## Activate animate
```{r, }
动画(Panim2)
发布于 2019-02-20 12:01:37
我不确定您的问题到底是什么,也不知道您使用的幻灯片输出样式是什么,但这段代码对我来说工作得很好(我得到了一张带有静态图像的幻灯片和一张带有动画的幻灯片)。
标题:“演示”
作者:“我”
日期:“2019年2月20日”
输出: ioslides_presentation
##幻灯片1
```{r,警告=F}
library(ggplot2)
library(gganimate)
library(gapminder)
```
##幻灯片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")
```
##幻灯片3
```{r}
ranim
```
## 幻灯片4
```{r}
ranim2 <- ranim +
transition_time(year) +
labs(title = "Year: {frame_time}")
```
## 幻灯片5
```{r}
animate(ranim2)
```
https://stackoverflow.com/questions/54777877
复制