How to show min and max ticks on plotly secondary y axis in R

Viewed 40

I want to show data on a secondary y axis using plotly. unfortunately, no ticks are showing.

What needs to be set so that I can show the minimum, maximum, and display ticks every 5 units? In this example, I would like to see val2 axis showing ticks at 5, 10 (5 is the smallest multiple of 5 < min value of val2/val3 columns, and 10 is the smallest multiple of 5 > max value) in the green box.

It is odd, as the primary y-axis shows and scales it fine

enter image description here enter image description here

code used to generate:

data = structure(list(Quarter = c("Q1-19", "Q2-19", "Q3-19", "Q4-19", "Q1-20", 
                       "Q2-20", "Q3-20", "Q4-20", "Q1-21", "Q2-21", "Q3-21", "Q4-21", 
                       "Q1-22"), val1 = c(-0.104, -0.069, -0.075, 0.1, -0.15, 
                                                 -0.05, -0.0825, 0, 0, 0, 0, 0, 0), val2 = c(8.14554910547994, 
                                                                                                              8.55275717435675, 8.40632494969184, 7.91931645491084, 7.61926376889051, 
                                                                                                              7.9804131283696, 7.84841940402634, 8.17523084115879, 8.20048296800237, 
                                                                                                              8.15037889614863, 8.49192165017997, 8.12639342202955, 8), val3 = c(8.9935262047237, 
                                                                                                                                                                                     9.14278022237807, 9.03679932091872, 8.71124810040193, 8.76215333422409, 
                                                                                                                                                                                     8.37943378478808, 8.49591400485852, 8.17523084115879, 8.20048296800237, 
                                                                                                                                                                                     8.15037889614863, 8.49192165017997, 8.12639342202955, 8)), row.names = c(NA, 
                                                                                                                                                                                                                                                              -13L), class = c("tbl_df", "tbl", "data.frame"))
quarters = data %>%
  select(Quarter) %>%
  pull()
xform <- list(categoryorder = "array",
              categoryarray = quarters)

fig <- plot_ly(data, 
               x = ~Quarter, 
               y = ~val1, 
               name = 'val1', 
               type = 'scatter', 
               mode = 'lines',
               line = list(dash = 'dot',
                           width = 5))%>%
  layout(xaxis = xform) 
ay <- list(
  tick0 = 0,
  dtick = 20,
  overlaying = "y",
  side = "right",
  title = "val2",
  showgrid = FALSE)

fig = fig %>% 
  add_trace(y = ~val2, 
            name = ' val2', 
            yaxis = 'y2',
            line = list(color = "rgb(122, 157, 07)",
                        width = 5,
                        dash = 'line')) %>%
  add_trace(y = ~val3, 
            name = 'val3', 
            yaxis = 'y2',
            line = list(color = "red",
                        width = 5,
                        dash = 'line')) %>%
  layout(
    title = list(text = "values"),
    yaxis2 = ay,
    xaxis = list(title=" ",
                 tickmode = "array",
                 tickvals = sort(c(0, seq( length(quarters)-1,1, -4))),
                 tickangle = 270, 
                 showgrid = FALSE),
    yaxis = list(title="val1")
  )
fig
1 Answers

Update ay to include ticksvals, as well as create a function to show the min and max of the available options

ay <- list(
  tickvals = as.list(find_seq(min(data[,3:4]), max(data[,3:4]))),
  overlaying = "y",
  side = "right",
  title = "val2",
  showgrid = FALSE)

function defined

find_seq = function(lower, upper){
  lower = round(lower,1)
  upper = round(upper,1)
  um=(upper - upper%%5)/5
  lm=(lower - lower%%5)/5
  if(um - lm > 1){
    next_in_seq =  ((lower - lower%%5)/5 + 1) * 5
    btwn_seq = seq(next_in_seq, floor(upper/5)*5, 5)
    if (btwn_seq[1]-lower < 1 & upper - btwn_seq[length(btwn_seq)] < 1){
      c(btwn_seq)
    } else if(btwn_seq[1]-lower < 1){
      c(btwn_seq,upper)
    } else if (upper - btwn_seq[length(btwn_seq)] < 1){
      c(lower, btwn_seq)
    } else{
      c(lower, btwn_seq, upper)
    }
    # c(lower,btwn_seq, upper)
  } else if (um - lm == 1) {
    if(upper%%5 < 1){
      c(lower,(upper - upper%%5))
    } else{
      c(lower,(upper - upper%%5), upper)
    }
    
  } else{
    c(lower, round((lower + upper)/2,1),upper)
  }
}

enter image description here

Related