How to Change Position of RangeSlider in R Plotly? - r

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)))
})

Related

How to create rangeselectors buttons for different periods on the plot - plotly

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

Shiny and Plotly update buttons are not updating with correct data

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")

Two timeseries line charts with common period selector

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

Second yaxis plotly does not apply

Hey I am using the package plotly to create diagrams and i am trying to set up 2 different yaxis, but it does not work. Here is my code:
Schuldenquote_Argentinien <- c(80.3,70.8,62.1,53.8,55.4,43.5,38.9,40.4,43.5,44.7,52.6,53.1,57.1)
Schuldenquote_Brasilien <- c(68.7,65.9,63.8,61.9,65,63.1,61.2,62.2,60.2,62.3,72.6,78.3,84.1)
Schuldenquote_Chile <- c(7,5,3.9,4.9,5.8,8.6,11.1,11.9,12.7,15,17.3,21,23.5)
Schuldenquote_Mexico <- c(38.5,37.4,37.2,42.5,43.7,42,42.9,42.7,45.9,48.9,52.8,56.8,54)
Schuldenquote_Peru <- c(40,34.8,31.9,27.9,28.4,25.4,23,21.2,20,20.7,24,24.5,25.4)
Schulden_Argentinien <- c(159783,0,0,0,0,184396,0,0,0,0,337657,0,367360)*1000000
Schulden_Brasilien <- c(610776,0,0,0,0,1391222,0,0,0,0,1306199,0,1726603)*1000000
Schulden_Chile <- c(8.6,0,0,0,0,18.67,0,0,0,0,42.131,0,65.395)*1000000000
Schulden_Mexico <- c(337621,0,0,0,0,443485,0,0,0,0,618382,0,625687)*1000000
Schulden_Peru <- c(29923,0,0,0,0,37706,0,0,0,0,46037,0,54448)*1000000
Einwohner_Argentinien <- c(39.1455,0,0,0,0,41.2239,0,0,0,0,43.4178,0,44.271)*1000000
Einwohner_Brasilien <- c(186.1974,0,0,0,0,196.7963,0,0,0,0,205.9621,0,209.2883)*1000000
Einwohner_Chile <- c(16.1471,0,0,0,0,16.9934,0,0,0,0,17.7627,0,18.0547)*1000000
Einwohner_Mexico <- c(108.4722,0,0,0,0,117.3189,0,0,0,0,125.8909,0,129.1633)*1000000
Einwohner_Peru <- c(27.6104,0,0,0,0,29.3736,0,0,0,0,31.3767,0,32.1655)*1000000
Schuldenkopf_Argentinien <- Schulden_Argentinien/Einwohner_Argentinien
Schuldenkopf_Brasilien <- Schulden_Brasilien/Einwohner_Brasilien
Schuldenkopf_Chile <- Schulden_Chile/Einwohner_Chile
Schuldenkopf_Mexico <- Schulden_Mexico/Einwohner_Mexico
Schuldenkopf_Peru <- Schulden_Peru/Einwohner_Peru
library(lubridate)
Jahr <- c("01/01/2005","01/01/2006","01/01/2007","01/01/2008","01/01/2009","01/01/2010","01/01/2011","01/01/2012","01/01/2013","01/01/2014","01/01/2015","01/01/2016","01/01/2017")
Jahr <- as.Date(Jahr, format = "%d/%m/%Y", origin = "1970-01-01")
Schuldenquote_Lateinamerika <- data.frame(Jahr,Schuldenquote_Argentinien,Schuldenquote_Brasilien,Schuldenquote_Chile,Schuldenquote_Mexico,Schuldenquote_Peru)
Schuldenkopf_Lateinamerika <- data.frame(Jahr, Schuldenkopf_Argentinien, Schuldenkopf_Brasilien, Schuldenkopf_Chile, Schuldenkopf_Mexico, Schuldenkopf_Peru)
names(Schuldenquote_Lateinamerika) <- c("Jahr","ARG","BRA","CHL","MEX","PER")
names(Schuldenkopf_Lateinamerika) <- c("Jahr","ARG","BRA","CHL","MEX","PER")
library(plotly)
Schulden_Plot <- plot_ly(data = Schuldenkopf_Lateinamerika, y = ~ARG, x = ~Jahr, name = "ARG", type = "bar", color = toRGB("cornflowerblue")) %>%
add_trace(y = ~BRA, name = "BRA", marker = list(color = toRGB("forestgreen"))) %>%
add_trace(y = ~CHL, name = "CHL", marker = list(color = toRGB("firebrick1"))) %>%
add_trace(y = ~MEX, name = "MEX", marker = list(color = toRGB("gold2"))) %>%
add_trace(y = ~PER, name = "PER", marker = list(color = toRGB("darkorchid"))) %>%
add_lines(y = Schuldenquote_Lateinamerika$ARG, name = " ", mode = "lines", line = list(color = toRGB("cornflowerblue"), width = 2, yaxis = "y2")) %>%
add_lines(y = Schuldenquote_Lateinamerika$BRA, name = " ", mode = "lines", line = list(color = toRGB("forestgreen"), width = 2, yaxis = "y2")) %>%
add_lines(y = Schuldenquote_Lateinamerika$CHL, name = " ", mode = "lines", line = list(color = toRGB("firebrick1"), width = 2, yaxis = "y2")) %>%
add_lines(y = Schuldenquote_Lateinamerika$MEX, name = " ", mode = "lines", line = list(color = toRGB("gold2"), width = 2, yaxis = "y2")) %>%
add_lines(y = Schuldenquote_Lateinamerika$PER, name = " ", mode = "lines", line = list(color = toRGB("darkorchid"), width = 2, yaxis = "y2")) %>%
layout(yaxis = list(title = "Schulden pro Kopf in USD", linewidth = 3), yaxis2 = list(side = "right", title = "Schulden in Relation zum GDP" , linewidth = 3, overlaying = "y"), xaxis = list(zerolinewidth = 2), barmode = "group", showlegend = FALSE, autosize = TRUE)
Schulden_Plot
I tried several different ways to implement it in the code, but I am probably missing something?
I think you used wrong argument position yaxis should be outside the line = list.
Additionally, I adjust the margin to looks clear.
Full code is shown below :
Schulden_Plot <- plot_ly(data = Schuldenkopf_Lateinamerika, y = ~ARG, x = ~Jahr, name = "ARG", type = "bar", color = toRGB("cornflowerblue")) %>%
add_trace(y = ~BRA, name = "BRA", marker = list(color = toRGB("forestgreen"))) %>%
add_trace(y = ~CHL, name = "CHL", marker = list(color = toRGB("firebrick1"))) %>%
add_trace(y = ~MEX, name = "MEX", marker = list(color = toRGB("gold2"))) %>%
add_trace(y = ~PER, name = "PER", marker = list(color = toRGB("darkorchid"))) %>%
add_lines(y = Schuldenquote_Lateinamerika$ARG, name = " ", mode = "lines", line = list(color = toRGB("cornflowerblue"), width = 2), yaxis = "y2") %>%
add_lines(y = Schuldenquote_Lateinamerika$BRA, name = " ", mode = "lines", line = list(color = toRGB("forestgreen"), width = 2), yaxis = "y2") %>%
add_lines(y = Schuldenquote_Lateinamerika$CHL, name = " ", mode = "lines", line = list(color = toRGB("firebrick1"), width = 2), yaxis = "y2") %>%
add_lines(y = Schuldenquote_Lateinamerika$MEX, name = " ", mode = "lines", line = list(color = toRGB("gold2"), width = 2), yaxis = "y2") %>%
add_lines(y = Schuldenquote_Lateinamerika$PER, name = " ", mode = "lines", line = list(color = toRGB("darkorchid"), width = 2), yaxis = "y2") %>%
layout(margin = list(l=50, r= 50, b= 50, t= 50),yaxis = list(title = "Schulden pro Kopf in USD", linewidth = 3),
yaxis2 = list(side = "right", title = "Schulden in Relation zum GDP" , linewidth = 3, overlaying = "y"),
xaxis = list(zerolinewidth = 2), barmode = "group", showlegend = FALSE, autosize = TRUE)

Vertical Range Slider in Plotly time series?

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.

Resources