保持箱形图中的闪避位置传递给 plotly
Keeping dodge position in boxplot passed to plotly
我在 ggplot2
中有一个常规的 boxplot
:
# 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))
看起来像这样:
但是,当我创建一个对象并将其传递给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()
看起来像这样:
我尝试添加 position=position_dodge()
和 position=position_dodge2()
但其中 none 有效:
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()
如何保持第一个剧情的闪避位置?
按照建议 here,添加 layout(boxmode = "group")
library(plotly)
mtcars_boxplot %>%
ggplotly() %>%
layout(boxmode = "group")
我在 ggplot2
中有一个常规的 boxplot
:
# 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))
看起来像这样:
但是,当我创建一个对象并将其传递给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()
看起来像这样:
我尝试添加 position=position_dodge()
和 position=position_dodge2()
但其中 none 有效:
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()
如何保持第一个剧情的闪避位置?
按照建议 here,添加 layout(boxmode = "group")
library(plotly)
mtcars_boxplot %>%
ggplotly() %>%
layout(boxmode = "group")