L. F. L. F. - 1 month ago 14
R Question

How can I fix misaligned ticks on the y axis of my plotly graphic?

This is how my code is looking like right now:

library(plotly)
library(RColorBrewer)

year <- c(1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006)
amt <- c(11000, 16000, 21000, 27000, 33000, 37000, 43000, 54000, 68000, 94000, 128000, 170000, 213000, 258000, 307000, 348000, 385000)

data <- data.frame(year, amt)

data$year <- factor(data$year, levels = data[["year"]])

a <- list(title = "Years",
showline = TRUE,
showgrid = FALSE,
showticklabels = TRUE,
linecolor = 'black',
linewidth = 1,
autotick = FALSE,
ticks = 'outside',
tickcolor = 'black',
tickwidth = 2,
ticklen = 5,
tickfont = list(family = 'Cambria',
size = 10,
color = 'rgb(82, 82, 82)'))

b <- list(title = "Years",
showline = TRUE,
showgrid = FALSE,
showticklabels = TRUE,
linecolor = 'black',
linewidth = 1,
autotick = FALSE,
ticks = 'outside',
tickcolor = 'black',
tickwidth = 2,
ticklen = 5,
tickfont = list(family = 'Cambria',
size = 10,
color = 'rgb(82, 82, 82)'))


plot_ly(data, x = ~year,
y = ~amt,
name= '',
type='scatter',
mode = 'lines+markers',
line = list(color = toRGB('#964f4d')),
marker = list(color = toRGB("#964f4d"))) %>%
layout(title = 'Pre-purchase financing poll',
xaxis = a,
yaxis = b)


But every tick from the y axis is showing in the 0 mark. Is there a way to change the scale of how the ticks are distributed? The graphic itself looks fine. Thank you!

Answer

You need to either:

  • specify autotick = TRUE
  • or manually specify tickval and ticktext like so:

    b <- list(title = "Years",
          showline = TRUE,
          showgrid = FALSE,
          showticklabels = TRUE,
          linecolor = 'black',
          linewidth = 1,
          tickval = amt,
          ticktext = amt,
          ticks = 'outside',
          tickcolor = 'black',
          tickwidth = 2,
          ticklen = 5,
          tickfont = list(family = 'Cambria',
                          size = 10,
                          color = 'rgb(82, 82, 82)'))