Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping dodge position in boxplot passed to plotly

I have a regular boxplot in ggplot2:

# working example
library(ggplot2)

mtcars %>%
  mutate(cyl=as.factor(cyl)) %>%
  mutate(vs=as.factor(vs)) %>%

  ggplot(aes(y=mpg, x=cyl)) +
  geom_boxplot(aes(colour=vs))

It looks like this: enter image description here

However, when I create an object and pass it to plotly, I lose the dodge position:

library(plotly)
mtcars_boxplot <-
mtcars %>%
  mutate(cyl=as.factor(cyl)) %>%
  mutate(vs=as.factor(vs)) %>%

  ggplot(aes(y=mpg, x=cyl)) +
  geom_boxplot(aes(colour=vs))

mtcars_boxplot %>%
  ggplotly() 

It looks like this: enter image description here

I tried to add position=position_dodge() & position=position_dodge2() but none of them worked:

library(plotly)

mtcars_boxplot <-
mtcars %>%
  mutate(cyl=as.factor(cyl)) %>%
  mutate(vs=as.factor(vs)) %>%

  ggplot(aes(y=mpg, x=cyl)) +
  geom_boxplot(aes(colour=vs), position=position_dodge2())

mtcars_boxplot %>%
  ggplotly() 

What should I do to keep the dodge position like the first plot?

like image 616
Hamideh Avatar asked Mar 04 '23 06:03

Hamideh


1 Answers

As suggested here, add layout(boxmode = "group")

library(plotly)
mtcars_boxplot %>%
  ggplotly() %>%
  layout(boxmode = "group")
like image 170
A. Suliman Avatar answered Mar 16 '23 05:03

A. Suliman