[SOLVED] range slider on ggarrange object

Issue

This Content is from Stack Overflow. Question asked by gaut

I need to have a rangeslider object present on a group of charts currently made with egg::ggarrange, but I get the following errors:

library(ggplot2)
dat <- data.frame(a = 1:1000, b = 1001:2000/100)

p0 <- ggplot(data = dat, aes(x = a))+
  geom_line(aes(y = b, color = 'this chart has a legend'))
p1 <- ggplot(data = dat, aes(x = a))+
  geom_line(aes(y = b))

plotly::ggplotly(p0, tooltip = c("text"), dynamicTicks = T) %>%
  rangeslider(borderwidth = 1)
# works fine

pp <- ggpubr::ggarrange(p0, p1, p1, ncol = 1) # not aligned vertically 
plotly::ggplotly(pp, tooltip = c("text"), dynamicTicks = T) %>%
  rangeslider(borderwidth = 1)
# geom_GeomDrawGrob() has yet to be implemented in plotly.

pp <- egg::ggarrange(p0, p1, p1, ncol = 1) # aligned vertically
plotly::ggplotly(pp, tooltip = c("text"), dynamicTicks = T) %>%
  rangeslider(borderwidth = 1)
# no applicable method for 'ggplotly' applied to an object of class "c('egg', 'gtable', 'gTree', 'grob', 'gDesc')"

The reason I am using egg is that it keeps the x-axis alignment even if one chart has a legend, see the difference below.

Is it possible to have a slider on such grouped charts, by keeping the vertical alignment?

  • needed slider (but works only on one ggplot chart):

enter image description here

  • grouped with ggpubr: doesn’t align vertically, no slider

ggpubr

  • with egg, aligns vertically, no slider.

egg



Solution

I managed with

  library(plotly)
  
  subshared <- subplot(p0, p1, p2, p3, nrows = 4, shareX=T)
  subshared %>% layout(xaxis = list(rangeslider = list(type = "date")))

enter image description here


This Question was asked in StackOverflow by gaut and Answered by gaut It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?