在图表中左对齐图表标题 [英] Left-align chart title in plotly

查看:239
本文介绍了在图表中左对齐图表标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在plot_ly对象(由ggplotly创建)中将图表标题左对齐?

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)

p的输出(预期的标题对齐):

Output of p (intended title alignment):

ggplotly(p)(不保留标题对齐):

ggplotly(p) (title alignment not preserved):

推荐答案

您可以做到

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)

这篇关于在图表中左对齐图表标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆