Order at r plotly barchart

Why do I get a different order in the graphical chart than what I defined in the variables x and y.

eg.

library(plotly)

plot_ly(
  x = c("giraffes", "orangutans", "monkeys"),
  y = c(20, 14, 23),
  name = "SF Zoo",
  type = "bar"
)

I need a histogram where I see the bands in the same order as the variable x (categorical). Is there any trick for this?

+4
source share
2 answers

plotlydoes it in alphabetical order. If you want to change it, try changing the factor level. This would be possible if you provide your data in a form data.framelike here:

library(plotly)

table <- data.frame(x = c("giraffes", "orangutans", "monkeys"),
                    y = c(20, 14, 23))
table$x <- factor(table$x, levels = c("giraffes", "orangutans", "monkeys"))

plot_ly(
    data=table,
    x = ~x,
    y = ~y,
    name = "SF Zoo",
    type = "bar"
)
+7
source

Plotly , . character ; . , categoryorder categoryarray xaxis 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)

enter image description here

+11

Source: https://habr.com/ru/post/1658300/


All Articles