In R plotly subplot graph, how to show only one legend?

The above answer results in a minor problem. The legend is only interactive with the first plot. You need to add the legendgroup to the plot_ly function to make the legend interactive with both plots.

library(plotly)
p1 <-
  iris%>%
  group_by(Species)%>%
  plot_ly(x=~Sepal.Length, color= ~Species, legendgroup=~Species)%>%
  add_markers(y= ~Sepal.Width)
p2 <-
  iris%>%
  group_by(Species)%>%
  plot_ly(x=~Sepal.Length, color= ~Species, legendgroup=~Species)%>%
  add_markers(y= ~Sepal.Width, showlegend=F)
subplot(p1,p2)

There seem to be some uncertain points in the answers given until now.

First of all data frame grouping hasn't any influence as far as I can see it. It is a question of sorting not grouping (as Maltas comment above indicates). So the data frame must sorted by the variable which is intended to serve as grouping variable. But there is another pitfall which nevertheless prevents the code working. Besides the required legendgroup you have therefore to ensure

  1. that your data frame is sorted by your grouping variable (fortunately iris data are already sorted by Species),
  2. that the variables you are using don't contain missing values (NAs).

Thus this should work:

library(plotly)
p1 <-
  iris %>%
  arrange(Species) %>%
  plot_ly(x = ~Sepal.Length, color = ~Species, legendgroup = ~Species) %>%
  add_markers(y = ~Sepal.Width)
p2 <-
  iris %>%
  arrange(Species) %>%
  plot_ly(x = ~Sepal.Length, color = ~Species, legendgroup = ~Species) %>%
  add_markers(y= ~Sepal.Width, showlegend = FALSE)

subplot(p1, p2)

The following examples do not work:

  1. Sorted by wrong variable:

    p1 <-
      iris %>%
      arrange(Sepal.Length) %>%
      plot_ly(x = ~Sepal.Length, color = ~Species, legendgroup = ~Species) %>%
      add_markers(y = ~Sepal.Width)
    p2 <-
      iris%>%
      arrange(Sepal.Length) %>%
      plot_ly(x=~Sepal.Length, color= ~Species, legendgroup=~Species)%>%
      add_markers(y = ~Sepal.Width, showlegend = FALSE)
    
    subplot(p1, p2)
    
  2. Variables with missing values:

    df <- iris
    df$Sepal.Length[2] <- NA
    head(df)
    
    #>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    #> 1          5.1         3.5          1.4         0.2  setosa
    #> 2           NA         3.0          1.4         0.2  setosa
    #> 3          4.7         3.2          1.3         0.2  setosa
    #> 4          4.6         3.1          1.5         0.2  setosa
    #> 5          5.0         3.6          1.4         0.2  setosa
    #> 6          5.4         3.9          1.7         0.4  setosa
    
    p1 <-
      df %>%
      arrange(Species) %>%
      plot_ly(x = ~Sepal.Length, color = ~Species, legendgroup = ~Species) %>%
      add_markers(y = ~Sepal.Width)
    p2 <-
     df %>%
      arrange(Species) %>%
      plot_ly(x = ~Sepal.Length, color = ~Species, legendgroup = ~Species)%>%
      add_markers(y = ~Sepal.Width, showlegend = FALSE)
    
    subplot(p1, p2)
    

I'll give you two answers, a straight one, and one for better practice and posterity (which also helps to better understand the problem) :

  1. Straight answer:
    Try to add showlegend = FALSE within the plot_ly() function, not in the layout() one. If we look at the ?subplot documentation:

    layout options found later in the sequence of plots will override options found earlier in the sequence.

    In other words, the layout showlegend option is only taken from your last plot. But using showlegend option from the plot_ly() function will affect the trace itself, saving its behavior within your subplot. Your code would now be as follows:

    require(plotly)  
    p1 <- plot_ly(data=iris,x=~Sepal.Length,y=~Sepal.Width,split=~Species,showlegend = F)
    p2 <-  plot_ly(data=iris,x=~Sepal.Length,y=~Sepal.Width,split=~Species, showlegend = T)  
    subplot(p1,p2)
    
  2. Better practice under plotly 4.0 and above.
    Use the pipe operator %>% and the group_by() function instead of split, as follows:

    p1 <-
      iris%>%
      group_by(Species)%>%
      plot_ly(x=~Sepal.Length, color= ~Species)%>%
      add_markers(y= ~Sepal.Width)
    p2 <-
      iris%>%
      group_by(Species)%>%
      plot_ly(x=~Sepal.Length, color= ~Species)%>%
      add_markers(y= ~Sepal.Width, showlegend = F)
    subplot(p1,p2)
    

This practice allows you to better understand how traces works in plotly. You can see that the data is first grouped by Species, passed to the plot_ly() function -which initializes the plot- and then you specify your trace type (markers) to actually make the plot.
Writing your code like this is easier when you want to add or remove traces and their respective options, add a grouping variable, or split/summarize your table.


Maybe you can try a simple method. (plotly 4.9.2)

subplot(style(p1, showlegend = F), p2)

It will just show the legend of p2. Hope this works.

Tags:

R

Plotly