Right now i got a graph in plotly made in R with the following rangeselector:
xaxis = list(
rangeselector = list(
buttons = list(
list(
count = 7,
label = "1W",
step = "day",
stepmode = "backward"),
list(
count = 1,
label = "1M",
step = "month",
stepmode = "backward"),
list(
count = 3,
label = "3M",
step = "month",
stepmode = "backward"),
list(
count = 6,
label = "6M",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1J",
step = "year",
stepmode = "backward"),
list(
count = 2,
label = "2J",
step = "year",
stepmode = "todate"))),
#rangeslider = list(type = "date")
FALSE
),
I'm trying to autoselect "1W" but i can't find the code to do that in the Plotly Documentation.
Is it possible to do that?
I don't think it's possible to choose default value, but if you add range = with the same date as the prefered range it will have the same result.
Here is the full code if the latest date is today. If not you can just do something like range = max(as.Date(df$date))-7, max(as.Date(df$date))) if your dataframe is called "df" and the dates are in column "Date".
xaxis = list(
range = c(Sys.Date()-7, Sys.Date()),
rangeselector = list(
buttons = list(
list(
count = 7,
label = "1W",
step = "day",
stepmode = "backward"),
list(
count = 1,
label = "1M",
step = "month",
stepmode = "backward"),
list(
count = 3,
label = "3M",
step = "month",
stepmode = "backward"),
list(
count = 6,
label = "6M",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1J",
step = "year",
stepmode = "backward"),
list(
count = 2,
label = "2J",
step = "year",
stepmode = "todate"))),
#rangeslider = list(type = "date")
FALSE
),
Related
I would like to somehow move the range slider toggle down further below the plot image of plot + range slider here, had to only show a portion of the image due to confidentiality. Is it possible to do this? I will paste my code for the plot below:
output$p <- renderPlotly({
plot1 <- plot_ly(x = x(), y = y(), name = as.character(input$select), type = 'scatter', mode = 'markers+lines', color = I("#696969")) %>%
add_lines(y = meaan(), name = paste("Mean =", meaan()[1]), color = I("black")) %>%
add_lines(y = ucl(), name = paste("UCL =", ucl()[1]), color = I("green")) %>%
add_lines(y = lcl(), name = paste("LCL =", lcl()[1]), color = I("green")) %>%
add_lines(y = usl(), name = paste("USL =", usl()[1]), color = I("red")) %>%
add_lines(y = lsl(), name = paste("LSL =", lsl()[1]), color = I("red")) %>%
add_trace(x=r5x(),y=r5y(),mode='markers', color = I("blue"), name = paste("R 5 =", length(r5())))%>%
add_trace(x=r4x(),y=r4y(),mode='markers', color = I("yellow"), name = paste("R 4 =", length(r4()))) %>%
add_trace(x=r1x(),y=r1y(),mode='markers', color = I("red"), name = paste("R 1 =", length(r1()))) %>%
layout(
title = paste("Test", input$select),
yaxis = list( showline = TRUE,
mirror = "ticks",
linecolor = toRGB("grey"),
linewidth = 1.85
),
xaxis = list( showline = TRUE,
mirror = "ticks",
linecolor = toRGB("grey"),
linewidth = 1.85,
rangeselector = list(
buttons = list(
list(
count = 3,
label = "3 mo",
step = "month",
stepmode = "backward"),
list(
count = 6,
label = "6 mo",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1 yr",
step = "year",
stepmode = "backward"),
list(
count = 1,
label = "YTD",
step = "year",
stepmode = "todate"),
list(step = "all"))),
rangeslider = list(type = "date", thickness=0.2)))
})
I'm looking for a way to automatically create buttons for different time periods in my dataset. As example from here shows it can be done using the following code:
library(plotly)
library(quantmod)
# Download some data
getSymbols(Symbols = c("AAPL", "MSFT"), from = '2018-01-01', to = '2020-01-01')
ds <- data.frame(Date = index(AAPL), AAPL[,6], MSFT[,6])
fig <- plot_ly(ds, x = ~Date)
fig <- fig %>% add_lines(y = ~AAPL.Adjusted, name = "Apple")
fig <- fig %>% add_lines(y = ~MSFT.Adjusted, name = "Microsoft")
fig <- fig %>% layout(
title = "Stock Prices",
xaxis = list(
rangeselector = list(
buttons = list(
list(
count = 3,
label = "3 mo",
step = "month",
stepmode = "backward"),
list(
count = 6,
label = "6 mo",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1 yr",
step = "year",
stepmode = "backward"),
list(
count = 1,
label = "YTD",
step = "year",
stepmode = "todate"),
list(step = "all"))),
rangeslider = list(type = "date")),
yaxis = list(title = "Price"))
fig
However, now I'd like to create a buttons to select the first year (2018) and second year (2019) separately. Both stepmode options seems to define ranges backwards from the last date only, so I'm not really sure how can I specify the range for 2018 year. Is it possible? If so, how can it be done?
You could use an updatemenu to select range:
library(plotly)
fig <- plot_ly(ds, x = ~Date)
fig <- fig %>% add_lines(y = ~AAPL.Adjusted, name = "Apple")
fig <- fig %>% add_lines(y = ~MSFT.Adjusted, name = "Microsoft")
rangeselector = list(
buttons = list(
list(
count = 3,
label = "3 mo",
step = "month",
stepmode = "backward"),
list(
count = 6,
label = "6 mo",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1 yr",
step = "year",
stepmode = "backward"),
list(
count = 1,
label = "YTD",
step = "year",
stepmode = "todate")))
xaxis = list(rangeselector = rangeselector,
rangeslider = list(type = "date"))
fig <- fig %>% layout(
title = "Stock Prices",
updatemenus = list(
list(
active = -1,
type = 'buttons',
buttons = list(
list(
label = '2018',
method = "relayout",
args = list(list(xaxis = list(range = as.POSIXct(c("2018-01-01","2018-12-31"), origin= "1970-1-1"),
rangeselector = rangeselector,
rangeslider = list(type = "date"))))),
list(
label = '2019',
method = "relayout",
args = list(list(xaxis = list(range = as.POSIXct(c("2019-01-01","2019-12-31"), origin= "1970-1-1"),
rangeselector = rangeselector,
rangeslider = list(type = "date"))))),
list(
label = 'All',
method = "relayout",
args = list(list(xaxis = list(range = as.POSIXct(c("2018-01-01","2019-12-31"), origin= "1970-1-1"),
rangeselector = rangeselector,
rangeslider = list(type = "date")))))
)
)
),
xaxis = xaxis,
yaxis = list(title = "Price"))
fig
https://plot.ly/r/custom-buttons/
Hello SO! First time posting here. I'm relatively new to R shiny and Plotly. I'm trying to use Plotly to essentially filter the data based upon the "update" button option that is in Plotly. The reference code is at the link above. Below is my code, I'm so close, the buttons will filter the data, but not correctly, so I feel like I'm missing something with the annotations in the args for Plotly.
I appreciate your feedback!
library(shiny)
library(shinydashboard)
library(plotly)
library(RODBC)
library(tidyverse)
library(lubridate)
library(htmlwidgets)
################
# ui
################
ui <- fluidRow(plotlyOutput("plot", width = '100%', height = '800px'))
################
# Server
################
server <- function(input, output, session) {
load(file = "/mnt/data/shinyAppsData/ltsang/B2.dat")
margin <- list(
l = 150,
r = 50,
b = 100,
t = 100,
pad = 4
)
build_annotations <- list(
x=B2$DATETIME,
y=B2$WL_BUILD)
release_annotations <- list(
x=B2$DATETIME,
y=B2$WL_RELEASE)
automation_annotations <- list(
x=B2$DATETIME,
y=B2$AUTOMATION_RCVD)
output$plot <- renderPlotly({B2 %>%
plot_ly(type = 'scatter', mode = 'lines') %>%
add_trace(x=~DATETIME, y = ~WL_BUILD, source="A", name = ~NODES, line = list(color = ~NODES, width = 4)) %>%
add_trace(x=~DATETIME, y = ~WL_RELEASE, source="B", name = ~NODES, line = list(color = ~NODES, width = 4, dash = 'dash')) %>%
add_trace(x=~DATETIME, y = ~AUTOMATION_RCVD, source= "C", name = ~NODES, line = list(color = ~NODES, width = 4, dash = 'dot')) %>%
layout(autosize = T, title = "NIGHT PRODUCTION", margin=margin,
xaxis = list(title = "TIME, DATE", tickformat = "%H - %m/%d/%Y (%a)", ticks= "inside", dtick=3600000, rangeslider = list(type = "date"),
rangeselector = list(
buttons = list(
list(
count = 1,
label = "1 Day",
step = "day",
stepmode = "backward"),
list(
count = 2,
label = "2 Days",
step = "day",
stepmode = "backward"),
list(
count = 3,
label = "3 Days",
step = "day",
stepmode = "backward"),
list(step = "all", label = "all")
)
)),
yaxis = list(title = "ACCESSION COUNT"),
updatemenus = list(
list(
x = -0.1,
y = 0.8,
showactive=TRUE,
active = -1,
bgcolor = "lightblue",
type = 'buttons',
buttons = list(
list(
label = "WL_BUILD",
method = "update",
args = list(list(visible = c(TRUE,FALSE,FALSE)),
list(title = "WORKLIST BUILD", source="A"))),
#annotations = list(build_annotations,release_annotations,automation_annotations )))),
list(
label = "WL_RELEASE",
method = "update",
args = list(list(visible = c(FALSE,TRUE,FALSE)),
list(title = "WORKLIST RELEASE", source="B"))),
#annotations = list(build_annotations,release_annotations,automation_annotations)))),
list(
label = "AUTOMATION_RCVD",
method = "update",
args = list(list(visible = c(FALSE,FALSE,TRUE)),
list(title = "AUTOMATION RECEIVED", source="C"))),
#annotations = list(build_annotations,release_annotations,automation_annotations)))),
list(
label = "VIEW ALL",
method = "update",
args = list(list(visible = c(TRUE,TRUE,TRUE)),
list(title = "AUTOMATION RECEIVED & WORKLIST BUILD/RELEASE")))
#annotations = list(release_annotations,build_annotations, automation_annotations))))
)
)
)
)}
)
#observeEvent(event_data("plotly_relayout"), {
#plotlyProxy("plot", session) %>%
#plotlyProxyInvoke("relayout")
#})
}
shinyApp(ui, server, enableBookmarking = "url")
We figured it out. Updated code below, lists are the bain of my existence.
server <- function(input, output, session) {
load(file = "/mnt/data/shinyAppsData/ltsang/B2.dat")
margin <- list(
l = 150,
r = 50,
b = 100,
t = 100,
pad = 4
)
chem<-length(c("COBAS7S", "COBAS7S2","COBAS7U","COBAS5S","COBASIA", "COBAS8LV","LIAISON","ARCTECT","CPL CBC","CPL UA","A1C","COAG1","RPR", "sg"))
release<-length(c("COBAS7S", "COBAS7S2","COBAS7U","COBAS5S","COBASIA", "COBAS8LV","LIAISON","ARCTECT","CPL CBC","CPL UA","A1C","COAG1","RPR"))
auto<-length(c("Automate3", "Automate5" ,"Automate6","BIM1", "BIM2", "BIM3","MUT2","P612-1", "P612-2","P612-3","P612-4", "P612-5","P612-6", "P612-7", "sg"))
output$plot <- renderPlotly({B2 %>%
plot_ly(type = 'scatter', mode = 'lines') %>%
add_lines(x=~DATETIME, y = ~WL_BUILD, name = ~NODES, line = list(color = ~NODES, width = 4)) %>%
add_lines(x=~DATETIME, y = ~WL_RELEASE, name = ~NODES, line = list(color = ~NODES, width = 4, dash = 'dash')) %>%
add_lines(x=~DATETIME, y = ~AUTOMATION_RCVD, name = ~NODES, line = list(color = ~NODES, width = 4, dash = 'dot')) %>%
layout(autosize = T, title = "NIGHT PRODUCTION", margin=margin,
xaxis = list(title = "TIME, DATE", tickformat = "%H - %m/%d/%Y (%a)", ticks= "inside", dtick=3600000, rangeslider = list(type = "date"),
rangeselector = list(
buttons = list(
list(
count = 1,
label = "1 Day",
step = "day",
stepmode = "backward"),
list(
count = 2,
label = "2 Days",
step = "day",
stepmode = "backward"),
list(
count = 3,
label = "3 Days",
step = "day",
stepmode = "backward"),
list(step = "all", label = "all")
)
)),
yaxis = list(title = "ACCESSION COUNT"),
updatemenus = list(
list(
x = -0.1,
y = 0.8,
showactive=TRUE,
active = -1,
bgcolor = "lightblue",
type = 'buttons',
buttons = list(
list(
label = "WL_BUILD",
method = "update",
args = list(list(visible = c(rep_len(TRUE, chem), rep_len(FALSE, release), rep_len(FALSE, auto))),
list(title = "WORKLIST BUILD", key="A"))),
list(
label = "WL_RELEASE",
method = "update",
args = list(list(visible = c(rep_len(FALSE, chem), rep_len(TRUE, release), rep_len(FALSE, auto))),
list(title = "WORKLIST RELEASE", key="B"))),
list(
label = "AUTOMATION_RCVD",
method = "update",
args = list(list(visible = c(rep_len(FALSE, chem), rep_len(FALSE, release), rep_len(TRUE, auto))),
list(title = "AUTOMATION RECEIVED", key="C"))),
list(
label = "VIEW ALL",
method = "update",
args = list(list(visible = c(rep_len(TRUE, chem), rep_len(TRUE, release), rep_len(TRUE, auto))),
list(title = "AUTOMATION RECEIVED & WORKLIST BUILD/RELEASE")))
)
)
)
)}
)
}
shinyApp(ui, server, enableBookmarking = "url")
Below I display 2 plotly line charts with Range Slider and period Selectors. I would like to know if there is a way to have a common period Selector for both charts instead of two which means that when the user would choose -for example 1yr- both charts would adapt at the same time.
library(plotly)
library(quantmod)
# Download some data
getSymbols(Symbols = c("AAPL", "MSFT"))
ds <- data.frame(Date = index(AAPL), AAPL[,6], MSFT[,6])
p <- plot_ly(ds, x = ~Date) %>%
add_lines(y = ~AAPL.Adjusted, name = "Apple") %>%
add_lines(y = ~MSFT.Adjusted, name = "Microsoft") %>%
layout(
title = "Stock Prices",
xaxis = list(
rangeselector = list(
buttons = list(
list(
count = 3,
label = "3 mo",
step = "month",
stepmode = "backward"),
list(
count = 6,
label = "6 mo",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1 yr",
step = "year",
stepmode = "backward"),
list(
count = 1,
label = "YTD",
step = "year",
stepmode = "todate"),
list(step = "all"))),
rangeslider = list(type = "date")),
yaxis = list(title = "Price"))
p
p1 <- plot_ly(ds, x = ~Date) %>%
add_lines(y = ~AAPL.Adjusted, name = "Apple") %>%
add_lines(y = ~MSFT.Adjusted, name = "Microsoft") %>%
layout(
title = "Stock Prices",
xaxis = list(
rangeselector = list(
buttons = list(
list(
count = 3,
label = "3 mo",
step = "month",
stepmode = "backward"),
list(
count = 6,
label = "6 mo",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1 yr",
step = "year",
stepmode = "backward"),
list(
count = 1,
label = "YTD",
step = "year",
stepmode = "todate"),
list(step = "all"))),
rangeslider = list(type = "date")),
yaxis = list(title = "Price"))
p1
For example, reproduce by running:
library(plotly)
library(quantmod)
setDefaults(getSymbols,src='google')
getSymbols('AAPL',from="2010-01-01",to=Sys.Date())
df <- data.frame(Date = index(AAPL), coredata(AAPL))
colnames(df)
p <- df %>%
plot_ly(x = ~Date, type="candlestick",
open = ~AAPL.Open, close = ~AAPL.Close,
high = ~AAPL.High, low = ~AAPL.Low) %>%
layout(title = "Basic Candlestick Chart")
p
Now this plot does not have a vertical slider/range selector like plotly scatterplots do (say to zoom on a price and time range rather than just a time range as now). How to add one to it?
This can be done by changing xaxis in layout. Using piece of code from example:
rangeselectorlist = list(
x = 0, y = 0.9,
bgcolor = "#0099cc",
font = list(color = "white"),
buttons = list(
list(count = 1, label = "reset", step = "all"),
list(count = 1, label = "1yr", step = "year", stepmode = "backward"),
list(count = 3, label = "3 mo", step = "month", stepmode = "backward"),
list(count = 1, label = "1 mo", step = "month", stepmode = "backward"),
list(step = "all")
)
)
and adding to
p <- df %>%
plot_ly(x = ~Date, type="candlestick",
open = ~AAPL.Open, close = ~AAPL.Close,
high = ~AAPL.High, low = ~AAPL.Low) %>%
layout(title = "Basic Candlestick Chart",
xaxis = list(rangeslider = list(visible = F),
rangeselector = rangeselectorlist) )
p
adds vertical slider/range selector.