修复情节ggplotly()标题重叠图,当标题在两条线上分割时

时间:2017-03-15 21:37:32

标签: javascript r plot plotly

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

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

overlapping plotly title

2 个答案:

答案 0 :(得分:1)

Plotly忽略尾随的新行字符,并且对于新行也需要HTML中断<br />而不是\n(请参阅末尾的示例)。

添加<br />以手动中断标题,并在您的布局中添加margin layout(gp, margin=list(t = 75))

that works

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 ggplot

<强> plotly

plotly

答案 1 :(得分:-2)

如您所知,ggplot不会重新计算总绝对高度。因此,在标题和绘图之间添加一些缓冲区空间的最简单方法是在长标题的末尾添加附加换行符\n)。

ggtitle("A REALLY, REALLY, REALLY LONG TITLE THAT I WANT TO\nSPLIT INTO TWO LINES\n")