0

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: enter image description here

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: enter image description here

Any idea how to get this to work for the type = 'violin' case?

dan
  • 6,048
  • 10
  • 57
  • 125
  • 2
    It works fine for me if you follow the code they give on the page they linked? – Axeman Jul 10 '20 at 17:52
  • 2
    Quick test suggests you need the `%>% layout(violinmode = 'group')` that is used in the example you link. – Axeman Jul 10 '20 at 17:55

0 Answers0