Left-align chart title in plotly

2019-06-23 18:20发布

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

enter image description here

ggplotly(p) (title alignment not preserved):

enter image description here

标签: r ggplot2 plotly
2条回答
做个烂人
2楼-- · 2019-06-23 18:49

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.

查看更多
太酷不给撩
3楼-- · 2019-06-23 19:01

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)
查看更多
登录 后发表回答