【问题标题】:How can I add an infobox to a plotly plot in R?如何将信息框添加到 R 中的情节图?
【发布时间】:2016-03-09 14:58:35
【问题描述】:

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

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

在我的原始代码下方,我提供了一个可重现的示例,说明“超出限制”的含义。

原码:

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

【问题讨论】:

    标签: r plotly


    【解决方案1】:

    添加&lt;br&gt; 将文本分隔到新行

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

    【讨论】:

    • 感谢您的建议。然而,这不是我正在寻找的解决方案,您可能意识到:) 但是,您对如何以编程方式添加
      有什么建议吗?我有很多文本,手动执行此操作没有意义。
    • 这可以解决问题“gsub('(.{1,3})(\\s|$)', '\\1\n', longstring)”,但我不会接受您的回答作为解决方案,因为我要求提供一个单独的信息框。
    猜你喜欢
    • 2017-05-26
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多