为绘图图表生成下拉菜单 [英] Generating Dropdown menu for Plotly charts
问题描述
这是我的10只股票的波幅代码:
This is my code for stock volatility for 10 stocks:
##------------producing stock volatility plot---------##
p1 = ggplot(Closed_Price_Return, aes(Date, R.BPCL)) + geom_line(color = "blue") +
theme_bw()
p2 = ggplot(Closed_Price_Return, aes(Date, R.TCS)) + geom_line(color = "green") +
theme_bw()
p3 = ggplot(Closed_Price_Return, aes(Date, R.CIPLA)) + geom_line(color = "red") +
theme_bw()
p4 = ggplot(Closed_Price_Return, aes(Date, R.EICHER)) + geom_line(color = "pink") +
theme_bw()
p5 = ggplot(Closed_Price_Return, aes(Date, R.INFY)) + geom_line(color = "yellow") +
theme_bw()
p6 = ggplot(Closed_Price_Return, aes(Date, R.LT)) + geom_line(color = "purple") +
theme_bw()
p7 = ggplot(Closed_Price_Return, aes(Date, R.MARUTI)) + geom_line(color = "orange") +
theme_bw()
p8 = ggplot(Closed_Price_Return, aes(Date, R.RELIANCE)) + geom_line(color = "#7fffd4") +
theme_bw()
p9 = ggplot(Closed_Price_Return, aes(Date, R.SUNPHARMA)) + geom_line(color = "#ff1493") +
theme_bw()
p10 = ggplot(Closed_Price_Return, aes(Date, R.YESBANK)) + geom_line(color = "#ff7256")+
theme_bw()
##------------Converting the ggplots into plotly objects-------##
p1 = ggplotly(p1)
p2 = ggplotly(p2)
p3 = ggplotly(p3)
p4 = ggplotly(p4)
p5 = ggplotly(p5)
p6 = ggplotly(p6)
p7 = ggplotly(p7)
p8 = ggplotly(p8)
p9 = ggplotly(p9)
p10 = ggplotly(p10)
现在,我想为所有这些图表生成一个下拉菜单按钮.请 告诉我如何继续进行按钮代码
Now i want to generate a dropdown menu button for all these charts. Please tell me how to proceed for button code in plotly
推荐答案
一种方法是(例如 plotly页面),在所有迹线都不可见的情况下创建plotly,然后添加一个下拉菜单以切换可见迹线:
One way is (like the example on plotly page), create the plotly with all your traces invisible, then add a dropdown that switches the visible trace:
plot_ly(mtcars, x = ~gear) %>%
add_trace(y = ~cyl, name = "cyl", visible = F, color=I("blue")) %>%
add_trace(y = ~hp, name = "hp", visible = F, color=I("green")) %>%
add_trace(y = ~gear, name = "gears", visible = F, color=I("red")) %>%
layout(
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
buttons = list(
list(method = "restyle",
args = list("visible", list(TRUE, FALSE, FALSE)),
label = "cyl"),
list(method = "restyle",
args = list("visible", list(FALSE, TRUE, FALSE)),
label = "hp"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE)),
label = "gear")))
)
)
在您的情况下,只需在add_trace
中定义一个不同的y
. TRUE
/FALSE
列表必须与下拉列表中的跟踪记录一样长.
In your case, just define a different y
in the add_trace
s. The TRUE
/FALSE
lists must be as long as you have traces in your Dropdown.
这对于一个闪亮的小型应用程序也非常简单:
This also works pretty straightforward with a small shiny app:
library(shiny)
library(plotly)
p1 <- plot_ly(mtcars, x=~cyl, y=~gear)
p2 <- plot_ly(mtcars, x=~hp, y=~am)
ui <-shinyUI(fluidPage(selectInput("selectPlot", "Choose desired plot", choices=paste0("p", 1:2)), plotlyOutput("plot")))
server <- shinyServer(function(input,output){
output$plot <- renderPlotly({
return(get(input$selectPlot)) # get("p1") from the workspace returns the object p1, which is a plotly object
})
})
shinyApp(ui,server)
这篇关于为绘图图表生成下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!