Ben
Ben

Reputation: 1522

Numbers of the y-axis missing in plot_ly()

I've done a plot with plot_ly in r:

Plot

Basically, it's fine. The only problem is that the values of the right y-axis are cut off (it should be 0, 200, 400, 600, 800, 1000). Is there a way to adjust the field or something similar?

plot_ly(scan, x = ~distance, y = ~Available_edges, name ="Available edges") %>%
  add_lines(colors = "blue") %>%
  add_lines(x = ~distance, y = ~cost, colors = "red", name = "cost", yaxis ="y2")  %>%
  add_lines(x = ~distance, y = ~cost_adj, colors = "green", name = "cost_adj", yaxis ="y2")  %>%
  layout(title="Distance scan",
         xaxis=list(autorange = "reversed"),
         xaxis=x, 
         yaxis=y,
         yaxis2 = list(overlaying = "y", 
                       side = "right",
                       yaxis=y2),
         legend = list(x = 0.1, y = 0.5)
         )

Upvotes: 0

Views: 806

Answers (1)

parkerchad81
parkerchad81

Reputation: 558

You should read Setting Graph Size in R, Plotly has many layout options that may be of interest, specifically automargin.

automargin (boolean)

Determines whether long tick labels automatically grow the figure margins.

Example Code

library(plotly)


ay <- list(
  tickfont = list(color = "red"),
  overlaying = "y",
  side = "right",
  automargin = TRUE,
  title = "second y axis"
)

plot_ly()  %>%
  add_lines(
    x = ~ rnorm(10, mean = 50, sd = 25),
    y = ~ rnorm(10, mean = 50000, sd = 25000),
  ) %>%
  add_lines(
    x = ~ rnorm(10, mean = 50, sd = 25),
    y = ~ rnorm(10, mean = 500, sd = 250),
    yaxis = "y2"
  ) %>%
  layout(title = "Double Y Axis - automargin",
         yaxis2 = ay,
         yaxis = list(title = 'first y axis'),
         xaxis = list(title = "x"),
         legend = list(x = 0.1, y = 0.5))

Example Plot enter image description here

Upvotes: 1

Related Questions