My question is about expanding R
plotly
's grouped violin plot to a case with more than two groups.
Taking the data that are used in the grouped violin plot example code and adding a third level
to df$sex
:
library(dplyr)
set.seed(1)
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")
df <- df %>%
rbind(df[sample(nrow(df), 100, replace = F),] %>%
dplyr::mutate(sex = "undefined", day = sample(df$day, 100 , replace = F), day = sample(df$day, 100, replace = F)))
df$sex <- factor(df$sex)
Trying to plot this with:
plotly::plot_ly(x = df$day, y = df$total_bill, type = 'violin', split = df$sex, color = df$sex)
I get the violins of each of the sexes centered rather than split:
And this remains the case if I switch split = df$sex
to name = df$sex
.
But if I change type = 'violin'
to type = 'bar'
I do get df$sex
split:
Any idea how to get this to work for the type = 'violin'
case?