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

ggplotly() does not reflect axis/facet position changes in ggplot.

Open goreaditya opened this issue 7 years ago • 5 comments

I have changed the position of facet strips and axis in ggplot but those changes are not translated by ggplotly.

Example code:

dia = ggplot(diamonds[diamonds$cut %in% c("Fair", "Good"),], aes(x = cut)) + 
geom_bar(stat = "identity",aes(y=depth,fill = cut)) + 
facet_wrap(~color, nrow=1,strip.position = "bottom")+scale_x_discrete(position = "top")

The ggplot result is: rplot

The result of ggplotly(dia) is: rplot01

Session info --------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.3 (2017-03-06)
 system   x86_64, mingw32             
 ui       RStudio (1.0.136)           
 language (EN)                        
 collate  English_United States.1252  
 tz       America/New_York            
 date     2017-04-13                  

Packages ------------------------------------------------------------------------------------------------
 package     * version date       source        
 assertthat    0.1     2013-12-06 CRAN (R 3.3.3)
 base64enc     0.1-3   2015-07-28 CRAN (R 3.3.2)
 colorspace    1.3-2   2016-12-14 CRAN (R 3.3.3)
 DBI           0.6-1   2017-04-01 CRAN (R 3.3.3)
 devtools      1.12.0  2016-06-24 CRAN (R 3.3.3)
 digest        0.6.12  2017-01-27 CRAN (R 3.3.3)
 dplyr         0.5.0   2016-06-24 CRAN (R 3.3.3)
 ggplot2     * 2.2.1   2016-12-30 CRAN (R 3.3.3)
 gtable        0.2.0   2016-02-26 CRAN (R 3.3.3)
 htmltools     0.3.5   2016-03-21 CRAN (R 3.3.3)
 htmlwidgets   0.8     2016-11-09 CRAN (R 3.3.3)
 httr          1.2.1   2016-07-03 CRAN (R 3.3.3)
 jsonlite      1.4     2017-04-08 CRAN (R 3.3.3)
 labeling      0.3     2014-08-23 CRAN (R 3.3.2)
 lazyeval      0.2.0   2016-06-12 CRAN (R 3.3.3)
 magrittr      1.5     2014-11-22 CRAN (R 3.3.3)
 memoise       1.0.0   2016-01-29 CRAN (R 3.3.3)
 munsell       0.4.3   2016-02-13 CRAN (R 3.3.3)
 plotly      * 4.5.6   2016-11-12 CRAN (R 3.3.3)
 plyr          1.8.4   2016-06-08 CRAN (R 3.3.3)
 purrr         0.2.2   2016-06-18 CRAN (R 3.3.3)
 R6            2.2.0   2016-10-05 CRAN (R 3.3.3)
 Rcpp          0.12.10 2017-03-19 CRAN (R 3.3.3)
 scales        0.4.1   2016-11-09 CRAN (R 3.3.3)
 tibble        1.3.0   2017-04-01 CRAN (R 3.3.3)
 tidyr         0.6.1   2017-01-10 CRAN (R 3.3.3)
 viridisLite   0.2.0   2017-03-24 CRAN (R 3.3.3)
 withr         1.0.2   2016-06-20 CRAN (R 3.3.3)
 yaml          2.1.14  2016-11-12 CRAN (R 3.3.3)

goreaditya avatar Apr 13 '17 15:04 goreaditya

Would like to note a similar issue, where I included facet_wrap(~name, ncol=1, strip.position = "left") in a ggplot and the resulting strips were at the top. (ggplotly, without the plotly wrapping, rendered this properly.)

スクリーンショット 2019-08-08 午後5 54 38

kojisposts avatar Aug 08 '19 22:08 kojisposts

Just stumbled upon this same problem. Is there any part of the plotly code we can take a look at?

bvaldebenitom avatar Feb 22 '20 14:02 bvaldebenitom

This is still an issue as far as I can see. Did anyone come up with a workaround?

mattjmeier avatar Nov 03 '21 20:11 mattjmeier

Hi @mattjmeier,

Unfortunately I haven't. Best I could was to switch some things in the original ggplot in order to "skip" the issue entirely.

bvaldebenitom avatar Nov 05 '21 12:11 bvaldebenitom

I recently answered an SO question that asked about the ignored strip position. I came up with a workaround after generating the plot with ggplotly. The questioner wanted the strips at the bottom. When I looked at the plotly object, I found that a lot of the settings were preset to place the strips at the bottom. I needed to make one change to annotations (where the text in the strips is coming from) and one change to shapes (where the background for the strips comes from). After that I adjust the ranges on the y-axis, to account for the strips, and the margin, to get rid of the empty white space on the top (where the strips were originally).

You can see the Q&A here.

Obviously, these changes will have to be modified depending on what you have in your plot and where you want to place the strips.

library(tidyverse)
library(plotly)

# plot from Stack Overflow Question
p = iris %>%
  ggplot(aes(x = Species , y = Sepal.Width)) +
  geom_boxplot() +
  facet_wrap(~ Species , strip.position = "bottom")
p

pp <- ggplotly(p) # lacking the strip position set in ggplot

# adjust the strip positions
invisible(
  lapply(1:length(pp$x$layout$shapes),
         function(i) {       # if a strip shape (using the strip color)
           if(isTRUE(pp$x$layout$shapes[[i]]$fillcolor == "rgba(217,217,217,1)")) {
             pp$x$layout$shapes[[i]]$yanchor <<- 0    # anchor at the bottom
           }                 # if there are at least i annotations (there are less annotations than shapes)
           if(i <= length(pp$x$layout$annotations)) { # if the text is a strip label
             if(any(pp$x$layout$annotations[[i]]$text %in% unique(iris$Species))) {
               pp$x$layout$annotations[[i]]$y <<- 0.01   # move text to the bottom
             }
           }
         })
)
pp %>% layout(yaxis = list(range = c(1.5, 4.5)), # add space for strip position
              yaxis2 = list(range = c(1.5, 4.5)),
              yaxis3 = list(range = c(1.5, 4.5)),
              margin = list(t = 10, r = 10, b = 43, l = 43)) # remove excess on top

image

fraupflaume avatar Sep 07 '22 03:09 fraupflaume