当标题在两行上拆分时,修复 plotly ggplotly() 标题重叠图 [英] Fix plotly ggplotly() Title Overlapping Plot When Title is Split on Two Lines

查看:56
本文介绍了当标题在两行上拆分时,修复 plotly ggplotly() 标题重叠图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,标题的第二行与图略有重叠.有没有办法通过增加标题和情节之间的间距来解决这个问题?

In the example below, the second line of the title overlaps slightly with the plot. Is there a way to fix this by increasing the spacing between the title and plot?

library(ggplot2)
library(plotly)
library(magrittr)

p1 <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point() + 
  ggtitle("A REALLY, REALLY, REALLY LONG TITLE THAT I WANT TO\nSPLIT INTO TWO LINES")
p1

ggplotly() %>% config(collaborate=FALSE, cloud=FALSE, displaylogo=FALSE, modeBarButtonsToRemove=c("select2d", "sendDataToCloud", "pan2d", "resetScale2d", "hoverClosestCartesian", "hoverCompareCartesian", "lasso2d", "zoomIn2d", "zoomOut2d"))

推荐答案

Plotly 忽略尾随换行符并且还需要 HTML 中断 <br/> 而不是 \n 用于新行(请参阅末尾的示例).

Plotly ignores trailing new line characters and also needs HTML breaks <br /> instead of \n for new lines (see example at the end).

添加 <br/> 以手动打破您的标题并将顶部 margin 添加到您的布局 (layout(gp, margin=list(t= 75))).

Add <br /> to manually break your title and add a top margin to your layout (layout(gp, margin=list(t = 75))).

library(ggplot2)
library(plotly)
library(magrittr)

p1 <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point() + 
  ggtitle("A REALLY, REALLY, REALLY LONG TITLE THAT I WANT TO <br />\nSPLIT INTO TWO LINES<br />\n")
p1

gp <- ggplotly() %>% config(collaborate=FALSE, cloud=FALSE, displaylogo=FALSE, modeBarButtonsToRemove=c("select2d", "sendDataToCloud", "pan2d", "resetScale2d", "hoverClosestCartesian", "hoverCompareCartesian", "lasso2d", "zoomIn2d", "zoomOut2d"))
gp <- layout(gp, margin=list(t = 75))
gp

<小时>

ggplot

情节

这篇关于当标题在两行上拆分时,修复 plotly ggplotly() 标题重叠图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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