首页 > 解决方案 > 如何在串扰过滤器后突出显示()线图

问题描述

highlight()我正在使用 R 使用和串扰filter_select()功能制作交互式绘图。但是,当colorplotly 的 lines 的参数“” matchhighlight_key()时,无法突出显示图中过滤数据的任何光线。

假设你拿起 Carson Sievert 的“使用 R、plotly 和闪亮的交互式 web 数据可视化”中的示例,在第 16.1 章“图形查询”的图 16.10 书中提供的示例工作正常,您可以过滤数据后突出显示一个项目:

library(gapminder)
g <- highlight_key(gapminder, ~country)
continent_filter <- filter_select("filter", "Select a country", g, ~continent)

p <- plot_ly(g) %>%
  group_by(country) %>%
  add_lines(x = ~year, y = ~lifeExp, color = ~continent) %>%
  layout(xaxis = list(title = "")) %>%
  highlight(selected = attrs_selected(showlegend = FALSE))

bscols(continent_filter, p, widths = 12)

在此处输入图像描述

但是,当“ color”参数add_lines()匹配highlight_key()变量(在这种情况下,~country)时,无法过滤并突出显示过滤后的数据

library(gapminder)
g <- highlight_key(gapminder, ~country)
continent_filter <- filter_select("filter", "Select a country", g, ~continent)

p <- plot_ly(g) %>%
  group_by(country) %>%
  # Please mind that in the next line IS NOT "color = ~continent" as in the last example
  add_lines(x = ~year, y = ~lifeExp, color = ~country) %>%
  layout(xaxis = list(title = "")) %>%
  highlight(selected = attrs_selected(showlegend = FALSE))

bscols(continent_filter, p, widths = 12)

在此处输入图像描述

当 plotly 绘图的颜色参数与 highlight_key 匹配时,是否可以在交互式 plot_ly 线图中突出显示项目(因为我已经检查了箱线图并且它有效)?我也尝试过 geom_line() + ggplotly() 但也不起作用

标签: rplotlydata-visualizationr-plotlyhtmlwidgets

解决方案


推荐阅读