I wish to plot a series with historic data, but it should be zoomed in to the last year when it starts out. My problem is that when I zoom in to the range of the last year using range for xaxis and yaxis, the line graph touches the xaxis on the top and the bottom. How can I program it so that it zooms in without touching the axes. One way would be to increase the range for the yaxis with an extra 6 percent on the top and the bottom like R does for base R graphics. I was wondering if there is a feature in plotly which does this automatically. I would love to hear from the community here.
Here is a reprex:-
library(zoo)
library(quantmod)
library(plotly)
myplotly <- function(x,n){
mydf <- data.frame(data.frame(x,row.names=NULL), "Date"=index(x))
colnames(mydf)[1] = "Data"
plot_ly(mydf,type="scatter",mode="lines") %>%
add_trace(x=~Date, y =~Data,hovertemplate = '%{x:%A} %{y:.2f}<extra></extra>') %>%
layout(title=n,
xaxis=list(title="",mirror=TRUE,showline=TRUE,linewidth=2, range=c(Sys.Date()-365,Sys.Date())),
yaxis=list(title="",showline=TRUE,linewidth=2,mirror=TRUE, range = c(min(window(x,start = Sys.Date()-365)),max(window(x,start = Sys.Date()-365, end = Sys.Date())))),
showlegend=FALSE)
}
getSymbols("GOOG")
myplotly(GOOG[,4],"Time Series")
As you will see in the above graph the graph starts out touching the xaxis on the top and the bottom.