Reputation: 666
I'm trying to understand why plotly
objects which are converted from ggplot2
objects using the ggplotly
function do not dynamically rescale the grid when using plotly's zoom feature on the html widget.
I would like to find a solution to this without the need to recode all plots as native plotly
objects.
Am I missing something, or could it be that this functionality is just not supported yet in the plotly
package?
Here is some sample code illustrating the problem (before executing, you need to set the your.path
variable to the directory you want the htmlwidget to be saved in):
library(ggplot2)
library(plotly)
library(htmlwidgets)
ggp <- ggplot(data=mtcars, aes(x=hp, y=mpg)) + geom_point() + theme_bw()
ply.ggp <- ggplotly(ggp)
saveWidget(ply.ggp, paste(your.path, "mtcarsGGPLOT.html", sep=""))
On this html widget when you zoom in, the grid scale remains static (e.g. the x grid lines on 100, 200, 300 are the only one you can see, even if you zoom in. There are no new grid lines visible e.g. at 25, 50, 75, etc.)
Compare this to the widget when creating the plot directly in plotly with this sample code:
ply <- plot_ly(data=mtcars, x=mtcars$hp, y=mtcars$mpg)
saveWidget(ply, paste(your.path, "mtcarsNATIVE.html", sep=""))
Here when you zoom in a region of the plot, a new dynamic grid is visible.
EDIT: Could it be that the observation occurs due to the htmlwidgets
package rather than plotly
or ggplot2
. But then again, why would the native plotly graphic render correctly when using htmlwidgets while the ggplot converted one doesn't..?
Upvotes: 2
Views: 845
Reputation: 418
I've also spent time trying to figure this out and it's actually quite simple.
Set dynamicTicks equal to TRUE within ggplotly.
ply.ggp <- ggplotly(ggp, dynamicTicks = TRUE)
I just tried creating the widget and it worked for me.
Upvotes: 4