Skip to content

Instantly share code, notes, and snippets.

@ajstewartlang
Last active November 24, 2023 21:15
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ajstewartlang/89dcdf01c4512a213141a16e9243626d to your computer and use it in GitHub Desktop.
Save ajstewartlang/89dcdf01c4512a213141a16e9243626d to your computer and use it in GitHub Desktop.
Hacked together code for animated raincloud plots for N=20 and N=500 where no difference exists in underlying populations
# Usimg @micahgallen's nice raincloud plot code
# https://wellcomeopenresearch.org/articles/4-63
# and fork of benmarwick/geom_flat_violin.R code
# gganimate by Thomas Lin Pedersen - @thomasp85
devtools::install_github('thomasp85/gganimate')
library(tidyverse)
library(gganimate)
library(RColorBrewer)
library(plyr)
#the following is a fork of benmarwick/geom_flat_violin.R
source("https://gist.githubusercontent.com/ajstewartlang/6c4cd8ab9e0c27747424acdfb3b4cff6/raw/fb53bd97121f7f9ce947837ef1a4c65a73bffb3f/geom_flat_violin.R")
#plotting two groups when no difference exists
df <- NULL
set.seed(1111)
sample_size = 20 # Change sample size here to 500
for (i in 1:10) {
a <- rnorm(sample_size, mean = 10, sd = 2)
b <- rnorm(sample_size, mean = 10, sd = 2)
a <- cbind(a, rep ("A", sample_size), rep (i, sample_size))
b <- cbind(b, rep("B", sample_size), rep (i, sample_size))
df <- rbind(df, (rbind(a, b)))
}
df <- as.tibble(df)
colnames(df) <- c("Score","Condition", "Sample")
df$Score <- as.numeric(df$Score)
raincloud_theme = theme(
text = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
axis.text = element_text(size = 12),
axis.text.x = element_text(angle = 45, vjust = 0.5),
legend.title=element_text(size=12),
legend.text=element_text(size=12),
legend.position = "right",
plot.title = element_text(lineheight=.8, face="bold", size = 16),
panel.border = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'),
axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid'))
lb <- function(x) mean(x) - sd(x)
ub <- function(x) mean(x) + sd(x)
dataRT <- df
sumld <- ddply(dataRT, ~Condition, summarise, mean = mean(Score), median = median(Score), lower = lb(Score), upper = ub(Score))
ggplot(data = dataRT, aes(y = Score, x = Condition, fill = Condition)) +
geom_flat_violin(position = position_nudge(x = .2, y = 0), alpha = .8, trim=FALSE) +
geom_point(aes(y = Score, color = Condition), position = position_jitter(width = .15), size = .5, alpha = 0.8) +
geom_boxplot(width = .1, outlier.shape = NA, alpha = 0.5) +
transition_states(Sample, transition_length = 20, state_length = 1) +
guides(fill = FALSE) +
guides(color = FALSE) +
scale_color_brewer(palette = "Accent") +
scale_fill_brewer(palette = "Accent") +
coord_flip() +
theme_bw() +
raincloud_theme +
labs (x = "Condition", y="DV", title= paste0("Sample size = ", sample_size))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment