如何将信息框添加到 R 中的绘图中?

How can I add an infobox to a plotly plot in R?

我正在使用 R plotly 来显示数据点。每个数据点都附有文本,我想在单击相应数据点时将其显示在信息框中。

我的代码如下所示。目前 "MESSAGE_BODY" 正在显示在工具提示中,但是当文本很长时它不会起作用,因为文本超出了工具提示的限制。

在我的原始代码下方,我提供了一个可重现的示例来说明我所说的 "Exceeds the limits"。

原代码:

plot_ly(PLS.scores, 
x = Comp1, 
y = Comp2,
type = "scatter",
mode = "markers",
color = as.factor(TARGET),
colors = c("red", "green"),
text = paste("Message:", MESSAGE_BODY))

可重现的例子:

install.packages("plotly")
library(plotly)

texts <- c("This is a short text and it works fine with the tool tip",
           "This text also work fine with the tooltip",
           "So does this one",
           "This text however, is so lengthy that it exceeds the limits if the tooltip. Therefore it would be extremely nice to be able to show such lengthy texts in a separate window with scrollbars! Or at least it is so lenghty that i hope you get the point... ")

dataX <- c(1,2,3,1)
dataY <- c(2,1,3,3)
TARGET <- c(1,0,1,0)

df <- data.frame(dataX,dataY,TARGET,texts)

plot_ly(df, 
x = dataX, 
y = dataY,
type = "scatter",
mode = "markers",
color = as.factor(TARGET),
colors = c("red", "green"),
text = paste("Message:", texts))

添加<br>将文本分隔到新行

texts <- c("This is a short text and it works fine with the tool tip",
       "This text also work fine with the tooltip",
       "So does this one",
       "This text however, is so lengthy that it exceeds the limits if the tooltip. <br> Therefore it would be extremely nice to be able to show such lengthy texts in a separate window with scrollbars!<br> Or at least it is so lenghty that i hope you get the point... ")