Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding title to plotly legend in R [duplicate]

Tags:

plot

r

plotly

I'd like to add a legend-title to my plotly graph (made in R). This doesn't seem possible in any straightforward fashion.

If nothing else works, I might add an annotation to the graph right above where the legend entries are shown. That said, I'm not sure where to anchor said notation.

I'm not sure how to proceed. Thanks.

like image 396
Bobak Digital Avatar asked Jan 22 '16 19:01

Bobak Digital


People also ask

How do you show legend Plotly?

By default, Plotly chart with multiple traces shows legends automatically. If it has only one trace, it is not displayed automatically. To display, set showlegend parameter of Layout object to True. Default labels of legends are trace object names.

How to hide legend in Plotly r?

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.

How do you change the legend size in Plotly?

Plotly's update_layout() function is used to change legend size in the plotly chart. The values in the input dict / keyword arguments are used to iteratively alter the parts of the original layout.


1 Answers

You can anchor the legend title to the legend by specifying the same y coordinate for both and using yanchor to place each element on the correct side:

plot_ly( ... ) %>%
  add_annotations( text="MyTitle", xref="paper", yref="paper",
                  x=1.02, xanchor="left",
                  y=0.8, yanchor="bottom",    # Same y as legend below
                  legendtitle=TRUE, showarrow=FALSE ) %>%
  layout( legend=list(y=0.8, yanchor="top") )
like image 134
Artem Sokolov Avatar answered Sep 30 '22 01:09

Artem Sokolov