使用plotly改变鼠标悬停时的geom_point大小

问题描述 投票:0回答:1

我编写了以下内容,其中背景中有图像(必要)和一些数据点:

library(plotly)
library(ggplot2)
library(lorem)

df <- data.frame(
  id = 1:50,
  value = rnorm(50)
)

# background image (necessary)
image_destination <- "https://i.imgur.com/ojTcMtN.jpeg"

gg <- ggplot(df,
             aes(x = id,
                 y = value,
                 text = ipsum_words(50, collapse = F) 
             )
) +
  geom_point(color = "red", alpha = 0.5, size = 5) +
  theme_bw()


# create plot
pp <- ggplotly(gg,
               tooltip = c("text"))

# display plot
plotly::layout(pp,
               # tooltip = c("text"),
               images = list(
                 list(
                   source =  image_destination,
                   xref = "x",
                   yref = "y",
                   x = 0,
                   y = 2,
                   sizex = 100,
                   sizey = 4,
                   sizing = "stretch",
                   opacity = 1,
                   layer = "below"
                 )
               ))

有没有办法操纵

plotly
,使得 geom_point
size鼠标悬停时增加(例如,从当前大小 5 增加到大小 10)?

r ggplot2 plotly mouseover ggplotly
1个回答
0
投票

您可以使用两个自定义事件处理程序来实现您想要的结果,如文档中所述。使用

plotly_hover
事件您可以增加悬停点的大小,使用
plotly_unhover
您可以将其设置回默认值。在这两种情况下,这都是通过
Plotly.restyle
实现的。但是,使用
Plotly.restyle
我们只能设置或更改迹线的标记样式。因此,要设置一个点的标记样式需要将每个点视为单个迹线。为此,我将
factor(id)
映射到
color
中的
ggplot()
aes 上。因为这将添加颜色图例,所以我在
showlegend = FALSE
中添加了
layout
,因为设置
guide="none"
不由
plotly
解释。最后,由于用于尺寸的单位与
ggplot2
不同,在绘图中获取或设置正确的尺寸需要转换
ggplot2
尺寸,这可以使用
Size_in_mm * 96 / 25.4

来实现

注意:由于图像部分与该问题无关,因此我将其删除以使示例更加简洁。

library(plotly)
library(ggplot2)
library(lorem)

set.seed(123)

df <- data.frame(
  id = 1:50,
  value = rnorm(50),
  text = ipsum_words(50, collapse = FALSE)
)

gg <- ggplot(
  df,
  aes(
    x = id,
    y = value,
    text = text,
    color = factor(id)
  )
) +
  geom_point(alpha = 0.5, size = 5) +
  scale_color_manual(
    values = rep("red", nrow(df)), guide = "none"
  ) +
  theme_bw()


# create plot
pp <- ggplotly(gg,
  tooltip = c("text")
)

# display plot
pp <- plotly::layout(pp,
  showlegend = FALSE
) 

pp |>
  htmlwidgets::onRender("
    function(el) {
      el.on('plotly_hover', function(data) {
        console.log(data);
    
        var pn='',
            tn='',
            colors=[];
        
        for(var i=0; i < data.points.length; i++){
          pn = data.points[i].pointNumber;
          tn = data.points[i].curveNumber;
          colors = data.points[i].data.marker.color;
        };
        var size = 10 * 96 / 25.4;
    
        var update = {'marker':{color: colors, size: size, opacity: 1}};
        Plotly.restyle(el.id, update, [tn]);
      });
      el.on('plotly_unhover', function(data) {
        var pn='',
            tn='',
            colors=[];
        
        for(var i=0; i < data.points.length; i++){
          pn = data.points[i].pointNumber;
          tn = data.points[i].curveNumber;
          colors = data.points[i].data.marker.color;
        };
        var size = 5 * 96 / 25.4;
    
        var update = {'marker':{color: colors, size: size, opacity: .5}};
        Plotly.restyle(el.id, update, [tn]);
      });
    }
  ")

© www.soinside.com 2019 - 2024. All rights reserved.