Left-align chart title in plotly

故事扮演 提交于 2019-12-12 12:13:56

问题


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


回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/44712678/left-align-chart-title-in-plotly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!