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

subplot missing Y labels when plots are faced

Open Sanrrone opened this issue 6 years ago • 3 comments

Dears, I'm working with the subplot function with two (or more) ggplotly objects, but when I try to put two different charts, the final subplot output give me the two charts without the labels that I can put in the previous ones.

for example:

library(plotly)

random dataframe 1

df1<-data.frame(x1=as.factor(rep(seq(1:5),3)),y1=sample(seq(0:100000),30),z1=c("class1","class2","class3"))
df1

random dataframe2 (with same x1 axis)

df2<-data.frame(x2=df1$x1,y2=sample(seq(0:5),30,replace = T), z2=c("a","b","c"))
df2

chart1 as boxplot and personalized ylab

p1<-ggplotly(ggplot(df1,aes(x1,y1)) + 
               geom_boxplot() +
               ylab("test ylab1") +
               facet_grid(z1~.))
p1

screen shot 2018-11-26 at 7 52 53 pm

chart1 as boxplot and personalized ylab

p2<-ggplotly(ggplot(df2,aes(x2,y2)) + 
               geom_bar(,stat = "identity") + 
               ylab("test ylab2") + 
               facet_grid(z2~.,margins = TRUE))
p2

screen shot 2018-11-26 at 7 53 55 pm

but applying subplot the ylabels are missing

subplot(plotlist,nrows = 2, shareY = TRUE)

screen shot 2018-11-26 at 7 54 24 pm

I notice the option shareY=TRUE works well when the ggplot objects doesn't have (in my case) facet_grid

I tried some alternatives using %>% layouts but is not the solution I'm looking for (even more if we consider this for N plots), so my questions are:

  • is there a way to recover o preserve the ylabs from original ggplot objects when they are faced?
  • or at least create new ones for each subplot?
  • it is a bug?

sessionInfo()

R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] bindrcpp_0.2.2        maps_3.3.0            RPostgreSQL_0.6-2     rmongodb_1.8.0        cowplot_0.9.2        
 [6] scales_0.5.0          reshape2_1.4.3        RMySQL_0.10.15        DBI_1.0.0             dplyr_0.7.8          
[11] shinycssloaders_0.2.0 shiny_1.2.0           plotly_4.8.0          ggplot2_3.1.0        

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0          pillar_1.2.2        compiler_3.5.0      later_0.7.5         plyr_1.8.4         
 [6] bindr_0.1.1         tools_3.5.0         digest_0.6.15       jsonlite_1.5        tibble_1.4.2       
[11] gtable_0.2.0        viridisLite_0.3.0   pkgconfig_2.0.1     rlang_0.3.0.1       rstudioapi_0.7     
[16] crosstalk_1.0.0     yaml_2.1.19         stringr_1.3.1       withr_2.1.2         httr_1.3.1         
[21] htmlwidgets_1.2     grid_3.5.0          tidyselect_0.2.4    glue_1.2.0          data.table_1.10.4-3
[26] R6_2.2.2            purrr_0.2.4         tidyr_0.8.0         magrittr_1.5        promises_1.0.1     
[31] htmltools_0.3.6     assertthat_0.2.0    mime_0.5            colorspace_1.3-2    xtable_1.8-2       
[36] httpuv_1.4.5        labeling_0.3        stringi_1.2.3       lazyeval_0.2.1      munsell_0.4.3      
[41] crayon_1.3.4 

thanks you very much in advance!, Sandro

Sanrrone avatar Nov 26 '18 23:11 Sanrrone

Hi All, sorry I am late to this conversation, but I am facing with the same issue (ylabel disappears after applying ggplotly in a subplot). Has there been any progress on this issue?

omidmsvi avatar Aug 16 '21 02:08 omidmsvi

I don't know really but it was recognized as a bug. If it was not solved in the newer versions there is no solution at all.

Sanrrone avatar Aug 16 '21 17:08 Sanrrone

I only have a partial solution to this, essentially ggplot2::facet_grid() does not seem compatible with plotly::subplot(). You would need to create independent plots with ggplot2, and then apply the subplot() from plotly after the plots are converted using ggplotly(). Then the solution is to specify titleY = TRUE in subplot() to preserve the axis labels from your initial plots. The drawback is that you will get one y axis label per plot, instead of two nested levels of labels.

library(ggplot2)
library(plotly)

df1 <- data.frame(x1 = as.factor(rep(seq(1:5), 3)), 
                  y1 = sample(seq(0:100000), 30), 
                  z1 = c("class1", "class2", "class3"))

df2 <- data.frame(x2 = df1$x1, 
                  y2 = sample(seq(0:5), 30, replace = T),
                  z2 = c("a", "b", "c"))

p1_c1 <- ggplotly(
  ggplot(df1[df1$z1=="class1",], aes(x1,y1)) +
    geom_boxplot() +
    ylab("ylab1 - class 1")
)

p1_c2 <- ggplotly(
  ggplot(df1[df1$z1=="class2",], aes(x1,y1)) +
    geom_boxplot() +
    ylab("ylab1 - class 2")
)

p1_c3 <- ggplotly(
  ggplot(df1[df1$z1=="class3",], aes(x1,y1)) +
    geom_boxplot() +
    ylab("ylab1 - class 3")
)

p2_a <- ggplotly(
  ggplot(df2[df2$z2=="a",], aes(x2,y2)) +
    geom_bar(stat = "identity") +
    ylab("ylab2 - a")
)

p2_b <- ggplotly(
  ggplot(df2[df2$z2=="b",], aes(x2,y2)) +
    geom_bar(stat = "identity") +
    ylab("ylab2 - b")
)

p2_c <- ggplotly(
  ggplot(df2[df2$z2=="c",], aes(x2,y2)) +
    geom_bar(stat = "identity") +
    ylab("ylab2 - c")
)

p2_all <- ggplotly(
  ggplot(df2, aes(x2,y2)) +
    geom_bar(stat = "identity") +
    ylab("ylab2 - (all)")
)

subplot(p1_c1, p1_c2, p1_c3, p2_a, p2_b, p2_c, p2_all, nrows = 7, titleY = TRUE, shareX = TRUE)

image

Answer helped by: https://stackoverflow.com/questions/75132414/axis-labels-lost-in-plotlysubplot

Flocondeneige55 avatar Sep 11 '23 12:09 Flocondeneige55