david
david

Reputation: 825

Add title to the plotly legend

In the following example how can i add a title to the legend in plot_ly for R ?

mtcars %>%   plot_ly(x = ~disp, y = ~mpg, color = ~factor(cyl), size = ~wt) %>%   add_markers(
    hoverinfo = "text",
    text = ~paste("Displacement = ", disp, "\nMiles Per Gallon = ", mpg)   ) %>%   layout(title ="Custom Hover Text")

thanks

Upvotes: 14

Views: 13622

Answers (3)

ismirsehregal
ismirsehregal

Reputation: 33397

Looking at @mics comment here, there still seems to be some confusion regarding the legend title - so lets unravel things:

Once we are passing factors to plot_ly()'s color parameter

color = ~factor(drat)

plotly will create a legend (for discrete / categorial data) and we can use layout() to set the legend title:

fig_discrete <- mtcars %>%
  plot_ly(
    type = "scatter",
    mode = "markers",
    x = ~ disp,
    y = ~ mpg,
    color = ~ factor(cyl),
    size = ~ wt,
    fill = "",
    hoverinfo = "text",
    text = ~ paste("Displacement = ", disp, "\nMiles Per Gallon = ", mpg)
  ) %>% layout(title = "Custom Hover Text", legend = list(title = list(text = "<b>Cylinders</b>")))
fig_discrete

# plotly_json(fig_discrete)
# navigate:
# layout.legend.title.text

result_1

However, once we pass numeric data to plot_ly()'s color parameter

color = ~drat 

plotly will create a colorbar (for continuous data) and we can use colorbar() to set the colorbar title:

fig_continuous <- mtcars %>%
  plot_ly(
    type = "scatter",
    mode = "markers",
    x = ~ disp,
    y = ~ mpg,
    color = ~ drat,
    marker = list(size = ~ wt * 3),
    hoverinfo = "text",
    text = ~ paste("Displacement = ", disp, "\nMiles Per Gallon = ", mpg)
  ) %>% layout(title = "Custom Hover Text") %>% colorbar(title = "<i>Rear Axle Ratio</i>")
fig_continuous

# plotly_json(fig_continuous)
# navigate:
# data[0].marker.colorbar.title
# and
# data[1].marker.colorbar.title

result_2

For further information please see the according documentation.

Upvotes: 1

hmhensen
hmhensen

Reputation: 3195

This functionality has since been included within the layout function in the legend option. There's a sub-option called title within which you can supply a list that includes the text.

mtcars %>% 
  plot_ly(x = ~disp, y = ~mpg, color = ~factor(cyl), size = ~wt) %>% 
  add_markers(hoverinfo = "text",
              text = ~paste("Displacement = ", disp, "\nMiles Per Gallon = ", mpg)   ) %>% 
  layout(title = "Custom Hover Text", 
         legend = list(title = list(text = "<b>Cylinders</b>"))) # TITLE HERE

Upvotes: 10

Mike Wise
Mike Wise

Reputation: 22807

The only way I know is to use an annotation and add it to the plot. Like this:

legendtitle <- list(yref='paper',xref="paper",y=1.05,x=1.1, text="Cylinders",showarrow=F)

mtcars %>%  plot_ly(x = ~disp, y = ~mpg, color = ~factor(cyl), size = ~wt) %>%   
  add_markers(  hoverinfo = "text",
                text = ~paste("Displacement=",disp, "\nMiles Per Gallon = ", mpg)) %>%   
  layout(title ="Custom Hover Text", annotations=legendtitle )

Yielding:

enter image description here

It is a bit tricky to place the legend title though, not sure if this placement would always work.

Another way would be to use ggplot and ggplotly of course, and let ggplot figure it out.

Upvotes: 8

Related Questions