Y-axis title position on a faceted ggplotly

Hi everyone !
As in this subject : ggplotly adjust label position, I wanted to "push" the y axis title away from the y-axis on my graph. And the solution given there worked great for a while until I had to facet_grid my plot with 2 variables. When I do so, the title is centered on one of the facetted plot (the top one) like so :

With the example given in the post metionned previously :

library(dplyr)
library(ggplot2)
library(plotly)

iris %>%
  ggplot(aes(x=Sepal.Length, y=Sepal.Width)) +
  geom_point() +
  labs(
    x = NULL,
    y = NULL 
  )+
  theme(text=element_text(size=24)) +
  facet_grid(vars(Species)) -> g

ggplotly(g) %>%
  layout(margin=list(l=300, r=100, b=100, t=100),
         yaxis=list(
           title=list(text = "Sepal width", standoff=50)
         ))

I looked through this page (Layout.yaxis in R) to see if there were an argument or something that could be helpful but I did not manage to make anything work...

1 Like

Hi, @lu_rchr
Maybe a different thought.
I input the axis-title in labs() and push away the y-axis title use the axis.title.y in function theme.
Mainly refer to the following link. Modify components of a theme — theme • ggplot2 (tidyverse.org)
Hope it would be helpful to you.

library(dplyr)
library(ggplot2)
library(plotly)

iris %>%
  ggplot(aes(x=Sepal.Length, y=Sepal.Width)) +
  geom_point() +
  labs(
    x = NULL,
    y ="Sepal width" 
  )+
  theme(text=element_text(size=24)) +
  facet_grid(vars(Species)) -> g
g

g+theme(axis.title.y = 
          element_text(margin = 
                         margin(0,2,0,0,'cm')))->g1

g1

Created on 2023-08-13 with reprex v2.0.2


This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.