在R绘图条形图中订购 [英] Ordering in r plotly barchart

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

问题描述

为什么在可绘制的条形图中得到的顺序与在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绘图条形图中订购的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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