Skip to content

Instantly share code, notes, and snippets.

@smarr
Forked from yannabraham/beanplots.R
Last active December 10, 2015 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smarr/4374042 to your computer and use it in GitHub Desktop.
Save smarr/4374042 to your computer and use it in GitHub Desktop.
Fixed small typo
## reproduce the figures from http://www.jstatsoft.org/v28/c01/paper using ggplot2
library(ggplot2)
## parameters
set.seed(2710)
## Figure 1
d <- rnorm(50)
plot(density(d)); rug(d)
ggplot(data=data.frame(value=d))+
stat_density(aes(x=value))+
geom_segment(aes(x=value,xend=value),y=0,yend=0.025,col='white')
## Figure 2
mu <- 2
si <- 0.6
c <- 500
ylim <- c(-7, 7)
bimodal <- c(rnorm(c/2, -mu, si), rnorm(c/2, mu, si))
uniform <- runif(c, -4, 4)
normal <- rnorm(c, 0, 1.5)
boxplot(bimodal, uniform, normal, ylim = ylim, main = "boxplot",names = 1:3)
d2 <- data.frame(
Distribution=factor(
c(rep('bimodal',c),rep('uniform',c),rep('normal',c)),
levels=c('bimodal','uniform','normal')
),
Value=c(bimodal,uniform,normal)
)
ggplot(data=d2)+
geom_boxplot(aes(x=Distribution,y=Value))
ggplot(data=d2)+
geom_violin(aes(x=Distribution,y=Value),fill='grey',trim=F)+
geom_segment(aes(
x=match(Distribution,levels(Distribution))-0.1,
xend=match(Distribution,levels(Distribution))+0.1,
y=Value,yend=Value),
col='black'
)
## Figure 3
data("singer", package = "lattice")
ylim <- c(55, 80)
ggplot(data=singer)+
geom_violin(aes(x=voice.part,y=height),fill='grey',trim=F)+
geom_segment(aes(
x=match(voice.part,levels(voice.part))-0.1,
xend=match(voice.part,levels(voice.part))+0.1,
y=height,yend=height),
col='black'
)
## Figure 4
## Figure 5
OrchardSprays$log_decrease <- log(OrchardSprays$decrease)
ggplot(data=OrchardSprays)+
geom_violin(aes(x=treatment,y=log_decrease),fill='grey',trim=F)+
geom_segment(aes(
x=match(treatment,levels(treatment))-0.1,
xend=match(treatment,levels(treatment))+0.1,
y=log_decrease,yend=log_decrease),
col='black'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment