如何按y值对r中的绘图堆叠条形图进行排序? [英] How to sort plotly stacked bar graph in r by y value?

查看:635
本文介绍了如何按y值对r中的绘图堆叠条形图进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此图,并且我希望它以Sam-Jhon-Paul的顺序显示,因为这是从最高成本到最低成本,有人可以告诉我如何按成本进行订购吗?使用下面的layout部分中的代码进行了尝试,但是没有显示出来.

I've got this graph and I want it to appear in Sam - Jhon - Paul order because that's going from highest to lowest cost, somebody could tell me how to order it by cost? Tried using code below in layout section but it didn't word.

 layout(yaxis = list(title = 'Cost'), 
        xaxis = list(title = 'Parent',
                     categoryorder = "array",
                     categoryarray = ~cost), 
                     barmode = 'stack') 

推荐答案

您快到了.您只需要在categoryarray中使用top_3$parent的顺序而不是cost,如下所示:

You are almost there. You just need to use the order of top_3$parent in your categoryarray instead of cost, as follows:

layout(yaxis = list(title = 'Cost'), 
       xaxis = list(title = 'Parent',
               categoryorder = "array",
               categoryarray = top_3$parent), 
               barmode = 'stack')

因此,使用完整的绘图代码:

So, using the full plotting code:

plot_ly(sample[sample$parent %in% top_3$parent,], 
        x = ~parent, y = ~cost, type = 'bar', color = ~topic) %>%
        layout(yaxis = list(title = 'Cost'), 
               xaxis = list(title = 'Parent', 
                            categoryorder = "array", 
                            categoryarray = top_3$parent), 
                            barmode = 'stack') %>%
        config(displayModeBar = FALSE)

您应该获得以下情节:

这篇关于如何按y值对r中的绘图堆叠条形图进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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