在情节图例中单独的符号和颜色 [英] Separate symbol and color in plotly legend

查看:14
本文介绍了在情节图例中单独的符号和颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 plotly 实现与这个 ggplot 代码相同的结果:

I want to achieve the same result as this ggplot code with plotly:

mtcars %>% add_rownames('car') %>% 
ggplot(aes(x = mpg,
         y = disp,
         color = as.factor(gear),
         shape = as.factor(cyl))) +
geom_point()

导致:

我的情节代码是:

library(dplyr)
mtcars %>% add_rownames('car') %>% 
plot_ly(x = ~mpg, 
      y = ~disp,
      text = ~car,
      color = ~as.factor(gear),
      symbol = ~as.factor(cyl),
      mode = 'markers')

枚举图例中所有可能的颜色和形状组合.

which enumerates all possible combinations of colors and shapes in the legend.

有没有办法与 ggplot 有类似的图例?

Is there a way to have a similar legend to the ggplot?

推荐答案

更新:为了克服我之前解决方案中提到的一些问题(见下文)并增加图例的可用性,可以简单地将列名添加到图例描述中,然后将图例组分配给每个类别.

UPDATE: To overcome some of the issues mentioned for my previous solution (see below) and to increase the usability of the legend, one can simply add the column name to the legend description and then assign the legendgroups to each category.

mtcars %>% rownames_to_column('car') %>%
    plot_ly() %>%
    #Plot symbols for cyl
    add_trace(type = "scatter",
              x = ~mpg, 
              y = ~disp,
              text = ~car,
              symbol = ~paste0(cyl," cyl."),
              mode = 'markers',
              marker = list(color = "grey", size = 15)) %>%
    #Overlay color for gears
    add_trace(type = "scatter",
              x = ~mpg, 
              y = ~disp,
              text = ~car,
              color = ~paste0(gear, " gears"),
              mode = 'markers')

这是以前的解决方案,在视觉上更接近 ggplot2 等效项:

This is the previous solution, which is visually closer to the ggplot2 equivalent:

基于 这个线程,我们可以手动创建汽缸gears的组.随后,通过 Artem Sokolov this thread 的回答,我们可以将图例标题添加为注释.

Based on the answer of dww in this thread, we can manually create the groups for cylinders and gears. Subsequently, with the answer of Artem Sokolov this thread, we can add the legend titles as annotations.

mtcars %>% rownames_to_column('car') %>% 
  plot_ly() %>%
  #Plot symbols for cyl
  add_trace(type = "scatter",
            x = ~mpg, 
            y = ~disp,
            text = ~car,
            symbol = ~as.factor(cyl),
            mode = 'markers',  
            legendgroup="cyl",
            marker = list(color = "grey", size = 15)) %>%
  #Overlay color for gears
  add_trace(type = "scatter",
            x = ~mpg, 
            y = ~disp,
            text = ~car,
            color = ~as.factor(gear),
            mode = 'markers', 
            legendgroup="gear") %>%
  #Add Legend Titles (manual)
  add_annotations( text="Cylinders:", xref="paper", yref="paper",
                   x=1.02, xanchor="left",
                   y=0.9, yanchor="bottom",    # Same y as legend below
                   legendtitle=TRUE, showarrow=FALSE ) %>%

  add_annotations( text="Gears:", xref="paper", yref="paper",
                   x=1.02, xanchor="left",
                   y=0.7, yanchor="bottom",    # Y depends on the height of the plot
                   legendtitle=TRUE, showarrow=FALSE ) %>%

  #Increase distance between groups in Legend
  layout(legend=list(tracegroupgap =30, y=0.9, yanchor="top")) 

未解决的问题:

  • 必须手动创建组
  • 组只是重叠(颜色超过形状).这意味着在图例中只能禁用/激活整个组(例如,不能只显示具有 4 个气缸的条目)
  • 第二个图例标题(注解)的位置取决于图的高度!

这篇关于在情节图例中单独的符号和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆