1

When I add a second axis to a ggplotly object, it does not align to where it should be.

library(ggplot2)
library(plotly)
df <- data.frame(dates = seq.Date(from = as.Date("01/01/1998", "%d/%m/%Y"), 
                          to = as.Date("26/01/1998", "%d/%m/%Y"), by ="day"),
         var1 = sample(1:100, 26),
         var2 = sample(1:10, 26, replace = TRUE))
p <- ggplot() + 
  geom_line(data = df, aes(x = dates, y = var1, lty = 'legend')) +
  geom_point(data = df, aes(x = dates, y = var2), colour = "red")

ay <- list(
  overlaying = "y",
  side = "right",
  title = "var2",
  color = "red"
)

ggplotly(p) %>% 
  add_lines(x = ~dates, y = ~var1, colors = NULL, yaxis = "y2", 
            data = df, showlegend = FALSE, inherit = FALSE) %>%
  layout(yaxis2 = ay)

enter image description here

The red line at zero (right y axis) is underneath the zero of the left hand axis. Does anyone know whats happening here or how to fix it? I want the second axis to have the same scale as the first axis.

Thanks

user63230
  • 4,095
  • 21
  • 43
  • Related: https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales try doing it in native `plotly` functions – M-- Jan 10 '23 at 18:10
  • thanks but `plotly` does not support `sec_axis` and for my purposes I want to stick with `ggplotly` – user63230 Jan 10 '23 at 18:15
  • It does support it: https://plotly.com/r/multiple-axes/ ggplot doea not support secondary axes with different scales. Read the link that I shared in my first comment. – M-- Jan 10 '23 at 19:31
  • Cheers but `ggplotly` does not support `sec_axis` as far as I am aware, see [here](https://github.com/plotly/plotly.R/issues/1051) and [here](https://stackoverflow.com/questions/70338188/use-sec-axis-from-ggplot2-in-ggplotly) - its an open issue. The link you gave presents `plotly` (not `ggplotly` which is what I am looking for and your first link is in relation to `ggplot`, not `ggplotly` – user63230 Jan 10 '23 at 20:41
  • 1
    We, more or less, are saying the same thing. If ggplotly doesn't support it, and you won't wanna do it in native plotly, it comes down to the fact that sec_axis in ggplot cannot have different scales. Sorry that I couldn't help. Hopefully someone would come up with a workaround. Good luck. – M-- Jan 11 '23 at 01:17
  • 1
    `ggolotly` and `ggplot2` create the same graph at the same scales in this code. If you had used `set.seed` or provided data via `dput`, I may have seen what you see. Your picture has a y-axis zero-line. Your code does not appear to have create the graph pictured. Where "should" the axes align? 0 = 0 and 100 = 100? If that's what you want and you believe it's not right, then take the range generated for `yaxis` and add that specification to `yaxis2`. If `plt <- ggplotly(p)`, then `plt$x$layout$yaxis$range` gets you that info. Add `range = ...` to `yaxis2` and rerun. – Kat Jan 12 '23 at 01:55
  • @Kat sorry, forgot to add `set.seed`. Thank you, that worked perfectly! – user63230 Jan 12 '23 at 16:54

1 Answers1

0

Thanks to @Kat, the following works:

library(ggplot2)
library(plotly)
set.seed(23)
df <- data.frame(dates = seq.Date(from = as.Date("01/01/1998", "%d/%m/%Y"), 
                                  to = as.Date("26/01/1998", "%d/%m/%Y"), by ="day"),
                 var1 = sample(1:100, 26),
                 var2 = sample(1:10, 26, replace = TRUE))
p <- ggplot() + 
  geom_line(data = df, aes(x = dates, y = var1, lty = 'legend')) +
  geom_point(data = df, aes(x = dates, y = var2), colour = "red")

#extract left hand side information
plt <- ggplotly(p)
plt$x$layout$yaxis$range
# [1] -3.6 97.6
plt$x$layout$yaxis$tickvals
# [1]  0 25 50 75

ay <- list(
  overlaying = "y",
  side = "right",
  title = "var2",
  color = "red",
  #add in values from above
  range = c(-3.6, 97.6),
  tickvals =  c(0, 25, 50, 75)
)

ggplotly(p) %>% 
  add_lines(x = ~dates, y = ~var1, colors = NULL, yaxis = "y2", 
            data = df, showlegend = FALSE, inherit = FALSE) %>%
  layout(yaxis2 = ay)

enter image description here

user63230
  • 4,095
  • 21
  • 43