How do I get ggplotly rangeslider to set a sensible initial range with a date axis?

别来无恙 提交于 2020-12-15 04:24:36

问题


I am trying to create an interactive visualisation of an individual's salary growth across multiple employers. When I add a rangeslider to the ggplotly function, the initial min and max values for the slider are set nowhere near the range of observations.

How can I get reasonable initial values for the slider, either by having it set automatically based on the data range or set manually?

data:

df <- data.frame(
"From" = seq(as.Date("2016-01-01"), as.Date("2026-01-01"), by = 14),
"To" = seq(as.Date("2016-01-01"), as.Date("2026-01-01"), by = 14)+13,
"Employer" = c(rep("Current employer",130),rep("Future employer",131)),
"Salary" = seq(50000,250000,length.out = 261)
)

vis:

library(ggplot2)
library(dplyr)
library(plotly)
ggplotly(
ggplot(data = df) +
  geom_rect(aes(xmin = From, xmax = To,
                ymin = Employer, ymax = Employer,
                colour = Employer,
                size = Salary))
) %>%
  rangeslider()

Output:

Desired output:

Please note I have asked a related question on how to enable tooltips to this visualisation.

来源:https://stackoverflow.com/questions/65269966/how-do-i-get-ggplotly-rangeslider-to-set-a-sensible-initial-range-with-a-date-ax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!