14
votes

Is there a way to change the legend size in plotly for R? I have not come across this option. I have looked at the docs on legends, https://plot.ly/r/legend/, but it does not mention this.

2
Will be changing the font size enough? - zyurnaidi
No. The scale and position will need to changed. But thank you. - Adam_G
See plot.ly/r/reference/#layout-legend for possible options - royr2
Which option are you referring to? I don't see anything pertaining to scaling the size of the legend. - Adam_G
I don't think changing the size is possible. The only things you could do are related to positioning, font size and some other aesthetic features that do not affect the overall size of the legend. - d-roy

2 Answers

16
votes

Use layout(legend = list(font = list(size(30)))):

plot_ly(data = mtcars, x = as.character(mtcars$cyl), 
        y = mtcars$mpg, type = "box", color = as.character(mtcars$cyl)) %>%
    layout(showlegend = TRUE, legend = list(font = list(size = 30)))
4
votes

While working with legends, there are two pieces to configure.

  1. Legend title (legend_title parameter)
  2. Actual legend (legend parameter)

Let's say you already have a fig(it can be mostly any plot), to that add below line with parameters as you wish, in separate dictionaries.

fig.update_layout(legend = dict(font = dict(family = "Courier", size = 50, color = "black")),
                  legend_title = dict(font = dict(family = "Courier", size = 30, color = "blue")))

This way you will have more control over two thing.