在 R 中将多个图连接在一起
Attaching Multiple Graphs Together in R
我在 R 中制作了以下 3 个图表:
#create data
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 1
c = data.frame(a,b,i)
c$col = as.factor(ifelse(c$b>10,"red", "blue"))
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 2
d = data.frame(a,b,i)
d$col = as.factor(ifelse(d$a>10,"red", "blue"))
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 3
e = data.frame(a,b,i)
e$col = as.factor(ifelse(e$b>10,"red", "blue"))
#put all the datasets together (i=1, i=2, i=3)
g = rbind(c,d,e)
#plot
plot_1 = plot(c$a, type = "h", col = c$col, main = "plot_1, i = 1")
plot_2 = plot(d$a, type = "h", col = d$col, main = "plot_2, i = 2")
plot_3 = plot(e$a, type = "h", col = e$col, main = "plot_3, i = 3")
我正在尝试制作这些图表的“动画”:plot_1 -> plot_2 -> plot_3
我可以使用 ggplot2/gganimate 做类似的事情:
library(ggplot2)
library(gganimate)
animate(
ggplot(g, aes(x=a)) +
geom_histogram(binwidth=1) +
transition_states(i, state_length = 0.2) +
labs(title = "Group: {closest_state}"),
fps = 25)
但我特别尝试使用用 base R 创建的图表来做到这一点。这是因为我发现着色方案更有效,我似乎无法像我一样清楚地显示 ggplot 版本中的不同颜色可以使用基础 R 版本。
有人可以告诉我如何将这 3 个图表“animate/attach”在一起吗?
谢谢!
您正在寻找这样的解决方案吗?
我们可以使用 par()
使用 par()
函数,我们可以包含选项 mfrow=c(nrows, ncols)
来创建一个由 nrows x ncols 图组成的矩阵,按行填充。
mfcol=c(nrows, ncols)
按列填充矩阵。
par(mar = rep(2, 4))
par(mfrow =c(2,3))
plot_1 = plot(c$a, type = "h", col = c$col, main = "plot_1, i = 1")
plot_2 = plot(d$a, type = "h", col = d$col, main = "plot_2, i = 2")
plot_3 = plot(e$a, type = "h", col = e$col, main = "plot_3, i = 3")
我们可以尝试在 markdown 页面中加入 gifski
动画
---
title: "Title"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r data, echo = FALSE}
suppressPackageStartupMessages(library(gifski))
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 1
c = data.frame(a,b,i)
c$col = as.factor(ifelse(c$b>10,"red", "blue"))
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 2
d = data.frame(a,b,i)
d$col = as.factor(ifelse(d$a>10,"red", "blue"))
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 3
e = data.frame(a,b,i)
e$col = as.factor(ifelse(e$b>10,"red", "blue"))
#put all the datasets together (i=1, i=2, i=3)
g = rbind(c,d,e)
lst1 <- list(c, d, e)
```
```{r, animation.hook="gifski"}
for (i in seq_along(lst1)) {
plot(lst1[[i]]$a, type = 'h', col = lst1[[i]]$col,
main = paste0("plot_", i))
}
```
或者如果我们只想将文件保存为 gif
library(gifski)
lst1 <- list(c, d, e)
gif_file <- "/path/to/file.gif")
save_gif(
for (i in seq_along(lst1)) {
plot(lst1[[i]]$a, type = 'h', col = lst1[[i]]$col, main = paste0("plot_", i))
}, gif_file, 1280, 720, res = 144)
我在 R 中制作了以下 3 个图表:
#create data
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 1
c = data.frame(a,b,i)
c$col = as.factor(ifelse(c$b>10,"red", "blue"))
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 2
d = data.frame(a,b,i)
d$col = as.factor(ifelse(d$a>10,"red", "blue"))
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 3
e = data.frame(a,b,i)
e$col = as.factor(ifelse(e$b>10,"red", "blue"))
#put all the datasets together (i=1, i=2, i=3)
g = rbind(c,d,e)
#plot
plot_1 = plot(c$a, type = "h", col = c$col, main = "plot_1, i = 1")
plot_2 = plot(d$a, type = "h", col = d$col, main = "plot_2, i = 2")
plot_3 = plot(e$a, type = "h", col = e$col, main = "plot_3, i = 3")
我正在尝试制作这些图表的“动画”:plot_1 -> plot_2 -> plot_3
我可以使用 ggplot2/gganimate 做类似的事情:
library(ggplot2)
library(gganimate)
animate(
ggplot(g, aes(x=a)) +
geom_histogram(binwidth=1) +
transition_states(i, state_length = 0.2) +
labs(title = "Group: {closest_state}"),
fps = 25)
但我特别尝试使用用 base R 创建的图表来做到这一点。这是因为我发现着色方案更有效,我似乎无法像我一样清楚地显示 ggplot 版本中的不同颜色可以使用基础 R 版本。
有人可以告诉我如何将这 3 个图表“animate/attach”在一起吗?
谢谢!
您正在寻找这样的解决方案吗?
我们可以使用 par()
使用 par()
函数,我们可以包含选项 mfrow=c(nrows, ncols)
来创建一个由 nrows x ncols 图组成的矩阵,按行填充。
mfcol=c(nrows, ncols)
按列填充矩阵。
par(mar = rep(2, 4))
par(mfrow =c(2,3))
plot_1 = plot(c$a, type = "h", col = c$col, main = "plot_1, i = 1")
plot_2 = plot(d$a, type = "h", col = d$col, main = "plot_2, i = 2")
plot_3 = plot(e$a, type = "h", col = e$col, main = "plot_3, i = 3")
我们可以尝试在 markdown 页面中加入 gifski
动画
---
title: "Title"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r data, echo = FALSE}
suppressPackageStartupMessages(library(gifski))
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 1
c = data.frame(a,b,i)
c$col = as.factor(ifelse(c$b>10,"red", "blue"))
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 2
d = data.frame(a,b,i)
d$col = as.factor(ifelse(d$a>10,"red", "blue"))
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 3
e = data.frame(a,b,i)
e$col = as.factor(ifelse(e$b>10,"red", "blue"))
#put all the datasets together (i=1, i=2, i=3)
g = rbind(c,d,e)
lst1 <- list(c, d, e)
```
```{r, animation.hook="gifski"}
for (i in seq_along(lst1)) {
plot(lst1[[i]]$a, type = 'h', col = lst1[[i]]$col,
main = paste0("plot_", i))
}
```
或者如果我们只想将文件保存为 gif
library(gifski)
lst1 <- list(c, d, e)
gif_file <- "/path/to/file.gif")
save_gif(
for (i in seq_along(lst1)) {
plot(lst1[[i]]$a, type = 'h', col = lst1[[i]]$col, main = paste0("plot_", i))
}, gif_file, 1280, 720, res = 144)