【问题标题】:Static polygons in plotly? Is it possible?静态多边形?可能吗?
【发布时间】:2018-10-19 00:16:36
【问题描述】:

我一直在尝试创建一个图,其中有一些我想与悬停信息等交互的点,但我想包含两个没有任何交互内容的多边形区域。

对于 MWE:

library(plotly)
data("iris")
xsq <- function(x) sqrt(x)
x <- c( c(seq(0,10,0.001),0,0), c(0.5,10,10,0.5))
y <- c( c(xsq(x[1:length(seq(0,10,0.001))]),xsq(max(x)),0), c(0,2,0,0))

## produce the plotly plot
plot_ly(x = x, y = y, alpha = 0.1, opacity=0.1) %>%
  add_polygons(hoverinfo = "none", color = I("red"),showlegend=F) %>%
  add_polygons(x = c(0.5,10,10,0.5), y=c(0,2,0,0), hoverinfo = "none", 
               color = I("blue"), showlegend=F) %>%
  add_markers(x=iris$Sepal.Length,y=iris$Sepal.Width-2,  opacity=1, alpha=1,
              color=iris$Species, hoverinfo="text", text=iris$Species)

这有点给了我想要的东西,但我有几个问题:

  1. 未显示多边形区域中点的悬停信息
  2. 如果我选择一个组,那么这两个多边形也会从视图中消失。即使只选择一组点,我也希望始终保持多边形存在

我也尝试使用 ggplot,但没有任何运气。

基本上我认为我想要的是一种将两个静态多边形添加到情节图的方法。有没有人有其他建议/想法?

谢谢。

【问题讨论】:

标签: r r-plotly


【解决方案1】:

所以经过一些广泛的搜索和实验,我想出了如何解决问题 1。多边形层需要包含hoveron="points"。例如,

plot_ly(x = x, y = y, alpha = 0.1, opacity=0.1) %>%
  add_polygons(hoverinfo = "none", color = I("red"),showlegend=F, hoveron="points") %>%
  add_polygons(x = c(0.5,10,10,0.5), y=c(0,2,0,0), hoverinfo = "none", 
               color = I("blue"), showlegend=F, hoveron="points") %>%
  add_markers(x=iris$Sepal.Length,y=iris$Sepal.Width-2,  opacity=1, alpha=1,
              color=iris$Species, hoverinfo="text", text=iris$Species)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    相关资源
    最近更新 更多