2017-06-23 146 views
2

如何將圖表標題對齊到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

回答

2

你可以做

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)