箱形图中保持躲避的位置传递给Plotly

问题描述 投票:0回答:1

我在boxplot中有一个常规的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))

看起来像这样:enter image description here

但是,当我创建一个对象并将其传递给plotly时,我失去了闪避位置:

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() 

看起来像这样:enter image description here

我尝试添加position=position_dodge()position=position_dodge2(),但没有一个起作用:

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() 

如何保持闪避位置,如第一个情节一样?

r ggplot2 plotly boxplot
1个回答
1
投票

按照建议的here,添加layout(boxmode = "group")

library(plotly)
mtcars_boxplot %>%
  ggplotly() %>%
  layout(boxmode = "group")
© www.soinside.com 2019 - 2024. All rights reserved.