I have come to notice that when converting a ggplot2
plot to an interactive plotly
plot using the ggplotly
function, strange things may occur.
I am plotting a "Punchcard plot", a nice way to present 4 dimensions of a data set:
df <- data.frame(cat1 = rep(c("a","b","c"), 3), cat2 = c(rep("A", 3),
rep("B", 3), rep("C", 3)), var1 = 1:9, var2 = 10:18)
ggplot(df, aes(x=cat1, y=cat2, size= var1, fill = var2)) +
geom_point(shape=21)
However, when I use ggplotly
to convert to interactive, plotly
only presents one of the legends:
p <- ggplot(df, aes(x=cat1, y=cat2, size= var1, fill = var2)) +
geom_point(shape=21)
ggplotly(p)
Why does
plotly
do this, how can I avoid this behavior?Seeing that I encounter more and more of these oddities - anyone has a link to somewhere I can read on how
ggplotly
works, in a way I can fix these issues myself in the future?
The second legend gets lost in during the conversion (or at least I couldn't find in the data). You can look at the result of
ggplotly
and modify everything from the raw data to the layout, e.g.gp[['x']][['layout']]
would contain all the layout variables passed fromggplotly
.A lot more lines of code but you have full control over all aspects of your graph.