Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add title to the plotly legend

Tags:

plot

r

plotly

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

like image 320
david Avatar asked Oct 12 '16 14:10

david


People also ask

How do you add labels in Plotly?

As a general rule, there are two ways to add text labels to figures: Certain trace types, notably in the scatter family (e.g. scatter , scatter3d , scattergeo etc), support a text attribute, and can be displayed with or without markers. Standalone text annotations can be added to figures using fig.

How do you hide legend titles in Plotly?

Plotly Show Legend To disable the Legend, we can use the update_layout() function and set the showlegend parameter to false.

How do you show legends in Plotly?

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.


1 Answers

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
like image 73
hmhensen Avatar answered Oct 11 '22 07:10

hmhensen