You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R语言的ECharts图例中隐藏单个系列(图表仍显示该系列)

解决echarts4r中隐藏图例指定系列但保留图表显示的问题

要实现隐藏图例中的series2但保留该系列在图表中显示,核心是在e_legend()函数中通过data参数指定需要在图例中展示的系列名称(排除series2),同时可结合selected参数控制系列的默认显示状态。

修改后的R代码如下:

library(echarts4r)
library(dplyr)

dataset <- data.frame(id = 1:5, value1 = seq(10, 50, length.out = 5), value2 = seq(200, 500, length.out = 5), value3 = seq(100, 250, length.out = 5))

dataset %>%
  e_charts(x = id) %>%
  e_line(serie = value1, name = "series1") %>%
  e_line(serie = value2, name = "series2") %>%
  e_line(serie = value3, name = "series3") %>%
  e_legend(
    data = c("series1", "series3"), # 仅指定要显示在图例中的系列
    selected = list("series3" = FALSE)
  )

关键说明

  • data参数接受字符向量,对应JavaScript方案中的legend.data数组,只传入需要展示图例的系列名称,就能隐藏未指定的系列图例(但该系列仍会在图表中正常渲染)。
  • selected参数用于控制系列的默认显示状态,这里设置series3默认隐藏,不影响series2的图表展示逻辑。

内容的提问来源于stack exchange,提问作者eastclintw00d

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 22:12:36