Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Hiding axis label and reducing margin accordingly

I want to hide the y-axis title from a plot, and have the margin adjusted to avoid unnecessary white space where the title would have been.

In ggplot2, this happens automatically:

library(ggplot2)
dat <- data.frame(x = c(1:5), y = c(1:5)^2)
gg <- ggplot(dat, aes(x, y)) + geom_point() + theme(axis.title.y = element_blank())

Result:

However, plotly (here: ggplotly) does not seem to have the same behavior:

library(plotly)
pl <- ggplotly(gg)

Result:

I’ve been trying to adjust the margin argument, as well as automargin, but I have so far not been able to reduce the left margin as I would like. How can I achieve this using R and plotly?

I managed to achieve what I wanted by setting the title font size to 1, standoff to 0, and margins to 0, as follows:

pl %>% layout(yaxis = list(title = list(standoff = 0, font = list(size = 1))),
              margin = list(r = 0, l = 0, t = 0, b = 0, pad = 0))