Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change legend size in plotly chart

Tags:

r

plotly

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.

like image 610
Adam_G Avatar asked May 16 '16 00:05

Adam_G


People also ask

How do you change the size of a graph in Plotly?

Here we have used width and height as function arguments in the scatter chart method of Plotly Express. We have used these arguments to set the dimension of the chart area. To update the margins of the graph we have used margin dimensions, left, right top, and bottom (l, r, t, b) in dictionary format.

How do you show legends in 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.

How do you not show legend on Plotly?

In this example, we are hiding legend in Plotly with the help of method fig. update(layout_showlegend=False), by passing the showlegend parameter as False.

How do you change your title position in Plotly?

In order to align titles in visualization using plotly module, we are going to use the update_layout() method. Parameters: title: Accepts string value as the title of the visualization. title_x: This parameter is used to align the title in a horizontal motion and accepts a value from 0 to 1.


Video Answer


2 Answers

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)))
like image 92
Ma Tusiaczek Avatar answered Sep 17 '22 08:09

Ma Tusiaczek


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.

like image 36
Sesha Avatar answered Sep 20 '22 08:09

Sesha