Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

R Plotly does not center title correctly

My issue is that, in R, the plot_ly and ggplotly commands produce a title that is centered to my entire plot including the legend on the side (which is very wide in my case) making it look like my title is off center.
When working with just ggplot2, the title correctly centers to the graph only, not including the legend.

I’m working with a Shiny app that takes in user data, so depending on the data, the legend could be very wide, making the title look extremely off-centered and unprofessional. Putting the legend inside the graph could fix this, but isn’t a great option, since I can’t predict the user data and it may obscure some of it. I also don’t want to put the legend at the bottom of the graph.

Here are some examples that show how plot_ly and ggplotly center the title vs. ggplot2: (I can only include one image)
ggplotly:
p <- ggplot(mtcars, aes(x = wt, y=mpg, colour = wt)) + geom_point()
p <- p + geom_hline(yintercept = 20, size = .25) + ggtitle(‘center title to graph only, not including legend’)
ggplotly§

plot_ly:
q = plot_ly(mtcars, x = wt, y = mpg, color = wt, mode = ‘markers’)
layout(q, title = ‘center title to graph only not including legend’)

ggplot2:
after ggplot code above, just type p

ggplot2 (without plotly) is the only one that centers the title how I want (the text is smaller, but you can tell it is centered to the graph only, not including the legend).