Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to hide the y-axis title from a plotly plot, and have the margin adjusted to avoid unnecessary white space where the axis 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: enter image description here

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

library(plotly)
pl <- ggplotly(gg)

Result: enter image description here

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
201 views
Welcome To Ask or Share your Answers For Others

1 Answer

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...