在 plotly 中格式化长文本标签的工具提示
Format tooltip in plotly for long text labels
考虑下面的简单示例。有没有一种方法可以格式化 plotly 工具提示,使长文本标签在框中可见,而不是这个切断值的荒谬矩形?
library(ggplot2); library(plotly)
df <- data.frame(x = 1:10, y = 1:10, z = rep("the longggggggggggggggggggggggggggggestttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt labelllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll you can imagineeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 10))
p <- ggplot(df, aes(x,y,label=z)) + geom_point()
ggplotly(p, tooltip = "label")
我很确定,某个地方存在更优雅的解决方案。我只能建议你像每个 n
字符一样休息一下。 :
有一个很好的解决方法
gsub('(.{1,90})(\s|$)', '\1\n', s)
It will break string "s" into lines with maximum 90 chars (excluding
the line break character "\n", but including inter-word spaces),
unless there is a word itself exceeding 90 chars, then that word
itself will occupy a whole line.
所以我们只需要将gsub()
添加到ggplot
美学中:
p <- ggplot(df, aes(x,y,
text = gsub('(.{1,20})(\s|$)', '\1\n', z))) +
geom_point()
ggplotly(p, tooltip = "text")
更新
这是@Rich Pauloo 评论中的一个更优雅的解决方案。在这种情况下,您的字符串也将大部分被左填充(但实际上是自动对齐的)。但是,填充取决于绘图分辨率和标签位置。
library(stringr)
p <- ggplot(df,
aes(x, y,
text = stringr::str_wrap(
string = z,
width = 20,
indent = 1, # let's add extra space from the margins
exdent = 1 # let's add extra space from the margins
))) +
geom_point()
ggplotly(p, tooltip = "text")
考虑下面的简单示例。有没有一种方法可以格式化 plotly 工具提示,使长文本标签在框中可见,而不是这个切断值的荒谬矩形?
library(ggplot2); library(plotly)
df <- data.frame(x = 1:10, y = 1:10, z = rep("the longggggggggggggggggggggggggggggestttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt labelllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll you can imagineeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 10))
p <- ggplot(df, aes(x,y,label=z)) + geom_point()
ggplotly(p, tooltip = "label")
我很确定,某个地方存在更优雅的解决方案。我只能建议你像每个 n
字符一样休息一下。 :
gsub('(.{1,90})(\s|$)', '\1\n', s)
It will break string "s" into lines with maximum 90 chars (excluding the line break character "\n", but including inter-word spaces), unless there is a word itself exceeding 90 chars, then that word itself will occupy a whole line.
所以我们只需要将gsub()
添加到ggplot
美学中:
p <- ggplot(df, aes(x,y,
text = gsub('(.{1,20})(\s|$)', '\1\n', z))) +
geom_point()
ggplotly(p, tooltip = "text")
更新
这是@Rich Pauloo 评论中的一个更优雅的解决方案。在这种情况下,您的字符串也将大部分被左填充(但实际上是自动对齐的)。但是,填充取决于绘图分辨率和标签位置。
library(stringr)
p <- ggplot(df,
aes(x, y,
text = stringr::str_wrap(
string = z,
width = 20,
indent = 1, # let's add extra space from the margins
exdent = 1 # let's add extra space from the margins
))) +
geom_point()
ggplotly(p, tooltip = "text")