填充交互不适用于 ggplot2 和 plotly

fill Interaction not working with ggplot2 and plotly

我想在我的绘图图形的填充参数中添加交互,例如回答了 here.

然而,虽然ggplot图正确,但plotly不正确。我正在使用以下版本并在下面显示 MWE:

ggplot2: 版本 2.0.0.9000, plotly: 版本 2.0.19

library(plotly)
g <- ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = interaction(factor(cyl),carb))) + geom_boxplot()
(gg <- ggplotly(g))

知道上面 g 和 gg 不同的原因吗?

不是一个完整的解决方案。包括与您的 x 字词的互动:

# use lex.order to obtain correct ordered levels
a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T), y = mpg,fill = interaction(cyl, carb, lex.order = T))) + 
     geom_boxplot()
# check the plot
a
# plotly
ggplotly(a)

实际上有一个替代方案可以满足您的需求。

mtcars$intec <- interaction(factor(cyl),carb)

mtcars %>%
  plot_ly(x = cyl, y = mpg, type = "box", color = as.factor(intec), fill=as.factor(intec)) %>%
  layout(boxmode = "group")