如何按正确的降序排列哑铃图? [英] How can I make my dumbbell chart in correct descending order?

查看:59
本文介绍了如何按正确的降序排列哑铃图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个哑铃图来显示不同时间段(例如工作日VS周末)之间产品销售的差异,并希望按降序选择20种最杰出的产品.但是看来我的订单和选择不能正常工作.

I made an dumbbell chart to show the difference of product sales among different time windows (e.g.weekday VS weekend), and wanted to select the 20 most distinguished products in an descending order. But it seems my order and selection doesn't work properly.

以下是哑铃图的数据:

>head(product_dumbbell)

product_aisle     daytime evening long medium short weekday weekend
1: candles           16       4    2      6    12      15       5
2: asian foods      115     25    23     29    88      90      50
3: baby accessories   7      3     0      0    10       7       3
4: baby body care     4      3     1      2     4       7       0
5: baby food formula 149    44    24     29    140    142      51
6: bakery desserts    53    11     6      6     52     47      17

我的哑铃图代码如下:

product_dumbbell%>%
   top_n(20)%>%
   ggplot() +
   aes(x=weekday, xend=weekend, y=product_aisle, 
       group=product_aisle) + 
   geom_dumbbell(color="#a3c4dc", 
            size=0.75, 
            colour_x="#edae52", 
            colour_xend = "#9fb059") + 
   labs(x=NULL, 
        y=NULL, 
        title="Product Dumbbell Chart: weekend VS weekday") +
    theme(plot.title = element_text(hjust=0.5, face="bold"),
          plot.background=element_rect(fill="#f7f7f7"),
          panel.background=element_rect(fill="#f7f7f7"),
          panel.grid.minor=element_blank(),
          panel.grid.major.y=element_blank(),
          panel.grid.major.x=element_line(),
          axis.ticks=element_blank(),
          legend.position="top",
          panel.border=element_blank())

R提醒我,结果是在周末选择的.实际上,我想通过周末和工作日之间的差异值选择前20名,然后将它们按降序排列.

R reminded me that the result is selected by weekend. Actually I want to select top 20 by their difference values between weekend and weekday, and place them in descending order.

有没有做过哑铃图的人可以帮助我?非常感谢!

Is there anybody who have made a dumbbell chart can help me? Thanks a lot!

推荐答案

因此,如果我理解正确,则希望按降序排列前20个差异.
首先,您必须创建一个带有 mutate 差异的列.在代码中,我使用 abs 给出绝对值.使用该变量,您可以获取前20个差异,并为函数赋予适当的权重.
然后,你根据其它变量重新排列的标签,我用差在本实施例中使用<代码> - 所以它的下降

So if I understood right you want top 20 differences in descending order.
First you have to create a column with the differences with mutate. In the code I use abs to give the absolute values. With the variable you can get the top 20 differences, giving the proper weight to the funtion.
Then you rearrange the labels according to other variable, I used difference in this example with the - so it's descending.

library(dplyr)
library(ggalt)

product_dumbbell%>%
  mutate(difference = abs(weekend-weekday)) %>% #creates the variable of differences
  top_n(20, wt = difference) %>% # Choose the rows with top 20 difference
  ggplot() +
  aes(x=weekday, xend=weekend, y=reorder(product_aisle, -difference), 
      group=product_aisle) + #reorder the labels by descending difference value
  geom_dumbbell(color="#a3c4dc", 
                size=0.75, 
                colour_x="#edae52", 
                colour_xend = "#9fb059") + 
  labs(x=NULL, 
       y=NULL, 
       title="Product Dumbbell Chart: weekend VS weekday") +
  theme(plot.title = element_text(hjust=0.5, face="bold"),
        plot.background=element_rect(fill="#f7f7f7"),
        panel.background=element_rect(fill="#f7f7f7"),
        panel.grid.minor=element_blank(),
        panel.grid.major.y=element_blank(),
        panel.grid.major.x=element_line(),
        axis.ticks=element_blank(),
        legend.position="top",
        panel.border=element_blank())

这篇关于如何按正确的降序排列哑铃图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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