简体   繁体   English

在 R 中订购带有 plotly 的 x 轴

[英]Ordering x-axis with plotly in R

I am trying to make chart with plotly package.我正在尝试使用 plotly package 制作图表。 I think this will be easy task but I am stuck with this code.我认为这将是一件容易的事,但我被这段代码困住了。 Below you can see code and data下面你可以看到代码和数据

  DataSetTable<-structure(list(Range = c("=<100", "100-200", "200-300", "300-400", 
                                                               "400-500", "500-600", "600-700", "700-800", "800-900", "900-1,000", 
                                                               "1,000-1,100", "1,100-1,200", "1,200-1,300", "1,300-1,400", "> 1,400"
                        ), Sales_before = c(91.3653, 92.89215, 281.44935, 135.2511, 93.73995, 
                                            44.87535, 25.1424, 15.1659, 9.32445, 5.6484, 4.3929, 3.2049, 
                                            2.2329, 1.58355, 7.6707), Sales_after = c(86.2056, 80.6733, 256.63905, 
                                                                                      142.83, 110.2518, 53.82045, 29.95515, 17.7552, 10.4598, 6.8472, 
                                                                                      4.7466, 4.06755, 2.62575, 2.01285, 9.1125)), row.names = c(NA, 
                                                                                                                                                 -15L), class = c("tbl_df", "tbl", "data.frame"))
                    

在此处输入图像描述

So next steep should be plotting this data with plotly package.所以下一个陡峭的应该是用 plotly package 绘制这个数据。 In order to do this I try to plot this data with code below为了做到这一点,我尝试使用下面的代码 plot 这个数据

library(plotly)
 fig <- plot_ly(DataSetTable, x = ~Range, y = ~Sales_before , name = "Sales_before", type = 'scatter', mode = 'lines',
                                       line = list(dash = "solid"))
                        fig <- fig %>% add_trace(y = ~Sales_after, name = "After_before", line = list(dash = "dash"))%>%
                          layout(
                            xaxis = list(title = ' '),
                            yaxis = list(title = ' ')
                          )
fig
                    

But result from this code is not good because x-axis have different ordering.You can see how is look like chart:但是这段代码的结果并不好,因为 x 轴有不同的顺序。你可以看到图表的样子: 在此处输入图像描述

So can anybody help me to fix this code in order to have chart like pic below?那么任何人都可以帮助我修复此代码以获得如下图所示的图表吗?

在此处输入图像描述

You could change the Range to be an ordered factor.您可以将 Range 更改为有序因子。

DataSetTable<-structure(list(Range = c("=<100", "100-200", "200-300", "300-400", 
                                                                                 "400-500", "500-600", "600-700", "700-800", "800-900", "900-1,000", 
                                                                                 "1,000-1,100", "1,100-1,200", "1,200-1,300", "1,300-1,400", "> 1,400"
    ), Sales_before = c(91.3653, 92.89215, 281.44935, 135.2511, 93.73995, 
                                            44.87535, 25.1424, 15.1659, 9.32445, 5.6484, 4.3929, 3.2049, 
                                            2.2329, 1.58355, 7.6707), Sales_after = c(86.2056, 80.6733, 256.63905, 
                                                                                                                                142.83, 110.2518, 53.82045, 29.95515, 17.7552, 10.4598, 6.8472, 
                                                                                                                                4.7466, 4.06755, 2.62575, 2.01285, 9.1125)), row.names = c(NA, 
                                                                                                                                                                                                                                                     -15L), class = c("tbl_df", "tbl", "data.frame"))
    
DataSetTable$Range <- factor(DataSetTable$Range, ordered = T, levels=DataSetTable$Range)
    
library(plotly)
fig <- plot_ly(DataSetTable, x = ~Range, y = ~Sales_before , name = "Sales_before", type = 'scatter', mode = 'lines',
                                 line = list(dash = "solid"))
fig <- fig %>% add_trace(y = ~Sales_after, name = "After_before", line = list(dash = "dash"))%>%
        layout(
            xaxis = list(title = ' '),
            yaxis = list(title = ' ')
        )
fig

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM