0

I am trying to interactively show both points and boxplots of the same data in a ggplotly situation. "dodged" positioning does the job in ggplot, but when passing to plotly positioning goes off--how do I get boxes and points to line up? (Essentially throwing points on top of this question. I also realize that an answer to this question would likely also be an answer to my question, though there may be more answers for my issue.)

What I want is for both layers to show up together, even when a group is missing at a location (either centered or in the group location), for examply like so:

enter image description here

What I get with interactivity so far is this:

library(plotly)
mtcars_boxplot <- mtcars %>%
  mutate(cyl=as.factor(cyl)) %>%
  mutate(vs=as.factor(vs)) %>%
  
  ggplot(aes(y=mpg, x=cyl)) +
  geom_boxplot(aes(color=vs), position=position_dodge())+
  geom_point(aes(color=vs), position=position_jitterdodge(), size = 0.5) 

mtcars_boxplot %>%
  ggplotly()  %>%
  layout(boxmode='group')

enter image description here

You can see that for cyl=8, the points are centered, but the box shows up in its group's location.

My question is: how do I get an interactive version of the first image, or something similar (preferably using ggplotly)?

maitagorri
  • 41
  • 5

1 Answers1

1

I found a way to do this--not with ggplot, but pure plotly:

mtcars_boxplot <- mtcars %>%
  mutate(cyl=as.factor(cyl)) %>%
  mutate(vs=as.factor(vs)) %>%
  plot_ly(type="box", 
    x = ~cyl, 
    y = ~mpg,
    color = ~vs,
    alignmentgroup = ~MOTART,
    boxpoints = "all",
    pointpos = 0,
    jitter = 1) %>%
  layout(boxmode='group')

If there is a ggplotly-answer, I would still love to know that one. (This actually ends up aligning more nicely, but is also more work when working in ggplot otherwise.)

maitagorri
  • 41
  • 5