Home > Enterprise >  Attaching Multiple Graphs Together in R
Attaching Multiple Graphs Together in R

Time:10-10

I made the following 3 graphs in R:

#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")

enter image description here

I am trying to make an "animation" of these graphs : plot_1 -> plot_2 -> plot_3

I was able to do something similar using 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)

enter image description here

But I am specifically trying to do this using the graphs created with base R. This is because I find the coloring scheme more effective, I can't seem to as clearly show the different colors in the ggplot version as I can with the base R version.

enter image description here

Can someone please show me how to "animate/attach" these 3 graphs together?

enter image description here

Thanks!

CodePudding user response:

Are you looking for such a solution?

We could use par()

With the par() function, we could include the option mfrow=c(nrows, ncols) to create a matrix of nrows x ncols plots that are filled in by row.

mfcol=c(nrows, ncols) fills in the matrix by columns.

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")

enter image description here

CodePudding user response:

We may try in gifski animation in a markdown page

---
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))
  }
 ```

Or if we just want to save the file as 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)
  • Related