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 is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

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

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

fig
20040060080010001200140050100150200
Tree 1Tree 2Tree 3Tree 4Tree 5ageTree1

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

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

fig

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

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

fig
20040060080010001200140050100150200
Tree 1Tree 2Tree 4Tree 5ageTree1

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

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

fig
20040060080010001200140050100150200
Tree 1Tree 2Tree 3Tree 4Tree 5ageTree1

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

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

fig
20040060080010001200140050100150200
Tree 1Tree 2Tree 3Tree 4Tree 5ageTree1

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

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

fig
20040060080010001200140050100150200
Tree 1Tree 2Tree 3Tree 4Tree 5ageTree1

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)


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

fig
20040060080010001200140050100150200
Tree 1Tree 2Tree 3Tree 4Tree 5ageTree1

Size of Legend Items

In this example, since the itemsizing attribute is set to constant, the size of legend items symbols remains the same, regardless of how tiny/huge the bubbles are in the graph.

library(plotly)

fig <- plot_ly(
  type='scatter',
  x=c(1, 2, 3, 4, 5),
  y=c(1, 2, 3, 4, 5),
  mode='markers',
  marker=list(size=10))

fig <- fig %>% add_trace(
  type='scatter',
  x=c(1, 2, 3, 4, 5),
  y=c(5, 4, 3, 2, 1),
  mode='markers',
  marker=list(size=100))

fig <- fig %>% layout(legend= list(itemsizing='constant'))
fig

Legend Title

library(plotly)

fig <- plot_ly(
  type='scatter',
  mode='line',
  x=c(1, 2, 3, 4, 5),
  y=c(1, 2, 3, 4, 5),
  name="Increasing"
)

fig <- fig %>% add_trace(
  type='scatter',
  mode='line',
  x=c(1, 2, 3, 4, 5),
  y=c(5, 4, 3, 2, 1),
  name="Decreasing"
) 

fig <- fig %>% layout(legend=list(title=list(text='<b> Trend </b>')))
fig

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

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

fig
5001000150050100150200
Zone 1 - Tree 1Zone 1 - Tree 2Zone 1 - Tree 3Zone 2 - Tree 1Zone 2 - Tree 2ageTree1

Subplot Grouped Legend

library(plotly)

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

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

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

fig <- subplot(fig1, fig2, nrows = 2, shareX = T)

fig

Reference

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

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)