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.
Related
This is the code I used for producing the graph:
plot1 <- plot_ly(ukdf, x = ~date, y = ~weeklyincidence,
name = "Weekly Incidence/100k",
type = "scatter",
mode = "lines+markers",
line = list(width = 2)) %>%
layout(title = "United Kingdom: Impact of Vaccination on ICU admissions", xaxis = list(title = "Date",
zerolinewidth = 2,
nticks = 20),
yaxis = list(title = "Weekly Incidence/100k & ICU admissions"),
autosize = F, width = 1500, height = 500)
plot1 <- plot1 %>% add_trace(y = ~severeicu,
name = "ICU admissions",
line = list(width = 2)) %>%
layout(shapes = list(vline(as.Date("2020-12-08")),
vline(as.Date("2021-06-09")),
vline(as.Date("2021-09-02")),
vline(as.Date("2021-12-19"))))
**plot1 %>%
add_text(showlegend = FALSE, x = c("2020-12-08", "2021-06-09", "2021-09-02", "2021-12-19"), y = c(4300, 2500, 3000, 4000),
text = c("Start of Vaccination", "50% fully vaccinated", "75% vaccinated", "50% boosted"),
mode = "markers"**)
print(plot1)
Plotly automatically adds a line between the add_text label positioning points that I have specified. How can I remove this line?
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
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
I want to create time series bar chart with an updatemenu() for dates. I have created the menu but I do not know how to connect it with my plotly bar chart.
date<-c("2007-12-31","2008-01-01","2008-01-02")
t1<-c(2200,3455,3456)
t2<-c(2280,3457,3456)
kl<-data.frame(date,t1,t2)
p <- plot_ly(kl,x = kl$D, y = kl$y, type = 'bar', name = 'Real Estate Index 1', marker = list(color = 'rgb(49,130,189)'),
xbins = list(
end = "2008-01-02",
size = "M1",
start = "2007-12-31"
)) %>%
add_trace(kl,y = kl$z, name = 'Real Estate Index 2', marker = list(color = 'rgb(204,204,204)')) %>%
layout(xaxis = list(title = "", tickangle = -45),
yaxis = list(title = ""),
margin = list(b = 100),
barmode = 'group',
updatemenus = list(
list(
x = 0.1,
y = 1.15,
active = 1,
showactive = TRUE,
buttons = list(
labels("D1", "Day"),
labels("M1", "Month"),
labels("M6", "Half Year"),
labels("M12", "Year")
)
)
))
p