Plotly R:突出显示饼图中的悬停标签,并灰色其他标签

发布于 2025-01-23 18:53:46 字数 499 浏览 4 评论 0 原文

DataFrame:

df2 = data.frame(value = c(9, 2, 7, 3, 6),
                 key = c('ar', 'or', 'br', 'gt', 'ko'))

这是生成饼图的代码:

df2 %>% 
  plot_ly(labels = ~key,
          values = ~value,
          type = 'pie')

基本上,我想突出显示我正在悬停并灰色的标签,例如这些图: 1st plot

有办法这样做吗?

Dataframe:

df2 = data.frame(value = c(9, 2, 7, 3, 6),
                 key = c('ar', 'or', 'br', 'gt', 'ko'))

And this is the code to generate the pie chart:

df2 %>% 
  plot_ly(labels = ~key,
          values = ~value,
          type = 'pie')

Basically, I would like to highlight the label that I'm hovering over and grey out the other labels, like these plots: 1st plot and 2nd plot.

Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま昔日黯然 2025-01-30 18:53:46

我使用了一个灰色和默认的颜色数组,并将您悬停在所有灰色数组中的替代品中使用的点,然后将其作为 plotly_restyle 发送到图形。如果您熟悉 updatemenus 适用于按钮的方法,那么它本质上是相同的编码,仅在JS而不是

library(htmlwidgets)
library(plotly)

df2 = data.frame(value = c(9, 2, 7, 3, 6),
                 key = c('ar', 'or', 'br', 'gt', 'ko'))


hoverer = "function(el, x) {
            el.on('plotly_hover', function(d){
              var pn;
              var oCol = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd'];
              var nCol = ['gray','gray','gray','gray','gray'];
              pn = d.points[0].pointNumber;
              nCol[pn] = oCol[pn];
              var update = {marker:{colors: nCol}};
              Plotly.restyle(el.id, update);
            });
            el.on('plotly_unhover', function(d){
              var oCol = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd'];
              var update = {marker:{colors: oCol}};
              Plotly.restyle(el.id, update);
            });}"


df2 %>% 
  plot_ly(labels = ~key,
          values = ~value,
          # default colors; matches JS
          marker = list(colors = list('#1f77b4',  # muted blue
                                      '#ff7f0e',  # safety orange
                                      '#2ca02c',  # cooked asparagus green
                                      '#d62728',  # brick red
                                      '#9467bd')),# muted purple))
          type = 'pie') %>% onRender(hoverer) # this is from htmlwidgets library

R。 “ rel =“ nofollow noreferrer”> “

I used a gray and default color array and used the point you hover on as a replacer in the array of all gray colors, then it's sent to the graph as a plotly_restyle. If you're familiar with how updatemenus works for buttons, it's essentially the same coding, just in JS instead of R.

library(htmlwidgets)
library(plotly)

df2 = data.frame(value = c(9, 2, 7, 3, 6),
                 key = c('ar', 'or', 'br', 'gt', 'ko'))


hoverer = "function(el, x) {
            el.on('plotly_hover', function(d){
              var pn;
              var oCol = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd'];
              var nCol = ['gray','gray','gray','gray','gray'];
              pn = d.points[0].pointNumber;
              nCol[pn] = oCol[pn];
              var update = {marker:{colors: nCol}};
              Plotly.restyle(el.id, update);
            });
            el.on('plotly_unhover', function(d){
              var oCol = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd'];
              var update = {marker:{colors: oCol}};
              Plotly.restyle(el.id, update);
            });}"


df2 %>% 
  plot_ly(labels = ~key,
          values = ~value,
          # default colors; matches JS
          marker = list(colors = list('#1f77b4',  # muted blue
                                      '#ff7f0e',  # safety orange
                                      '#2ca02c',  # cooked asparagus green
                                      '#d62728',  # brick red
                                      '#9467bd')),# muted purple))
          type = 'pie') %>% onRender(hoverer) # this is from htmlwidgets library

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文