Plotly R 的滑块 [英] Slider for Plotly R

查看:69
本文介绍了Plotly R 的滑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

有没有办法使用 Plotly 创建两个选项来添加滑动条或按钮(不知道这是否是正确的术语)3 个月收入6 个月收入,其中选择第一个选项会显示第一个图,选择第二个选项会显示第二个?

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?

推荐答案

使用 frame 美学.这是一个例子.

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)

这篇关于Plotly R 的滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆