This is a short post with one great animation. The goal here is to make a strong connection between a boxplot and a histogram as two ways to graphically represent data. As a quick review of a boxplot, recall that the ends of the boxes are the first and third quartiles so that half the data lies within the box. The line within the box is the median. The ends of the tails, or whiskers, are the minimum and maximum values, so that 25% of the data is each of the whiskers. In this example, the boxplot also includes dots to represent each data point, although they are jittered so that they aren’t all on top of each other. Each data point has some transparency, so darker dots are overlaps of more than one dot. The R code is at the end. Please share and subscribe if you enjoyed this animation.
Please share and like
Please help me find readers by forwarding this article to your friends (and even those who aren't your friends), sharing this post on social media, and clicking like. If you have any article ideas, feedback, or other views, please email me at briefedbydata@substack.com.
Thank you
In a crowded media market, it's hard to get people to read your work. I have a long way to go and I want to say thank you to everyone who has helped me find and attract subscribers.
R code
## Packages
library(ggplot2)
library(gganimate)
library(magick)
## Colors
MyPurple <- "#5B005B"
MyLightP <- "#dfdbdf"
MyLightP2 <- "#f8f4f8"
MyLightP3 <- "#fcfafc"
MyPurple5 <- "#9c669c"
## Create Data
data<- data.frame("value"=c(runif(4000, 0, 20),
rbinom(3000, 20, 0.5) + rnorm(3000, 0, 0.3),
runif(1000, 0, 20),
rbinom(2000, 20, 0.7)+ rnorm(2000, 0, 0.3),
runif(2000, 0, 20),
rbinom(2000, 20, 0.1)+ rnorm(2000, 0, 0.3),
runif(2000, 0, 20)),
"time"=rep(1:4, each = 4000))
## Create Animated Graphs
g1 <- ggplot(data, aes(x="",y = value)) +
geom_boxplot(fill = MyPurple5, color = "black",lwd = 1.5, fatten = 1) +
coord_flip() +
geom_jitter(width = 0.25, color = MyPurple, size = 2, alpha = 0.2) +
transition_states(time, transition_length = 3, state_length = 2 ) +
enter_fade() +
exit_fade() +
theme(plot.title=element_text(size=20),
plot.background = element_rect(fill = MyLightP3),
panel.background = element_rect(fill=MyLightP),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank()
) +
labs(title="Histogram and Boxplot", y = NULL, x = NULL)
g2 <- ggplot(data, aes(x = value)) +
geom_histogram(bins = 30, fill = MyPurple5, color = "black") +
transition_states(time, transition_length = 3, state_length = 2 ) +
enter_fade() +
exit_fade() +
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 16),
plot.background = element_rect(fill = MyLightP3),
panel.background = element_rect(fill = MyLightP),
plot.caption = element_text(hjust = c(1), size = c(14),
color = c(MyPurple) )
) +
labs(title = NULL, y = NULL, x = NULL,
caption=c("Briefed by Data || Thomas J Pfaff") )
BoxPlotAnimate <- animate(g1 , fps = 5, duration = 10,
width = 1456 / 2, height = (936 / 2) / 2,
renderer = magick_renderer() )
HistPlotAnimate <- animate(g2 , fps = 5, duration = 10,
width = 1456 / 2, height = 3 * (936 / 2) / 4,
renderer = magick_renderer() )
## Combine the two animated graph into one image
HistBoxAnimate <- image_append(c(BoxPlotAnimate[1],HistPlotAnimate[1]), stack = TRUE)
for( i in 2:50) {
TempGif <- image_append(c(BoxPlotAnimate[i], HistPlotAnimate[i]), stack = TRUE)
HistBoxAnimate <- c(HistBoxAnimate, TempGif)
}
## Save graph
anim_save("HistBoxAnimate.gif", HistBoxAnimate)
wow...great animation, I find it very usefull. thx.