use sec.axis from ggplot2 in ggplotly

Viewed 104

I am using ggplotly to make plotly graphs and have been using sec.axis to label my facets in ggplot2. Like this: ggplot

After this I try to pass the ggplot object to ggplotly and the sec.axis disappears. I tried to add a second yaxis in layout() but it does not seem to work. How can I label my facets in plotly similar to using sec.axis() in ggplot2?

library(ggplot2)

library(plotly)

p1 <- ggplot(mtcars)+
      geom_point(aes(x = cyl , y = mpg))+
      facet_grid(vs~am)+
      scale_y_continuous("MPG", sec.axis = sec_axis(~., name = "VS") )+ 
      theme(strip.placement = "outside")

ay <- list(
  tickfont = list(size=11.7),
  titlefont=list(size=14.6),
  overlaying = "y",
  nticks = 5,
  side = "right",
  title = list(
  text = "Second y axis",
  standoff = 0)
)

ggplotly(p1) %>%
  layout(yaxis2 = ay)
1 Answers
Related