如何在使用 split 参数时在 plot_ly 中创建和格式化图例

How to create & format legend in plot_ly while using the split parameter

我正在尝试使用 split 参数在 plot_ly 中创建一个散点图,但是我在图例和格式方面遇到了一些困难。具体来说,一旦我将标签添加到我的图表中,更多的项目就会添加到图例中,颜色不匹配,而且我无法通过单击关闭整个系列。这是一个例子:

library(plotly)

plot_ly(mtcars,
        type = "scatter",
        x = ~hp,
        y =~qsec,
        split = ~cyl,
        text = rownames(mtcars)) %>%
  add_text(textposition = "middle right")

当您 运行 这段代码时,您可以看到标签确实出现在每个点上,但是它们以随机颜色显示。此外,图例有 6 个项目。我想要的是:

  1. 标签的颜色应与 series/split(编号 气缸数)
  2. 图例有 3 个项目(4、6 和 8)
  3. 当你点击 在图例中的一项上,它应该 off/on BOTH series/split
  4. 的标记和文本标签

非常感谢您的帮助!

如果有帮助:

使用类型scatter时需要设置mode。使用 markers+text 选项,我相信您无法单独为文本着色。如果你不介意文字变灰,解决方案是:

plot_ly(mtcars,
        type = "scatter",
        x = ~hp,
        y = ~qsec,
        split = ~cyl,
        mode = "markers+text",
        text = rownames(mtcars),
        textposition = "middle right") 

如果你想让它 100% 匹配你的请求,它会变得更复杂,你不能使用 split 参数。

您必须为每个柱面级别创建单独的跟踪,首先使用 mutate 明确包含行名称。然后,您使用 filter 为每个柱面级别设置 mtcars 的子集。为每个级别创建 2 个轨迹、一个标记和一个文本。 color 将等于 cyl 的因子水平。最后,您必须对图例中每个圆柱体的轨迹进行分组,隐藏您不需要的条目。

library(plotly)
library(dplyr)    

plot_ly(filter(mutate(mtcars, names = rownames(mtcars)), cyl == 4),
        type = "scatter",
        x = ~hp,
        y = ~qsec,
        mode = "markers",
        color = ~factor(cyl, c(4,6,8)),
        legendgroup = '4',
        name = '4',
        textposition = "middle right") %>% 
  add_trace(data = filter(mutate(mtcars, names = rownames(mtcars)), cyl == 4),
            type = "scatter",
            x = ~hp+2,
            y = ~qsec,
            color = ~factor(cyl, c(4,6,8)),
            mode = "text",
            text = ~names, 
            legendgroup = '4',
            name = '4',
            showlegend = FALSE,
            textposition = "middle right") %>% 
  add_trace(data = filter(mutate(mtcars, names = rownames(mtcars)), cyl == 6),
          type = "scatter",
          x = ~hp,
          y = ~qsec,
          mode = "markers",
          color = ~factor(cyl, c(4,6,8)),
          legendgroup = '6',
          name = '6',
          textposition = "middle right") %>% 
  add_trace(data = filter(mutate(mtcars, names = rownames(mtcars)), cyl == 6),
            type = "scatter",
            x = ~hp+2,
            y = ~qsec,
            mode = "text",
            color = ~factor(cyl, c(4,6,8)),
            text = ~names, 
            legendgroup = '6',
            name = '6',
            showlegend = FALSE,
            textposition = "middle right") %>% 
  add_trace(data = filter(mutate(mtcars, names = rownames(mtcars)), cyl == 8),
            type = "scatter",
            x = ~hp,
            y = ~qsec,
            mode = "markers",
            color = ~factor(cyl, c(4,6,8)),
            legendgroup = '8',
            name = '8',
            textposition = "middle right") %>% 
  add_trace(data = filter(mutate(mtcars, names = rownames(mtcars)), cyl == 8),
            type = "scatter",
            x = ~hp+2,
            y = ~qsec,
            mode = "text",
            color = ~factor(cyl, c(4,6,8)),
            text = ~names, 
            legendgroup = '8', 
            name = '8',
            showlegend = FALSE,
            textposition = "middle right")