Search code examples
rplotlyr-plotly

Vertical line appearing when manually adjust x-axis range using plotly


I am creating a chart using plotly and would like to extend the x-axis to 0. However, for some reason, when I do this, plotly renders a vertical reference line at 0, as shown below. Is there a way to omit this line and have the vertical line at 0 simply appear gray line the other grid lines?

enter image description here

library(dplyr)
library(plotly)

mtcars %>% 
  mutate(cyl = factor(cyl)) %>% 
  plot_ly(
    x = ~mpg, 
    y = ~cyl,
    type = "scatter",
    mode = "markers"
  ) %>% 
  layout(xaxis = list(
    range = c(0, 35)
  ))

Solution

  • By default plotly will automatically draw a zeroline. That could be toggled on and off via layout argument zeroline. If you want a zeroline you could set the color to be equal to the color of the gridlines via zerolinecolor="#eee", where #eee is the default grid line color. See the docs.

    library(dplyr)
    library(plotly)
    
    mtcars %>% 
      mutate(cyl = factor(cyl)) %>% 
      plot_ly(
        x = ~mpg, 
        y = ~cyl,
        type = "scatter",
        mode = "markers"
      ) %>% 
      layout(xaxis = list(
        range = c(0, 35), 
        zeroline = FALSE
        #zerolinecolor = "#eee"
      ))