Split Violin Plot or Beanplot

473 views
Skip to first unread message

Christopher Flach

unread,
Feb 16, 2017, 5:06:41 PM2/16/17
to ggplot2
I'm trying to do a split Violin Plot or Beanplot. I'd like it to look somewhat similar to the plot on page 6 of the link below with the following changes:
  1. Change the dashed line from the mean to a specific number such as 412.
  2. Create asymmetric bean plots based on the variable Exchange (variable in code provided). This is similar to what is shown in the picture. I just can’t get it to work when I was modifying my code.
  3. When the asymmetric bean plots are created, I’d like a line that goes through the average per market (not separate lines for each market and exchange combination). In the plot on page 6, they seem to have two different lines for each group and singer combination. 

Code:

Market <- c('SG', 'SG', 'SG', 'SG', 'SG', 'SG', 'SG', 'SG', 'Ind', 'Ind', 'Ind', 'Ind','Ind', 'Ind', 'Ind', 'Ind')
Prem <- c(400, 405, 433, 450, 420, 422, 415, 444, 351, 363, 387, 371, 350, 360, 375, 388)
Exchange <- c('On','On','On', 'On', 'On','On','On', 'On', 'Off', 'On', 'Off', 'On', 'Off', 'On', 'Off', 'On')

df <- data.frame(Market, Prem, Exchange)
attach(df)

data <- split(Prem, Market)

library(ggplot2)
library(vioplot)
library(beanplot)

names(data)[1] <- 'x'

do.call("vioplot", c(data, list(names = levels(Market), col = "white")))
par(mfrow = c(1,1))
par(lend=1)
beanplot(Prem~Market, data = df, col = list("black", c("grey", "white")))

I know 'side' in the beanplot function creates an asymmetric beanplot, but I'd like to keep the Markets separate and have them split by the variable exchange.

Wouter van der Bijl

unread,
Feb 17, 2017, 7:46:07 AM2/17/17
to ggplot2
Hi,

You are emailing the ggplot2 help list. Your question is not about ggplot2.

W.

Christopher Flach

unread,
Feb 17, 2017, 10:49:54 AM2/17/17
to ggplot2
Perhaps someone has a solution using ggplot...

John Dougherty

unread,
Feb 17, 2017, 4:19:14 PM2/17/17
to ggp...@googlegroups.com
On Thu, 16 Feb 2017 12:21:28 -0800 (PST)
Christopher Flach <flach...@gmail.com> wrote:

You might want to check into the geometries offered in ggplot2. I am
unfamiliar with "bean plots" but violin plots are possible in ggplot2.
You can get a very thorough introduction into the 'hows' in R Graphics
Cookbook by Chang. You will need to call "geom_violin()"

--

JWDougherty

Christopher Flach

unread,
Feb 17, 2017, 4:24:44 PM2/17/17
to John Dougherty, ggp...@googlegroups.com
Ok, thanks!

Sent from my iPhone
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to a topic in the Google Groups "ggplot2" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/ggplot2/2UvQYZ2N4RA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages