图例中的符号和颜色分开 [英] Separate symbol and color in plotly legend

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

问题描述

我希望通过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:

基于dww在此线程的答案,我们可以添加图例标题作为注释.

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天全站免登陆