简体   繁体   English

在图解图例中分隔符号和颜色

[英]Separate symbol and color in plotly legend

I want to achieve the same result as this ggplot code with plotly: 我希望通过plotly获得与此ggplot代码相同的结果:

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

which results in: 这导致: ggplot版本

My plotly code is: 我的情节代码是:

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. 它列举了图例中颜色和形状的所有可能组合。 在此输入图像描述

Is there a way to have a similar legend to the ggplot? 有没有办法让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')


This is the previous solution, which is visually closer to the ggplot2 equivalent: 这是以前的解决方案,它在视觉上更接近ggplot2等价物:

Based on the answer of dww in this thread , we can manually create the groups for cylinders and gears . 基于此线程中dww的答案,我们可以手动创建cylindersgears Subsequently, with the answer of Artem Sokolov this thread , we can add the legend titles as annotations. 随后,通过Artem Sokolov 这个帖子的答案,我们可以添加图例标题作为注释。

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")) 

Unsolved issues: 未解决的问题:

  • Groups have to be created manually 必须手动创建组
  • Groups are just overlayed (color over shape). 组刚刚重叠(颜色超过形状)。 This means that only the whole group can be dis-/activated in the legend (eg, it is not possible to only show only the entries with 4 cylinders) 这意味着只能在图例中禁用/激活整个组(例如,不能仅显示具有4个柱面的条目)
  • The position of the second legend title (annotation) depends on the height of the plot! 第二个图例标题(注释)的位置取决于图的高度!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM