Skip to content

Instantly share code, notes, and snippets.

@lawsofthought
Created April 26, 2017 20:11
Show Gist options
  • Save lawsofthought/3397727279401c3e4b64cd79552e24c6 to your computer and use it in GitHub Desktop.
Save lawsofthought/3397727279401c3e4b64cd79552e24c6 to your computer and use it in GitHub Desktop.
A R script to illustrate how to make box, bean, pirate, etc, plots
# Some R code to demo boxplots, beanplots, pirateplots, etc
library(psych)
library(dplyr)
library(tidyr)
library(beanplot)
library(yarrr)
# Load the cushny data
data(cushny)
# Plot it using error.bars. Following example in psych package manual
error.bars(cushny[1:4],
within=TRUE,
ylab="Hours of sleep",
xlab="Drug condition",
main="95% confidence of within subject effects")
# Reformat the data into a "long" and "tidy" format
Df <- select(cushny, Control, drug1, drug2L, drug2R) %>%
mutate(subject = factor(seq(length(Control)))) %>%
select(subject, everything()) %>%
gather(drug, sleep, Control:drug2R) %>%
arrange(subject)
# A box plot
boxplot(sleep ~ drug,
data=Df,
ylab='Hours of sleep',
xlab='Drug')
# Beanplot
beanplot(sleep ~ drug,
data=Df,
ylab='Hours of sleep',
xlab='Drug',
col='lightgray',
overallline = 'median')
# Pirateplot
pirateplot(formula = sleep ~ drug,
data = Df,
theme = 3,
point.o = 1,
ylab='Hours of sleep',
xlab='Drug')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment