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.
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.
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.
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.
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") )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With