❝本节来介绍如何使用「camcorder」包来将「ggplot2图形转换为GIF动画文件」,下面小编就来通过几个案例做演示,希望各位观众老爷能够喜欢。更多详细内容请参考作者官方文档❞
官方文档
https://thebioengineer.github.io/camcorder/
关注下方公众号下回更新不迷路
加载R包
代码语言:javascript复制library(tidyverse)
install.packages("camcorder")
library(camcorder)
定义文件属性
代码语言:javascript复制gg_record(
dir = file.path("~/Desktop", "plot"),
device = "png",
units = "in",
dpi = 300)
代码语言:javascript复制❝dir: 指定了保存图表的文件路径 device: 指定了保存图表的文件格式 units: 指定了图表尺寸的单位,它设置为 "in",即英寸 dpi: 指定了图表的分辨率,单位是每英寸点数(DPI) ❞
ggplot(mtcars, aes(x = mpg, y = hp))
geom_point()
代码语言:javascript复制ggplot(mtcars, aes(x = mpg, y = hp))
geom_point(aes(shape = as.factor(gear)))
代码语言:javascript复制ggplot(mtcars, aes(x = mpg, y = hp))
geom_point(aes(color = as.factor(gear)))
代码语言:javascript复制ggplot(mtcars, aes(x = mpg, y = hp))
geom_point(aes(color = as.factor(gear)))
geom_path()
代码语言:javascript复制ggplot(mtcars, aes(x = mpg, y = hp))
geom_point(aes(color = disp))
geom_smooth()
代码语言:javascript复制ggplot(mtcars, aes(x = mpg, y = hp))
geom_smooth()
geom_point(aes(color = disp))
代码语言:javascript复制ggplot(mtcars, aes(x = mpg, y = hp))
geom_smooth()
geom_point(aes(color = disp))
scale_color_viridis_c()
theme_light()
代码语言:javascript复制ggplot(mtcars, aes(x = mpg, y = hp))
geom_smooth()
geom_point(aes(color = disp))
scale_color_viridis_c()
theme_light()
labs(
title = "MPG vs Horse Power!",
subtitle = "Power and economy, the classic compromise!"
)
代码语言:javascript复制ggplot(mtcars, aes(x = mpg, y = hp))
geom_smooth()
geom_point(aes(color = disp))
scale_color_viridis_c()
theme_light(base_family = "Roboto Mono")
labs(
title = "MPG vs Horse Power!",
subtitle = "Power and economy, the classic compromise!",
x = "Efficiency (Miles/Gallon)",
y = "Power (Horsepower)",
color = "Displacementn(Cubic Inch)"
)
生成gif
代码语言:javascript复制gg_playback(
name = file.path("~/Desktop","plot","diamonds.gif"),
first_image_duration = 1,
last_image_duration = 2,
frame_duration = .5,
image_resize = 600)
❝name: 指定了保存GIF动画的文件路径。示例表示结果将保存到桌面上一个名为 "plot" 的文件夹内,文件名为 "diamonds.gif" first_image_duration: 第一帧图像在GIF动画中显示的时间,单位是秒。在这里设置为5秒 last_image_duration: 最后一帧图像在GIF动画中显示的时间,单位是秒。在这里设置为15秒 ❞
❝frame_duration: 除了第一帧和最后一帧之外,其他所有帧的显示时间,单位是秒。在这里设置为0.4秒 image_resize: 图像的大小将被调整为这个参数指定的值,单位是像素。在这里设置为800像素。 ❞