在 r plotly 条形图中排序 [英] Ordering in r plotly barchart

查看:21
本文介绍了在 r plotly 条形图中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我在 plotly 条形图中得到的顺序与我在 x 和 y 变量中定义的顺序不同.

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

例如

library(plotly)

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

我需要条形图,其中我看到条形的顺序与定义 x 变量(分类)的顺序相同.有什么诀窍吗?

I need bar chart where I see bars in the same order as x variable (categorical) is defined. Is there any trick for that?

推荐答案

plotly 按字母顺序进行.如果你想改变它,只需尝试改变因子的水平.如果您以 data.frame 的形式提供数据,这将是可能的:

plotly does it in alphabetical order. If you want to change it, just try to change levels of factor. This would be possible if you give your data in the form of data.frame like 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"
)

这篇关于在 r plotly 条形图中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆