问题
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