plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

ggplotly breaks custom, log transformed, axis labels

Open bracharz opened this issue 3 years ago • 2 comments

Unfortunately this does not show an error, only a warning. A better behavior would probably be returning an error when passing an expression.

See below for an example where the x axis has been log transformed.

plot_gg = ggplot(iris,aes(Sepal.Length,Sepal.Width,col=Species))+
    geom_point()+ 
    scale_x_continuous(trans = scales::log2_trans(),
                       breaks = scales::trans_breaks("log2", function(x) 2^x),
                       labels = scales::trans_format("log2", scales::math_format(2^.x)))
plot_gg

image

plot_ly=plotly::ggplotly(plot_gg)
plot_ly

image

Warning message:
In is.na(ticktext) :
  is.na() applied to non-(list or vector) of type 'expression'

bracharz avatar Feb 22 '22 08:02 bracharz

This also seems to happen with scale::label_log() and other exponent expressions. ggplotly converts them to jus the exponent, see reprex example below.

# the following needs reticulate and kaleido (python library) to work
# install.packages("reticulate")
# reticulate::install_miniconda()
# reticulate::conda_install('r-reticulate', 'python-kaleido')
# reticulate::conda_install('r-reticulate', 'plotly', channel = 'plotly')
reticulate::use_miniconda('r-reticulate')

library(ggplot2)
p_gg <- ggplot(cars) + aes(speed, dist) + geom_point() + 
  scale_y_log10(labels = scales::label_log())
p_gg


# plotly rendering
p_plotly <- plotly::ggplotly(p_gg)
#> Warning in is.na(ticktext): is.na() applied to non-(list or vector) of type
#> 'expression'
plotly::save_image(p_plotly, "screenshot.png")
knitr::include_graphics("screenshot.png")

Created on 2023-11-05 with reprex v2.0.2

sebkopf avatar Nov 06 '23 04:11 sebkopf

a nvm, looks like this is the general issue with translating expressions: https://github.com/plotly/plotly.R/issues/2006

sebkopf avatar Nov 06 '23 04:11 sebkopf