Overlaying two violin plots

Hi!

I'm plotting two violin plots for comparison (over time) with plotly. Everything is working fine so far - but now I was asked to overlay both violin plots (for better comparison). For the sake of my life, it seems I'm not capable of getting it to work! They will always end side beside side.

Has anyone faced a similar challenge?

Best regards!

library(plotly)
library(dplyr)

df_test <- data.frame(side=rep(c("left","right"), 100), 
                       daysPassed=c(sample(1:100, 200, replace=T)), month=c(rep(201601,20),rep(201602,20),rep(201603,20),rep(201604,20),rep(201605,20),rep(201606,20),rep(201607,20),rep(201608,20),rep(201609,20),rep(201610,20))
)

df_test %>%
  plot_ly(
    x = ~side,
    y = ~daysPassed,
    frame = ~month,
    split = ~side,
    type = 'violin',
    points = F,
    spanmode = "hard",
    showlegend = F,
    box = list(
      visible = F
    ),
    meanline = list(
      visible = T
    )
  ) %>% 
  layout(
    yaxis = list(range = c(0, max(df_test$daysPassed, na.rm=T)),
                 title = "Days Passed",
                 zeroline = T,
                 autorange = F),
    xaxis = list(
      title = "",
      showticklabels = TRUE
    )
  )

I have very little experience with plotly, so there may well be a better way than adding a dummy x variable as I did below. I turned on the legend so it is clear which curve is left and right.

library(plotly)
library(dplyr)

df_test <- data.frame(side=rep(c("left","right"), 100), 
                      X = rep("x", 100),
                      daysPassed=c(sample(1:100, 200, replace=T)), month=c(rep(201601,20),rep(201602,20),rep(201603,20),rep(201604,20),rep(201605,20),rep(201606,20),rep(201607,20),rep(201608,20),rep(201609,20),rep(201610,20))
)

df_test %>%
  plot_ly(
    x = ~X,
    y = ~daysPassed,
    frame = ~month,
    #split = ~side,
    color = ~side,
    type = 'violin',
    points = F,
    spanmode = "hard",
    showlegend = T,
    box = list(
      visible = F
    ),
    meanline = list(
      visible = T
    )
  ) %>% 
  layout(
    yaxis = list(range = c(0, max(df_test$daysPassed, na.rm=T)),
                 title = "Days Passed",
                 zeroline = T,
                 autorange = F),
    xaxis = list(
      title = "",
      showticklabels = TRUE
    )
  )
1 Like

It seems it is not necessary to modify the data frame at all. Setting x="" in the plot_ly call is enough.

df_test <- data.frame(side=rep(c("left","right"), 100), 
                      daysPassed=c(sample(1:100, 200, replace=T)), month=c(rep(201601,20),rep(201602,20),rep(201603,20),rep(201604,20),rep(201605,20),rep(201606,20),rep(201607,20),rep(201608,20),rep(201609,20),rep(201610,20))
)

df_test %>%
  plot_ly(
    x = ~"",
    y = ~daysPassed,
    frame = ~month,
    #split = ~side,
    color = ~side,
    type = 'violin',
    points = F,
    spanmode = "hard",
    showlegend = T,
    box = list(
      visible = F
    ),
    meanline = list(
      visible = T
    )
  ) %>% 
  layout(
    yaxis = list(range = c(0, max(df_test$daysPassed, na.rm=T)),
                 title = "Days Passed",
                 zeroline = T,
                 autorange = F),
    xaxis = list(
      title = "",
      showticklabels = TRUE
    )
  )
1 Like

Hi FJCC,

I'm pretty much speechless. That's a really simple and therefore fantastic fix - it works perfectly!

Thank you very, very much - I highly appreciate your help!

Have a great day,

david

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.