Created
July 16, 2012 09:31
-
-
Save yannabraham/3121818 to your computer and use it in GitHub Desktop.
ggplot2 BeanPlots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 <− rnorm(50))); 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
I wish there was an option to turn off the symmetry from geom_violin():