1

I want to add interaction to the fill argument of my plotly figure, as e.g. answered here.

However, although the ggplot figure comes out correctly, the plotly does not. I am using the following versions and show a MWE below:

ggplot2: version 2.0.0.9000, plotly: version 2.0.19

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

Any ideas why g and gg differs above?

Community
  • 1
  • 1
Nick
  • 3,262
  • 30
  • 44

2 Answers2

2

Not a complete solution. Include the interaction to your x term:

# 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)
Roman
  • 17,008
  • 3
  • 36
  • 49
  • any ideas on fixing [this](http://stackoverflow.com/questions/34493789/how-to-change-the-position-of-plotly-figures-using-ggplotly) problem? – Nick Dec 28 '15 at 12:46
1

actually there is an alternative that will give you exactly what you want.

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

enter image description here

MLavoie
  • 9,671
  • 41
  • 36
  • 56