Plotly R 的滑块

Chi*_*vas 3 r ggplot2 plotly ggplotly r-plotly

我使用 ggplot2 使用以下代码绘制了以下两个图表。

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在此输入图像描述

有没有办法使用 Plotly 添加滑块或按钮(不知道这是否是正确的术语)来创建两个选项 3 months revenue6 months revenue,其中选择第一个选项将显示第一个图,选择第二个选项将显示第二个图?

Sté*_*ent 6

使用frame审美。这是一个例子。

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述