Ordering in r plotly barchart

后端 未结 3 1466
夕颜
夕颜 2020-12-05 18:03

Why do I get the different order in plotly bar chart than I defined in x and y variables.

E.g.

library(plotly)

plot_ly(
  x = c(\"giraffes\", \"oran         


        
3条回答
  •  臣服心动
    2020-12-05 18:55

    Plotly will order your axes by the order that is present in the data supplied. In case of character vectors alphabetically; in case of factors by the order of levels. To override this behaviour, you need to define categoryorder and categoryarray for the xaxis inside layout:

    library(plotly)
    xform <- list(categoryorder = "array",
                  categoryarray = c("giraffes", 
                                    "orangutans", 
                                    "monkeys"))
    
    plot_ly(
     x = c("giraffes", "orangutans", "monkeys"),
     y = c(20, 14, 23),
     name = "SF Zoo",
     type = "bar") %>% 
     layout(xaxis = xform)
    

提交回复
热议问题