Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly R order legend entries

Tags:

r

plotly

Is it possible to order the legend entries in R?

If I e.g. specify a pie chart like this:

  plot_ly(df, labels = Product, values = Patients, type = "pie",
          marker = list(colors = Color), textfont=list(color = "white")) %>%
    layout(legend = list(x = 1, y = 0.5))

The legend gets sorted by which Product has the highest number of Patients. I would like the legend to be sorted in alphabetical order by Product.

Is this possible?

like image 620
marcopah Avatar asked Mar 21 '16 07:03

marcopah


People also ask

How do I make a legend appear in Plotly?

When using plotly.graph_objects rather than Plotly Express, legend items will appear in the order that traces appear in the data: Legends have an anchor point, which can be set to a point within the legend using layout.legend.xanchor and layout.legend.yanchor.

What is the default legend order in Plotly Express?

Legend Order By default, Plotly Express lays out legend items in the order in which values appear in the underlying data.

How to sort legend items of a ggplot2 graphic manually?

This Example shows how to sort legend items of a ggplot2 graphic manually. First, we need to replicate our data: Now, we can modify the factor levels of our grouping column in the order we want. Figure 2 shows the output of the previous R code: The legend items were ordered according to the specification of factor levels that we did before.

Why do legend items have to be linked to traces?

The fact that legend items are linked to traces means that when using discrete color, a figure must have one trace per color in order to get a meaningful legend. Plotly Express has robust support for discrete color to make this easy.


1 Answers

Yes, it's possible. Chart options are here: https://plot.ly/r/reference/#pie.

An example:

library(plotly)
library(dplyr)

# Dummy data
df <- data.frame(Product = c('Kramer', 'George', 'Jerry', 'Elaine', 'Newman'), 
                 Patients = c(3, 6, 4, 2, 7))

# Make alphabetical
df <- df %>%
    arrange(Product)

# Sorts legend largest to smallest
plot_ly(df, 
        labels = ~Product, 
        values = ~Patients, 
        type = "pie",
        textfont = list(color = "white")) %>%
    layout(legend = list(x = 1, y = 0.5))

# Set sort argument to FALSE and now orders like the data frame
plot_ly(df, 
        labels = ~Product, 
        values = ~Patients, 
        type = "pie",
        sort = FALSE,
        textfont = list(color = "white")) %>%
    layout(legend = list(x = 1, y = 0.5))

# I prefer clockwise
plot_ly(df, 
        labels = ~Product, 
        values = ~Patients, 
        type = "pie",
        sort = FALSE,
        direction = "clockwise",
        textfont = list(color = "white")) %>%
    layout(legend = list(x = 1, y = 0.5))

Session info:

R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252    LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                       LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] bindrcpp_0.2.2 dplyr_0.7.5    plotly_4.7.1   ggplot2_2.2.1

EDIT: Modified to work with plotly 4.x.x (i.e. added ~)

like image 161
mal Avatar answered Sep 23 '22 05:09

mal