How to edit axis titles of a faceted-ggplot-object converted to a plotly-object using the function ggplotly()?

Viewed 26

I recently remarked that plotly::ggplotly(), when used on a faceted-ggplot-object, creates axis titles as annotations i.e. they can not be changed using layout(yaxis = ...) as proposed here.

What is the intended way of editing axis labels of a faceted-ggplot-object?

library(ggplot2)
library(plotly)
library(magrittr)
ex <- ggplot(data = iris, aes(x = Sepal.Width, y = Petal.Width)) +
  facet_grid(.~ Species)
# Fails
plotly::ggplotly(ex) %>% 
  layout(yaxis = list(title = list(text = "My title")))

# Possible workaround
ex1 <- plotly::ggplotly(ex)
ex1$x$layout$annotations[[2]]$text <- "My title"
ex1

Created on 2022-09-15 with reprex v2.0.2

0 Answers
Related