Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slider for Plotly R

I have plotted two following graphs using ggplot2, using the following codes.

p <- ggplot() +
  geom_line(data = campaigns_profit_3months, aes(y = clients_3monthsBefore, x = c(1:41), group = 1, color = "Clients 3 Months Before"), 
            size = 1 ) +
  geom_line(data = campaigns_profit_3months, aes(y = clients_3monthsAfter, x = c(1:41), group = 1, color = "Clients 3 Months After"), 
            size = 1 )  + 
  xlab('Campaigns') + ylab('Clients') + ggtitle('Clients 3 months before and after the campaigns') +
  scale_x_continuous(breaks = seq(1,41,1)) + scale_y_continuous(limits=c(0, 200)) +
  theme(legend.title=element_blank())

q <- ggplot() +
  geom_line(data = campaigns_profit_6months, aes(y = revenue_6monthsBefore, x = c(1:41), group = 1, color = "Revenue 6 Months Before"), 
            size = 1 ) +
  geom_line(data = campaigns_profit_6months, aes(y = revenue_6monthsAfter, x = c(1:41), group = 1, color = "Revenue 6 Months After"), 
            size = 1 )  + 
  xlab('Campaigns') + ylab('Revenue (USD)') + ggtitle('Revenue 6 months before and after the campaigns') +
  scale_x_continuous(breaks = seq(1,41,1)) + scale_y_continuous(limits=c(0, 200000)) +
  theme(legend.title=element_blank())

plot(p)
plot(q)

enter image description here

enter image description here

Is there a way to add a slider bar or buttons(don't know if that's the right terminology) using Plotly to create two options 3 months revenue and 6 months revenue, where selecting the first option would display the first plot and selecting the second option would display the second?

like image 737
Chirag Srinivas Avatar asked Aug 15 '26 02:08

Chirag Srinivas


1 Answers

Use the frame aesthetic. Here is an example.

library(plotly)

dat <- iris
dat$months <- c(3,6)

gg <- 
  ggplot(dat, aes(Sepal.Length, Sepal.Width, color = Species, frame = months)) + 
  geom_point()

ggplotly(gg)

enter image description here

like image 147
Stéphane Laurent Avatar answered Aug 16 '26 19:08

Stéphane Laurent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!