Question:
I have 3 numerical variables and plotting them via barchart inplot_ly
. ‘sale’ and ‘share’ columns are visualized via bars, while ‘cost’ by red line. Now I want to customize legend a bit and add black border around cost in legend map (see expected outcome)
Expected output:

Answer:
This one was tricky! I know that Plotly doesn’t have anything built-in to do this simply. I tried to work out using shapes to draw this box, but that doesn’t work, because it puts the legend on top (so most of the box is hidden). I’m apparently more stubborn than Plotly though.This answer requires the package
htmlwidgets
. I didn’t change your plot other an assigning it to an object. I named your Plotly object pL
for this answer.The content for the
onRender
function:onRender
function to draw the box.
By the way, this should scale with your plot, as well.
If you have better answer, please add a comment about this, thank you!