Range Slider in R plotly for Non-numerical Variable

261 Views Asked by At

I have a plot in Shiny that I created with r-plotly, which has a categorical variable on the x-axis. The plot is so wide that to make the labels readable I have to expand it and scroll all the way to the right to see the right side of it, but when I do that the y-axis dissapears.

I found out that a rangeslider can make it easier to read the plot if the range is pre-set to a small window in numerical plots, and I would like to know how to apply that to a categorical axis instead. I was able to add a range slider to my plot, but it defaults to the whole range of the plot, and I would like to pre-set the range. Can anyone tell me how to do that?

I want the range to default to the first 20 categories so I tried the following, but it did not work. The range slider still shows up for the whole range of the plot.

Is what I am trying to accomplish even doable for categorical axes? If not, how else can I show the whole plot if I have too many categories to make the axis tick-labels readable.

x <- df$categories

start <- x[1]

end <- x[20]

p %>% layout(xaxis = 
  list(rangeslider = 
    list(rangemode='fixed', 
         range = list(start, end)
    )
  )
)
1

There are 1 best solutions below

0
ekolima On

Adding a pipe directly to the plot with p %>% rangeslider(start=0, end=20) should work (not tested though on your example, given it's not reproducible). Start and end are the positions where the first and last observations are placed in the dataset, beginning from 0.