简体   繁体   中英

Ordering x-axis with plotly in R

I am trying to make chart with 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. In order to do this I try to plot this data with code below

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: 在此处输入图像描述

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.

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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