plotly / plotly/plotly.R

Solution to combine facetted legends breaks for continuous variable

Open
#2,112 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

I am trying to use plotly to create facetted plots - I do this by using group_map() to create my plots robustly.

The solution I currently have works in reducing duplicated legend labels when using discrete variables for color/size/etc but fail to work for continuous variables. I assume this is do to each facetted plot potentially having different total ranges for the continuous variable.

library(tidyverse)
library(plotly)

city_list = unique(txhousing$city) %>%  
  sample(8)

##solution works for discrete labels 
txhousing %>%  
  filter(month < 6, 
         city %in% city_list) %>% 
  group_by(month) %>% 
  group_map(~{
    plot_ly(.x, x=~year, y = ~sales, color = ~city,legendgroup = ~city,type = "bar"
            ,showlegend = (.y == "1")
            ) %>%
      layout(yaxis = list(
        title = paste0(c(rep("&nbsp;", 20),
                         paste("<b>", as.character(.y), "</b>"),
                         rep("&nbsp;", 20),
                         rep("\n&nbsp;", 3)),
                       collapse = ""))) %>%
      layout(barmode = 'stack')
  }
  ) %>%
  subplot(nrows = NROW(.), margin = .05, shareX = T, shareY = T, titleY = T) 

##solution fails to merge legends for continuous variable
txhousing %>%  
  filter(month < 6, 
         city %in% city_list) %>% 
  group_by(month) %>% 
  group_map(~{
    plot_ly(.x, x=~year, y = ~sales, color = ~volume,legendgroup = ~volume,type = "bar"
            ,showlegend = (.y == "1")
            ) %>%
      layout(yaxis = list(
        title = paste0(c(rep("&nbsp;", 20),
                         paste("<b>", as.character(.y), "</b>"),
                         rep("&nbsp;", 20),
                         rep("\n&nbsp;", 3)),
                       collapse = ""))) %>%
      layout(barmode = 'stack')
  }
  ) %>%
  subplot(nrows = NROW(.), margin = .05, shareX = T, shareY = T, titleY = T) 

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the two supplied R reproductions using plot_ly(), group_map(), and subplot(), then compare the discrete and continuous color cases. Trace how subplot combines traces and legends; done means continuous-variable legends are combined without breaking the facetted plot behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.