Winston Chang edited this page May 2, 2012 · 12 revisions

I've added a new geom for violin plots. Here are some examples.

# Generate some data
dat <- data.frame(x=LETTERS[1:3], y=rnorm(90))
# Get rid of half the C entries (to illustrate scaling to sample size)
dat <- dat[ dat$x!="C" | c(T,F), ]
# Basic
ggplot(dat, aes(x=x, y=y)) + geom_violin()

# Narrower, with points overlaid
ggplot(dat, aes(x=x, y=y)) + geom_violin(width=.5) + geom_point(shape=21)

# With dotplot
ggplot(dat, aes(x=x, y=y)) + geom_violin() +
  geom_dotplot(binwidth=.2, binaxis="y", stackdir="center")

# Scale area proportional to sample size
ggplot(dat, aes(x=x, y=y)) + geom_violin(scale="count") +
  geom_dotplot(binwidth=.2, binaxis="y", binstataxis="y", stackdir="center")

# With tails and points
ggplot(dat, aes(x=x, y=y)) + geom_violin(trim=FALSE) + geom_point(shape=21)

# With tails, with smaller bandwidth using adjust
ggplot(dat, aes(x=x, y=y)) + geom_violin(adjust=.3, trim=FALSE) + geom_point(shape=21)

# Dodging
ggplot(dat, aes(x="foo", y=y, fill=x)) + geom_violin()

# With box plot
ggplot(dat, aes(x=x, y=y)) + geom_violin() + 
  geom_boxplot(width=.1, outlier.size=0, fill="grey50") +
  stat_summary(fun.y=median, geom="point", fill="white", shape=21, size=4)

# Dodging with boxplot overlay
# mapping group=x is necessary for dodging to work for boxplots and points
pd <- position_dodge(0.9)
ggplot(dat, aes(x="foo", group=x, y=y)) +
  geom_violin(aes(fill=x), position=pd) +
  geom_boxplot(width=.1, outlier.size=0, fill="grey50", position=pd) +
  stat_summary(fun.y=median, geom="point", fill="white", shape=21, size=4, position=pd)

# Median (black) and mean (white) points
ggplot(dat, aes(x=x, y=y)) + geom_violin() +
  stat_summary(fun.y=median, geom="point", fill="black", shape=21, size=5) + 
  stat_summary(fun.y=mean, geom="point", fill="white", shape=23, size=3)