在Plotly中左对齐图表标题

7

我如何在由ggplotly创建的plot_ly对象中左对齐图表标题?

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个回答

12

plot_ly 已经 添加 了这个功能。现在您可以调用:

ggplotly(p) %>%
  layout(
    title = list(
      xanchor = "right"
    )
  )

ggplotly(p) %>%
  layout(
    title = list(
      x = 0.1
    )
  )

其中 x 是规范化位置,x=0 将所有内容定位到最左边,x=1将所有内容定位到最右边。


8

您可以执行

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接