简体   繁体   English

用于更改散点图中数据颜色属性的下拉菜单 plot (Plotly R)

[英]Dropdown menu for changing the color attribute of data in scatter plot (Plotly R)

I am trying to create a plotly graph with selectable color attribute so that passing the selected categorical data column as color variable, it changes the color of marks as well as the legend of my scatter plot.我正在尝试创建一个具有可选颜色属性的 plotly 图,以便将选定的分类数据列作为颜色变量传递,它会更改标记的颜色以及我的散点图 plot 的图例。

Here is the Example:这是示例:

df <- data.frame(x = runif(200), y = runif(200), 
                 z = sample(c("a", "b", "c"), 200, replace=TRUE),
                 w = sample(c("d", "e", "f",'g'), 200, replace=TRUE))
p <- plot_ly(df, x = ~x)%>%
  add_markers(y = ~y, color = ~z,visible=T)%>%
  layout(
    title = "Drop down menus - color",
    xaxis = list(domain = c(0.1, 1)),
    yaxis = list(title = "y"),
    updatemenus = list(
      list(
        y = 0.7,
        buttons = list(
          list(method = "restyle",
               args = list("color", list(~z)),  
               label = "group by z"),
          list(method = "restyle",
               args = list("color", list(~w)),  
               label = "group by w")))
    ))

在此处输入图像描述

However, switching between the two options, the plot does not change.但是,在两个选项之间切换,plot 不会改变。 Apparently, we could change any data attribute with dropdown events except the color.显然,我们可以使用下拉事件更改任何数据属性,除了颜色。 Any help would be appreciated.任何帮助,将不胜感激。

Unfortunately I don't believe this is possible with solely using plotly :不幸的是,我不相信仅使用plotly是不可能的:

Per a github issue :根据github 问题

color / symbol / linetype / size /etc mapping (from data values to color codes) happen on the R side of things, so once the plot object gets passed to the browser, it loses information about those mapping(s), so I'm afraid something like this won't be possible...unless you do the mapping yourself color / symbol / linetype / size /等映射(从数据值到颜色代码)发生在 R 方面,所以一旦 plot ZA8CFDE6331BD59EB2AC96F8911IC4B666Z 被传递给浏览器,它就会丢失有关这些映射的信息恐怕这样的事情是不可能的......除非你自己做映射

Alternatives备择方案

This seems to be a nice use case for shiny .这似乎是shiny的一个很好的用例。 There is a stack overflow question that shows you these steps on creating a menu outside of plotly, and using the selection to re-render the plot.有一个堆栈溢出问题,向您展示了在 plotly 之外创建菜单并使用选择重新呈现 plot 的这些步骤。

Additionally, you can change the color of the plot by using plotly.js and HTML.此外,您可以使用 plotly.js 和 HTML 更改 plot 的颜色。 You would have to add select elements to a HTML page, and add event listeners to call Plotly.newPlot() on update.您必须将 select 元素添加到 HTML 页面,并添加事件侦听器以在更新时调用Plotly.newPlot() I believe shiny is an easier solution as it's a bit more of an R-centric approach.我相信 shiny 是一个更简单的解决方案,因为它更像是一种以 R 为中心的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM