Plotly生成的ggplot geom_tile中的工具提示

huangapple go评论101阅读模式
英文:

Tooltips in plotly generated from ggplot geom_tile

问题

我试图制作一个带有两个垂直轴和鼠标悬停工具提示的交互式Plotly图形。图中应该有y1数据点和y2数据条形。当我在两边的数据都使用点时,一切都按需要工作,但当我在y2上使用条形时,无法显示正确的信息。

使用两边的点工作正常:
![点][1]

使用geom_tile生成的右侧条形时,每个条的工具提示都显示相同ID的所有条的信息:
![条形][2]

可能会有一些相关的补充信息:

  • 我使用geom_tile生成条形,因为我需要能够反转轴,而使用其他几何图形时遇到了困难。我基于[这个SO回答][3]采用了geom_tile
  • 需要从ggplot对象生成Plotly图,而不是直接生成Plotly图。这是一个大型Shiny应用的一部分,我需要将ggplot对象存储在另一个位置,并进一步进行其他操作。

MRE

**注意:**我不确定我的示例是否足够简化,因为我不太清楚哪个步骤会导致工具提示出错。因此,我更喜欢在这里包括我的应用程序案例的所有元素:两个垂直轴、反转和包括有问题的条形的两种不同的几何图形。

数据

  1. # 用于垂直轴y1(左侧)
  2. df1 <- data.frame(ID = c("A", "A", "A", "A", "B", "B", "B", "B"),
  3. Date = structure(c(19078, 19085, 19092, 19099, 19078, 19085, 19092, 19099), class = "Date"),
  4. Val = c(236, 221, 187, 136, 77, 100, 128, 180))
  5. # 用于垂直轴y2(右侧)
  6. df2 <- data.frame(ID = c("J", "J", "J", "J", "K", "K", "K", "K"),
  7. Date = structure(c(19078, 19085, 19092, 19099, 19078, 19085, 19092, 19099), class = "Date"),
  8. Val = c(478, 500, 549, 479, 73, 5, 15, 74))

工作正常的情况下,两侧都是点

  1. library(ggplot2)
  2. library(dplyr)
  3. library(plotly)
  4. # 准备y2缩放数据
  5. ylim1 <- rev(range(df1$Val))
  6. ylim2 <- range(df2$Val)
  7. scale_y2.1 <- function(y, ylim1, ylim2) {
  8. ylim1[1] + (ylim1[2] - ylim1[1]) *(y - ylim2[1])/(ylim2[2] - ylim2[1])
  9. }
  10. dfAll <- full_join(df1, df2, by = c("ID", "Date"), suffix = c("1", "2"))
  11. y2.scl <- scale_y2.1(dfAll$Val2, ylim1, ylim2)
  12. dfAll <- dfAll %>% mutate(Val2_scl = y2.scl)
  13. # 准备y2刻度和缩放的刻度线
  14. labs2 <- pretty(ylim2)
  15. brks2 <- scale_y2.1(labs2, ylim1, ylim2)
  16. # 生成ggplot
  17. ggp1 <- ggplot(dfAll) +
  18. geom_point(aes(x = Date, y = Val1, color = ID, group = ID), na.rm = TRUE) +
  19. geom_point(aes(x = Date, y = Val2_scl, group = ID, color = ID), na.rm = TRUE, shape = 4, stroke = 0.6) +
  20. scale_y_continuous(trans = "reverse",
  21. sec.axis = dup_axis(breaks = rev(brks2), labels = rev(labs2), name = "Val2")) +
  22. coord_cartesian(ylim = rev(ylim1))
  23. # 生成Plotly图
  24. yaxis2 <- list(overlaying = "y", range = rev(ylim2), ticks = 'outside', side = "right",
  25. title = "Val2", zeroline = FALSE, showgrid = FALSE, automargin = TRUE,
  26. tickfont = list(size = 11.8), titlefont = list(size = 14.6))
  27. ply1 <- ggplotly(ggp1) %>%
  28. add_lines(x = ~Date, y = ~Val2_scl, yaxis = "y2", data = dfAll, inherit = FALSE) %>%
  29. style(showlegend = FALSE) %>%
  30. layout(yaxis2 = yaxis2)
  31. # 插入工具提示
  32. tlTips <- paste0("Value: ", c(df1$Val, df2$Val), '\n',
  33. "Date: ", dfAll$Date, '\n',
  34. "ID: ", dfAll$ID)
  35. for (i in seq_along(ply1$x$data)) {
  36. aName <- ply1$x$data[[i]]$name
  37. if (!is.null(aName)) {
  38. aTags <- grep(aName, tlTips, value = TRUE, fixed = TRUE)
  39. ply1$x$data[[i]]$text <- aTags
  40. }
  41. }
  42. # 显示图形
  43. ply1

条形在右侧时出错

  1. # 生成ggplot
  2. ggp2 <- ggplot(dfAll) +
  3. geom_point(aes(x = Date, y = Val1, color = ID, group = ID), na.rm = TRUE) +
  4. geom_tile(aes(x = Date, y = (ylim1[1] + Val2_scl)/2, height = ylim1[1] - Val2_scl, fill = ID, group = ID),
  5. na.rm = TRUE, stat = "identity", position = position_dodge(preserve = "single")) +
  6. scale_y_continuous(trans = "reverse",
  7. sec.axis = dup_axis(breaks = rev(brks2), labels = rev(labs2), name = "Val2")) +
  8. coord_cartesian(ylim = rev(ylim1))
  9. # 生成Plotly图
  10. ply2 <- ggplotly(ggp2) %>%
  11. add_lines(x = ~Date, y = ~Val2_scl, yaxis = "y2", data = dfAll, inherit = FALSE) %>%
  12. style(showlegend =
  13. <details>
  14. <summary>英文:</summary>
  15. I&#39;m trying to produce an interactive plotly graphic with two vertical axes and on-hover tooltips. The plot should have y1 data as points and y2 data as bars. **Everything works just as needed when I use points for data in both sides, but when I use bars for y2 I cannot get the tooltips to display the correct information.**
  16. With points at both sides tooltips display fine:
  17. [![points][1]][1]
  18. With `geom_tile` bars at the right side, every bar&#39;s tooltip displays the information of all the bars of the same ID:
  19. [![bars][2]][2]
  20. Some complementary information that could be relevant:
  21. - I&#39;m using `geom_tile` to produce the bars because I need to be able to reverse the axes and I was having difficulties doing that with other geoms. I adopted `geom_tile` based on [this SO answer][3].
  22. - It is necessary to produce the plotly graphic from a ggplot one, not directly the plotly one. This is part of a large Shiny app that I got to work on, where the ggplot object is stored apart and further manipulated for other purposes.
  23. ---------------------
  24. ### MRE
  25. **Note:** I&#39;m not sure if my example is minimal because I don&#39;t know well what part of the process is messing up the tooltips. Thus, I prefer to include all elements of my application case here: two vertical axes, reversing and two different geoms including the problematic bars.
  26. ** Data
  27. # for vertical axis y1 (left)
  28. df1 &lt;- data.frame(ID = c(&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;),
  29. Date = structure(c(19078, 19085, 19092, 19099, 19078, 19085, 19092, 19099), class = &quot;Date&quot;),
  30. Val = c(236, 221, 187, 136, 77, 100, 128, 180))
  31. # for vertical axis y2 (right)
  32. df2 &lt;- data.frame(ID = c(&quot;J&quot;, &quot;J&quot;, &quot;J&quot;, &quot;J&quot;, &quot;K&quot;, &quot;K&quot;, &quot;K&quot;, &quot;K&quot;),
  33. Date = structure(c(19078, 19085, 19092, 19099, 19078, 19085, 19092, 19099), class = &quot;Date&quot;),
  34. Val = c(478, 500, 549, 479, 73, 5, 15, 74))
  35. ** **Working case** with points at both sides
  36. library(ggplot2)
  37. library(dplyr)
  38. library(plotly)
  39. # prepare y2 scaled data
  40. ylim1 &lt;- rev(range(df1$Val))
  41. ylim2 &lt;- range(df2$Val)
  42. scale_y2.1 &lt;- function(y, ylim1, ylim2) {
  43. ylim1[1] + (ylim1[2] - ylim1[1]) *(y - ylim2[1])/(ylim2[2] - ylim2[1])
  44. }
  45. dfAll &lt;- full_join(df1, df2, by = c(&quot;ID&quot;, &quot;Date&quot;), suffix = c(&quot;1&quot;, &quot;2&quot;))
  46. y2.scl &lt;- scale_y2.1(dfAll$Val2, ylim1, ylim2)
  47. dfAll &lt;- dfAll %&gt;% mutate(Val2_scl = y2.scl)
  48. # prepare y2 ticks and scaled breaks
  49. labs2 &lt;- pretty(ylim2)
  50. brks2 &lt;- scale_y2.1(labs2, ylim1, ylim2)
  51. # generate ggplot
  52. ggp1 &lt;- ggplot(dfAll) +
  53. geom_point(aes(x = Date, y = Val1, color = ID, group = ID), na.rm = TRUE) +
  54. geom_point(aes(x = Date, y = Val2_scl, group = ID, color = ID), na.rm = TRUE, shape = 4, stroke = 0.6) +
  55. scale_y_continuous(trans = &quot;reverse&quot;,
  56. sec.axis = dup_axis(breaks = rev(brks2), labels = rev(labs2), name = &quot;Val2&quot;)) +
  57. coord_cartesian(ylim = rev(ylim1))
  58. # generate plotly
  59. yaxis2 &lt;- list(overlaying = &quot;y&quot;, range = rev(ylim2), ticks = &#39;outside&#39;, side = &quot;right&quot;,
  60. title = &quot;Val2&quot;, zeroline = FALSE, showgrid = FALSE, automargin = TRUE,
  61. tickfont = list(size = 11.8), titlefont = list(size = 14.6))
  62. ply1 &lt;- ggplotly(ggp1) %&gt;%
  63. add_lines(x = ~Date, y = ~Val2_scl, yaxis = &quot;y2&quot;, data = dfAll, inherit = FALSE) %&gt;%
  64. style(showlegend = FALSE) %&gt;%
  65. layout(yaxis2 = yaxis2)
  66. # insert tooltips
  67. tlTips &lt;- paste0(&quot;Value: &quot;, c(df1$Val, df2$Val), &#39;\n&#39;,
  68. &quot;Date: &quot;, dfAll$Date, &#39;\n&#39;,
  69. &quot;ID: &quot;, dfAll$ID)
  70. for (i in seq_along(ply1$x$data)) {
  71. aName &lt;- ply1$x$data[[i]]$name
  72. if (!is.null(aName)) {
  73. aTags &lt;- grep(aName, tlTips, value = TRUE, fixed = TRUE)
  74. ply1$x$data[[i]]$text &lt;- aTags
  75. }
  76. }
  77. # display
  78. ply1
  79. ** **Broken case** with bars at right side
  80. # generate ggplot
  81. ggp2 &lt;- ggplot(dfAll) +
  82. geom_point(aes(x = Date, y = Val1, color = ID, group = ID), na.rm = TRUE) +
  83. geom_tile(aes(x = Date, y = (ylim1[1] + Val2_scl)/2, height = ylim1[1] - Val2_scl, fill = ID, group = ID),
  84. na.rm = TRUE, stat = &quot;identity&quot;, position = position_dodge(preserve = &quot;single&quot;)) +
  85. scale_y_continuous(trans = &quot;reverse&quot;,
  86. sec.axis = dup_axis(breaks = rev(brks2), labels = rev(labs2), name = &quot;Val2&quot;)) +
  87. coord_cartesian(ylim = rev(ylim1))
  88. # generate plotly
  89. ply2 &lt;- ggplotly(ggp2) %&gt;%
  90. add_lines(x = ~Date, y = ~Val2_scl, yaxis = &quot;y2&quot;, data = dfAll, inherit = FALSE) %&gt;%
  91. style(showlegend = FALSE) %&gt;%
  92. layout(yaxis2 = yaxis2)
  93. # insert tooltips
  94. for (i in seq_along(ply2$x$data)) {
  95. aName &lt;- ply2$x$data[[i]]$name
  96. if (!is.null(aName)) {
  97. t1 &lt;- grepl(&quot;(&quot;, aName, fixed = TRUE)
  98. t2 &lt;- grepl(&quot;,&quot;, aName, fixed = TRUE)
  99. t3 &lt;- grepl(&quot;)&quot;, aName, fixed = TRUE)
  100. if (all(t1, t2, t3)) {
  101. aName &lt;- strsplit(sub(&quot;(&quot;, &quot;&quot;, aName, fixed = TRUE), &quot;,&quot;, fixed = TRUE)[[1]][1]
  102. }
  103. aTags &lt;- grep(aName, tlTips, value = TRUE, fixed = TRUE)
  104. ply2$x$data[[i]]$text &lt;- aTags
  105. }
  106. }
  107. # display
  108. ply2
  109. [1]: https://i.stack.imgur.com/AkTqR.png
  110. [2]: https://i.stack.imgur.com/6Q29F.png
  111. [3]: https://stackoverflow.com/a/76310566/12372421
  112. </details>
  113. # 答案1
  114. **得分**: 2
  115. # Option 1:
  116. 这个修改修改了您用于修改工具提示的`for`语句。我使用了工具提示的*绘图*数据。我不确定这是否是您想要的瓷砖的值或ID,但至少您会看到我如何更改它。
  117. `lapply``if(`...跟踪是`markers`的部分开始。代码的这一部分与您的`for`语句中的内容几乎完全相同。请注意评论中唯一更改的地方(`&lt;&lt;`而不是`&lt;`)。
  118. `else(`...是如果跟踪不是标记(线/瓷砖)。
  119. `else`中,我提取文本,它看起来像这样:`&quot;Date: 2022-03-27&lt;br /&gt;(ylim1[1] + Val2_scl)/2: 166.8759&lt;br /&gt;ylim1[1] - Val2_scl: NA&lt;br /&gt;ID: J&lt;br /&gt;ID: J&quot;`
  120. 首先我按`&lt;br /&gt;`(换行符)拆分。然后第二个断点(`&quot;(ylim1[1] + Val2_scl)/2: 166.8759&quot;`)按`:`拆分。接下来,我按标记中使用的顺序重新构建提示(值,日期,然后ID)。
  121. #### 代码
  122. 这从您的问题中的对象`ggp2`开始。
  123. ```R
  124. #----------- 这部分与您的代码相同 ------------
  125. # 生成plotly图
  126. ply2 <- ggplotly(ggp2) %>%
  127. add_lines(x = ~Date, y = ~Val2_scl, yaxis = "y2", data = dfAll, inherit = FALSE) %>%
  128. style(showlegend = FALSE) %>%
  129. layout(yaxis2 = yaxis2)
  130. #----------- 我的修改开始的地方 -------------
  131. # 这在很大程度上与问题中的for()语句的内容基本相同
  132. invisible(lapply(1:length(ply2$x$data), function(i) {
  133. if(ply2$x$data[[i]]$mode == "markers") { # 所有内容都来自于您的for()语句
  134. aName <- ply2$x$data[[i]]$name
  135. if (!is.null(aName)) {
  136. t1 <- grepl("(", aName, fixed = TRUE)
  137. t2 <- grepl(",", aName, fixed = TRUE)
  138. t3 <- grepl(")", aName, fixed = TRUE)
  139. if (all(t1, t2, t3)) {
  140. aName <- strsplit(sub("(", "", aName, fixed = TRUE), ",", fixed = TRUE)[[1]][1]
  141. }
  142. aTags <- grep(aName, tlTips, value = TRUE, fixed = TRUE)
  143. ply2$x$data[[i]]$text <<- aTags # <--------- 这里有改动!
  144. }
  145. } else { # 否则模式不是markers(瓷砖/线)
  146. if(!is.null(ply2$x$data[[i]]$text)) { # 如果存在文本
  147. # 按换行符拆分当前多行字符串的工具提示
  148. aName <- strsplit(ply2$x$data[[i]]$text, "<br />", fixed = T)
  149. l2 <- strsplit(aName[[1]][2], ":") # 拆分 'Value' 行
  150. # 根据标记提示中的顺序重新构建瓷砖的工具提示
  151. # 和绘图数据
  152. aTag <- paste0("Value:", l2[[1]][2], "<br />", aName[[1]][1],
  153. "<br />", aName[[1]][length(aName[[1]])])
  154. ply2$x$data[[i]]$text <<- aTag # 注意 <<-
  155. }
  156. }
  157. }))
  158. ply2

[![enter image description here][1]][1]


Option 2:

感谢Kat的建议,我能够实现一种适用于geom_point() - geom_tile()geom_line() - geom_tile()组合的方法,它在每个条形上安装了正确的Value、Date和ID。Kat的意见对于开发我的版本非常重要,所以我决定将这段代码作为答案的附加部分添加,而不是发布新答案。

  • 为了简单起见,我回到了使用for循环。无论如何,这只涉及到几次迭代。
  • 这次我使用了geom_line()geom_tile()的组合来说明一种情况,其中ply2$x$data[[i]]$mode无法区分线和瓷砖跟踪。然而,该代码足够通用,也适用于geom_point()geom_tile()的组合使用。
  • 我准备了两个不同于条形的凹面和一个用于条形的凹面的工具提示向量。

代码

ggplot对象定义开始。

  1. # 生成ggplot图
  2. ggp2 <- ggplot(dfAll) +
  3. geom_line(aes(x = Date, y = Val1, color = ID, group = ID), na.rm = TRUE) +
  4. geom_tile(aes(x = Date, y = (ylim1[1] + Val2_scl)/2, height = ylim1[1] - Val2_scl, fill = ID, group = ID),
  5. na.rm = TRUE, stat = "identity", position = position_dodge(preserve = "single")) +
  6. scale_y_continuous(trans = "reverse",
  7. sec.axis = dup_axis(breaks = rev(brks2), labels = rev(labs2), name = "Val2")) +
  8. coord_cartesian(ylim = rev(ylim1))
  9. # 生成plotly图
  10. ply2 <- ggplotly(ggp2) %>%
  11. add_lines(x = ~Date, y = ~Val2_scl, yaxis = "y2", data = dfAll, inherit = FALSE) %>%
  12. style(showlegend = FALSE) %>%
  13. layout(yaxis2 = yaxis2)
  14. # 准备工具提示
  15. # - 用于与凹面和线条相关的其他图元
  16. tlTipsOthers <- paste0("Value: ", df1
  17. <details>
  18. <summary>英文:</summary>
  19. # Option 1:
  20. This modification modifies your `for` statement that you used to modify the tooltips. I used the *plotting* data for the tooltips. I&#39;m not sure if that is the value or ID you wanted for the tiles, but you&#39;ll at least see how I changed it.
  21. The `lapply` starts with `if(`...the trace is `markers`. This part of the code is mostly identical to the content in your `for` statement. Note the comment indicating the only thing I changed (`&lt;&lt;` instead of `&lt;`).
  22. The `else(`... is if the trace is *not* markers (lines/tiles).
  23. In the `else`, I extract the text, which looks like this: `&quot;Date: 2022-03-27&lt;br /&gt;(ylim1[1] + Val2_scl)/2: 166.8759&lt;br /&gt;ylim1[1] - Val2_scl: NA&lt;br /&gt;ID: J&lt;br /&gt;ID: J&quot;`.
  24. First I split by `&lt;br /&gt;` (linebreaks). Then the second break (`&quot;(ylim1[1] + Val2_scl)/2: 166.8759&quot;`) is split by the `:`. Next, I rebuild the tip in the order used in the markers (value, date, then id).
  25. #### The Code
  26. This starts with the object `ggp2` from your question.

#----------- this is unchanged from your code ------------

generate plotly

ply2 <- ggplotly(ggp2) %>%
add_lines(x = ~Date, y = ~Val2_scl, yaxis = "y2", data = dfAll, inherit = FALSE) %>%
style(showlegend = FALSE) %>%
layout(yaxis2 = yaxis2)

#----------- where my mods start -------------

this is mostly the same content as in you the question's for statement

invisible(lapply(1:length(ply2$x$data), function(i) {
if(ply2$x$data[[i]]$mode == "markers") { # all in if is from your for() statement
aName <- ply2$x$data[[i]]$name
if (!is.null(aName)) {
t1 <- grepl("(", aName, fixed = TRUE)
t2 <- grepl(",", aName, fixed = TRUE)
t3 <- grepl(")", aName, fixed = TRUE)

  1. if (all(t1, t2, t3)) {
  2. aName &lt;- strsplit(sub(&quot;(&quot;, &quot;&quot;, aName, fixed = TRUE), &quot;,&quot;, fixed = TRUE)[[1]][1]
  3. }
  4. aTags &lt;- grep(aName, tlTips, value = TRUE, fixed = TRUE)
  5. ply2$x$data[[i]]$text &lt;&lt;- aTags # &lt;--------- this is changed!
  6. }

} else { # else the mode is NOT markers (tiles/lines)
if(!is.null(ply2$x$data[[i]]$text)) { # if text exists
# split the current multiple stringed tooltip by line break
aName <- strsplit(ply2$x$data[[i]]$text, "<br />", fixed = T)
l2 <- strsplit(aName[[1]][2], ":") # split 'Value' line

  1. # rebuild tooltip for tiles based on the order in markers tips
  2. # and plotting data
  3. aTag &lt;- paste0(&quot;Value:&quot;, l2[[1]][2], &quot;&lt;br /&gt;&quot;, aName[[1]][1],
  4. &quot;&lt;br /&gt;&quot;, aName[[1]][length(aName[[1]])])
  5. ply2$x$data[[i]]$text &lt;&lt;- aTag # note the &lt;&lt; for envir assign
  6. }

}
}))
ply2

  1. [![enter image description here][1]][1]
  2. ------------------------------------------
  3. # Option 2:
  4. *Thanks to Kat&#39;s suggestions, I was able to implement an approach which (i) works for combinations of `geom_point()` - `geom_tile()` and `geom_line()` - `geom_tile()`; and (ii) installs the correct Value, Date and ID on each bar. Kat&#39;s input was fundamental to have my version developed so I decided it was better to add this code as an addition to this answer rather than posting a new one.*
  5. - I got back to a for loop for simplicity. Anyway it only involves a few iterations.
  6. - This time I used `geom_line()` in combination with `geom_tile()` to illustrate a case where `ply2$x$data[[i]]$mode` doesn&#39;t let one differentiate between lines and tiles traces. However, the code is general enough to work also if `geom_point()` is used in combination with `geom_tile()`.
  7. - I prepared two separate tooltip vectors, one for other geoms different to bars and one for bars.
  8. #### The Code
  9. Starting from the `ggplot` object definition.
  10. # generate ggplot
  11. ggp2 &lt;- ggplot(dfAll) +
  12. geom_line(aes(x = Date, y = Val1, color = ID, group = ID), na.rm = TRUE) +
  13. geom_tile(aes(x = Date, y = (ylim1[1] + Val2_scl)/2, height = ylim1[1] - Val2_scl, fill = ID, group = ID),
  14. na.rm = TRUE, stat = &quot;identity&quot;, position = position_dodge(preserve = &quot;single&quot;)) +
  15. scale_y_continuous(trans = &quot;reverse&quot;,
  16. sec.axis = dup_axis(breaks = rev(brks2), labels = rev(labs2), name = &quot;Val2&quot;)) +
  17. coord_cartesian(ylim = rev(ylim1))
  18. # generate plotly
  19. ply2 &lt;- ggplotly(ggp2) %&gt;%
  20. add_lines(x = ~Date, y = ~Val2_scl, yaxis = &quot;y2&quot;, data = dfAll, inherit = FALSE) %&gt;%
  21. style(showlegend = FALSE) %&gt;%
  22. layout(yaxis2 = yaxis2)
  23. # prepare tooltips
  24. # - for geom_point and geom_line related traces
  25. tlTipsOthers &lt;- paste0(&quot;Value: &quot;, df1$Val, &#39;\n&#39;,
  26. &quot;Date: &quot;, df1$Date, &#39;\n&#39;,
  27. &quot;ID: &quot;, df1$ID)
  28. # - for geom_tiles related traces
  29. tlTipsTiles &lt;- paste0(&quot;Value: &quot;, df2$Val, &#39;\n&#39;,
  30. &quot;Date: &quot;, df2$Date, &#39;\n&#39;,
  31. &quot;ID: &quot;, df2$ID)
  32. # insert tooltips
  33. tilePivs &lt;- c()
  34. for (i in seq_along(ply2$x$data)) { # tooltips for traces other than tiles
  35. # i &lt;- 0
  36. # i &lt;- i + 1
  37. aName &lt;- ply2$x$data[[i]]$name
  38. if (!is.null(aName)) {
  39. t1 &lt;- grepl(&quot;(&quot;, aName, fixed = TRUE)
  40. t2 &lt;- grepl(&quot;,&quot;, aName, fixed = TRUE)
  41. t3 &lt;- grepl(&quot;)&quot;, aName, fixed = TRUE)
  42. if (all(t1, t2, t3)) {
  43. aName &lt;- strsplit(sub(&quot;(&quot;, &quot;&quot;, aName, fixed = TRUE), &quot;,&quot;, fixed = TRUE)[[1]][1]
  44. }
  45. # determine if this is a geom_tile() trace, else insert others&#39; tooltips
  46. if (any(grepl(aName, tlTipsTiles, fixed = TRUE)) &amp;&amp; ply2$x$data[[i]]$hoveron == &quot;fills&quot;) {
  47. tilePivs &lt;- c(tilePivs, i)
  48. } else {
  49. aTags &lt;- grep(aName, tlTipsOthers, value = TRUE, fixed = TRUE)
  50. ply2$x$data[[i]]$text &lt;- aTags
  51. }
  52. }
  53. }
  54. for (i in seq_along(tilePivs)) { # tooltips for tiles traces
  55. ply2$x$data[[tilePivs[i]]]$text &lt;- tlTipsTiles[[i]]
  56. }
  57. # display
  58. ply2
  59. [![lines tooltip ok][2]][2]
  60. [![tiles tooltip ok][3]][3]
  61. [1]: https://i.stack.imgur.com/yD9MY.png
  62. [2]: https://i.stack.imgur.com/JvZ5C.png
  63. [3]: https://i.stack.imgur.com/8AUwe.png
  64. </details>

huangapple
  • 本文由 发表于 2023年6月14日 23:53:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定