我在 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)

但我特別嘗試使用用基礎 R 創建的圖形來做到這一點。這是因為我發現著色方案更有效,我似乎無法像使用基礎 R 一樣清楚地顯示 ggplot 版本中的不同顏色版本。

有人可以告訴我如何將這 3 個圖形“影片/附加”在一起嗎?

謝謝!
uj5u.com熱心網友回復:
您是否正在尋找這樣的解決方案?
我們可以用 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")

uj5u.com熱心網友回復:
我們可以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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315852.html
下一篇:在同一圖上用置信區間覆寫兩條線
