How can I left-align the chart title in a plot_ly
object (as created from ggplotly
)?
library(ggplot2)
library(plotly)
p <-
ggplot(mtcars, aes(mpg, cyl)) +
geom_point() +
ggtitle("My Title") +
# Not necessary by default:
theme(plot.title = element_text(hjust = 0.0))
p
ggplotly(p)
Output of p
(intended title alignment):
ggplotly(p)
(title alignment not preserved):
You could do
ggplotly(p) %>%
add_annotations(
yref="paper",
xref="paper",
y=1.15,
x=0,
text="My Title",
showarrow=F,
font=list(size=17)
) %>%
layout(title=FALSE)
plot_ly
has added this functionality. Now you can call:
ggplotly(p) %>%
layout(
title = list(
xanchor = "right"
)
)
or
ggplotly(p) %>%
layout(
title = list(
x = 0.1
)
)
where x
is the normalized position, with x=0
positioning all the way to the left and x=1
positioning all the way to the right.