左对齐图表标题

问题描述:

如何将图表标题对齐到plot_ly对象(如从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) 

p(要的标题对齐)的输出:

enter image description here

ggplotly(p)(不保留标题排列):

enter image description here

你可以做

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)