Nick
Nick

Reputation: 3374

fill Interaction not working with ggplot2 and plotly

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?

Upvotes: 1

Views: 1659

Answers (2)

MLavoie
MLavoie

Reputation: 9836

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

Upvotes: 1

Roman
Roman

Reputation: 17648

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)

Upvotes: 2

Related Questions