Show Sidebar Hide Sidebar

Legends in R

How to modify the legend in R graphs. Nine examples of how to move, color, and hide the legend.

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

library(plotly)
packageVersion('plotly')
## [1] '4.9.1'

Legend Names

library(plotly)
library(tidyr)
library(plyr)

data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))

p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1') %>%
  add_trace(y = ~Tree2, name = 'Tree 2') %>%
  add_trace(y = ~Tree3, name = 'Tree 3') %>%
  add_trace(y = ~Tree4, name = 'Tree 4') %>%
  add_trace(y = ~Tree5, name = 'Tree 5')

p

Hiding the Legend

library(plotly)
library(tidyr)
library(plyr)

data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))

p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines') %>%
  add_trace(y = ~Tree2) %>%
  add_trace(y = ~Tree3) %>%
  add_trace(y = ~Tree4) %>%
  add_trace(y = ~Tree5) %>%
  layout(showlegend = FALSE)

p

Hiding Legend Entries

library(plotly)
library(tidyr)
library(plyr)

data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))

p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1') %>%
  add_trace(y = ~Tree2, name = 'Tree 2') %>%
  add_trace(y = ~Tree3, name = 'Tree 3', showlegend = FALSE) %>%
  add_trace(y = ~Tree4, name = 'Tree 4') %>%
  add_trace(y = ~Tree5, name = 'Tree 5')

p

Positioning the Legend Inside the Plot

library(plotly)
library(tidyr)
library(plyr)

data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))

p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1') %>%
  add_trace(y = ~Tree2, name = 'Tree 2') %>%
  add_trace(y = ~Tree3, name = 'Tree 3') %>%
  add_trace(y = ~Tree4, name = 'Tree 4') %>%
  add_trace(y = ~Tree5, name = 'Tree 5') %>%
  layout(legend = list(x = 0.1, y = 0.9))

p

Positioning the Legend Outside the Plot

library(plotly)
library(tidyr)
library(plyr)

data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))

p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1') %>%
  add_trace(y = ~Tree2, name = 'Tree 2') %>%
  add_trace(y = ~Tree3, name = 'Tree 3') %>%
  add_trace(y = ~Tree4, name = 'Tree 4') %>%
  add_trace(y = ~Tree5, name = 'Tree 5') %>%
  layout(legend = list(x = 100, y = 0.5))

p

Changing the Legend Orientation

library(plotly)
library(tidyr)
library(plyr)

data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))

p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1') %>%
  add_trace(y = ~Tree2, name = 'Tree 2') %>%
  add_trace(y = ~Tree3, name = 'Tree 3') %>%
  add_trace(y = ~Tree4, name = 'Tree 4') %>%
  add_trace(y = ~Tree5, name = 'Tree 5') %>%
  layout(legend = list(orientation = 'h'))

p

Styling the Legend

library(plotly)
library(tidyr)
library(plyr)

data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))

l <- list(
  font = list(
    family = "sans-serif",
    size = 12,
    color = "#000"),
  bgcolor = "#E2E2E2",
  bordercolor = "#FFFFFF",
  borderwidth = 2)


p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1') %>%
  add_trace(y = ~Tree2, name = 'Tree 2') %>%
  add_trace(y = ~Tree3, name = 'Tree 3') %>%
  add_trace(y = ~Tree4, name = 'Tree 4') %>%
  add_trace(y = ~Tree5, name = 'Tree 5') %>%
  layout(legend = l)

p

Grouped Legend

Plotly legends are interactive. Click on the legend entries to hide and show traces. The legendgroup key groups legend entries so that clicking on one legend entry will hide or show all of the traces in that group.

library(plotly)
library(tidyr)
library(plyr)

data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))

p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', 
        legendgroup = 'group1', name = 'Zone 1 - Tree 1') %>%
  add_trace(y = ~Tree2, legendgroup = 'group2', name = 'Zone 2 - Tree 1') %>%
  add_trace(y = ~Tree3, legendgroup = 'group1', name = 'Zone 1 - Tree 2') %>%
  add_trace(y = ~Tree4, legendgroup = 'group2', name = 'Zone 2 - Tree 2') %>%
  add_trace(y = ~Tree5, legendgroup = 'group1', name = 'Zone 1 - Tree 3') 

p

Subplot Grouped Legend

library(plotly)

df <- data.frame(x = c("a","b","c"), y = c(2,3,2), y2 = c(4,2,4))

p1 <- df %>%
  plot_ly(
    type = 'bar', 
    x = ~x, 
    y = ~y, 
    color = ~x, 
    legendgroup = ~x
  ) %>% 
  layout(
    xaxis = list(
      showgrid = F
    ),
    yaxis = list(
      showgrid = F
    )
  )

p2 <- df%>% 
  plot_ly(
    type = 'bar', 
    x = ~x, 
    y = ~y2, 
    color =  ~x, 
    legendgroup = ~x, 
    showlegend = F
  ) %>% 
  layout(
    xaxis = list(
      showgrid = F
    ),
    yaxis = list(
      showgrid = F
    )
  )

p <- subplot(p1, p2, nrows = 2, shareX = T) 

p

Reference

See https://plot.ly/r/reference/#layout-legend for more information and chart attribute options!