marcrng
marcrng

Reputation: 31

R Plotly - Large unused space on top and bottom of graph when setting height

I'm trying to create a scatterplot with categorical data on the y-axis so that the data can be viewed by scrolling down.

To achieve this, I've set the tick type to 'category' and its mode to 'linear' and manually set the height to allow for enough room for plotly to show each label. However, this is leaving me with large gaps on the top and bottom of the plot

Here's an example:

testdata <- data.frame(a = sample(0:500),b = 0:500)

plot_ly(testdata,
        type = 'scatter',
        mode = 'markers',
        x = ~a,
        y = ~b,
        height = 5000
        ) %>% 
  layout(
    margin = list(
      l = 50,
      r = 50,
      b = 100,
      t = 1,
      pad = 1
    ),
    yaxis = list(
      type = 'category',
      tickmode = 'linear',
      dtick = 1
    )
  )

Image of plot showing large white spaces on top and bottom

I've tried playing around with the padding and margins with no luck. Setting a lower height just results in a slightly smaller white space without all of the y labels included.

Ideal result would have the upper and lower white space be smaller and all of the y axis labels showing.

Upvotes: 2

Views: 1328

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33442

You can simply set the y-axis range:

library(plotly)

testdata <- data.frame(a = sample(0:500),b = 0:500)

plot_ly(testdata,
        type = 'scatter',
        mode = 'markers',
        x = ~a,
        y = ~b,
        height = 5000
) %>% 
  layout(
    margin = list(
      l = 50,
      r = 50,
      b = 100,
      t = 1,
      pad = 1
    ),
    yaxis = list(
      type = 'category',
      tickmode = 'linear',
      dtick = 1,
      range = c(min(testdata$b)-1, max(testdata$b)+1)
    )
  )

Upvotes: 1

Related Questions