gpt4 book ai didi

r - 在 boxplot 中保持躲避位置传递给 plotly

转载 作者:行者123 更新时间:2023-12-03 20:27:18 25 4
gpt4 key购买 nike

我有一个普通的 boxplotggplot2 :

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

我应该怎么做才能像第一个 plotly 一样保持闪避位置?

最佳答案

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

library(plotly)
mtcars_boxplot %>%
ggplotly() %>%
layout(boxmode = "group")

关于r - 在 boxplot 中保持躲避位置传递给 plotly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58586314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com