Module # 13
Issaiah. Jennings
Module # 13
For this cool animation, I used the animation package in R to create a GIF that shows how a histogram changes as the mean of a dataset increases over time. It’s basically a moving picture of data that shifts a little more to the right with each frame. I really enjoyed this assignment give it a try!
library(animation)
>
> ani.options(interval = 0.05) # 100 / 0.05 = 20 fps
>
>
> # Save the animated histogram
> saveGIF({
+ par(mar = c(4, 4, 2, 1)) # Set margins
+ for (i in 1:100) {
+ # Simulate normal data, shifting the mean over time
+ data <- rnorm(200, mean = i * 0.1, sd = 1)
+
+ # Plot histogram
+ hist(data,
+ breaks = 20,
+ col = "skyblue",
+ border = "white",
+ main = paste("Iteration:", i),
+ xlim = c(0, 20),
+ ylim = c(0, 40),
+ xlab = "Value",
+ ylab = "Frequency")
+
+ # Add a vertical line at the mean
+ abline(v = mean(data), col = "red", lwd = 3)
+ }
+ }, movie.name = "shifting_histogram.gif")
Output at: shifting_histogram.gif
[1] TRUE
>

Comments
Post a Comment