Reputation: 169
I'm trying to plot some data and I want to have a fixed date range even if I filter my data. What I mean by this is when I have this plotly graph below:
e.test_data = structure(list(Date = structure(c(14975, 15005, 15035, 15065,
15095, 15125, 15155, 15185, 15215, 15245, 15275, 15305, 15335,
15365, 15395, 15425, 15455, 15485, 15515, 15545, 15575, 15605,
15635, 15665, 15695, 15725, 15755, 15785, 15815, 15845, 15875,
15905, 15935, 15965, 15995, 16025, 16055, 16085, 16115, 16145,
16175, 16205, 16235, 16265, 16295, 16325, 16355, 16385, 16415,
16445, 16475, 16505, 16535, 16565, 16595, 16625, 16655, 16685,
16715, 16745, 16775, 16805, 16835, 16865, 16895, 16925, 16955,
16985, 17015, 17045, 17075, 17105, 17135, 17165, 17195, 17225,
17255, 17285, 17315, 17345, 17375, 17405, 17435, 17465, 17495,
17525, 17555, 17585, 17615, 17645, 17675, 17705, 17735, 17765,
17795, 17825, 17855, 17885, 17915, 17945, 17975, 18005, 18035,
18065, 18095, 18125, 18155, 18185, 18215, 18245, 18275, 18305,
18335, 18365, 18395, 18425, 18455, 18485, 18515, 18545, 18575,
18605, 18635, 18665, 18695, 18725, 18755, 18785, 18815, 18845,
18875, 18905, 18935, 18965, 18995), class = "Date"), X1 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1), X2 = c("OK", "OK", "OK", "OK", "OK",
"OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK",
"OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK",
"OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK",
"OK", "OK", "OK", "NOK", "OK", "NOK", "OK", "NOK", "OK", "NOK",
"OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK",
"OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK",
"OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK",
"OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK",
"OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK",
"OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK",
"OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK",
"OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK")), row.names = c(NA,
135L), class = "data.frame")
plot_ly(data=e.test_data, x=~Date, y=~X1, type ="bar", color=~X2)%>%
layout(xaxis = list(range = c(as.numeric(as.Date('2010-01-01'))*1000,
as.numeric(as.Date('2023-01-01'))*1000),
type = "date" ))
And select the "NOK" value, the range change. I get this graph below:
Is there a way to keep the same "x" (Date) axis range between 2010 and 2023 even when I only select "NOK" value?
Also, is there a way to show all the years on the date axis (2010,2011,2013,...) not only some of the years?
Thank you very much in advance !
Upvotes: 1
Views: 134
Reputation: 18754
To keep the range fixed, you need to set fixedrange
and assign the range. When I ran your code, nothing plotted because of how you set the range.
You don't need to make it numeric. You actually don't need to set the type, either.
Check out the difference:
plot_ly(data = e.test_data, x = ~Date, y = ~X1, type = "bar", color = ~X2) %>%
layout(xaxis = list(range = c(as.Date('2010-01-01'), as.Date('2023-01-01')),
type = "date", fixedrange = T))
Upvotes: 1