Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
881 views
in Technique[技术] by (71.8m points)

plot - R Plotly: Cannot re-arrange x-axis when axis type is category

I have the following data:

myData <- data.frame(FISCAL_YEAR_WEEK = c('2016-09','2016-09','2016-09','2016-09','2016-09','2016-10','2016-10','2016-10','2016-10','2016-10','2016-10','2016-10','2016-10','2016-11','2016-11','2016-11','2016-11','2016-12','2016-12','2016-12','2016-12','2016-12','2016-12','2016-14','2016-14','2016-14','2016-14'), 
                     MOTOR_VEND_ID = c('7','E','F','F','M','7','9','E','E','F','F','M','R','7','E','F','F','E','E','F','F','M','M','7','E','F','M'),HGA_SUPPLIER=c('RHO','RHO','HWY','RHO','RHO','RHO','RHO','HWY','RHO','HWY','RHO','RHO','RHO','RHO','RHO','HWY','RHO','HWY','RHO','HWY','RHO','HWY','RHO','RHO','RHO','RHO','RHO'), 
                     RTPAD_TOT = c(0,0,0,0,0,0,420,6,0,0,0,20,1,76,0,0,0,76,62,0,0,0,0,6,1,1,0))

I would like to have a line chart with RTPAD_TOT against FISCAL_YEAR_WEEK, grouped by the other varables. FISCAL_YEAR_WEEK represents the fiscal week in a fiscal year. 2016-11 means the 11th week of fiscal year 2016. Here's my plotly code:

ax <- list(
type = "category",
showgrid = TRUE,
showline = TRUE,
autorange = TRUE,
showticklabels = TRUE,
ticks = "outside",
tickangle = 0
)

ay <- list(
range = c(0,300),
dtick = 50,
showgrid = TRUE,
showline = TRUE,
autorange = FALSE,
showticklabels = TRUE,
ticks = "outside",
tickangle = 0
)

plot_ly(myData,
x = FISCAL_YEAR_WEEK,
y = RTPAD_TOT,
type = 'scatter',
mode = 'markers+lines',
color = interaction(MOTOR_VEND_ID,HGA_SUPPLIER)) %>%
layout(xaxis = ax, yaxis = ay)

However, this results in my x-axis not arranged in order: plotly jumbled up my x-axis

Obviously, I would like 2016-09 to come before 2016-10 in the chart. But my FISCAL_YEAR_WEEK in the x-axis is all jumbled up.

Using ggplot2 to construct the same plot does not jumble up my x-axis:

ggplot(data = myData, 
       aes(x = FISCAL_YEAR_WEEK, 
           y = RTPAD_TOT, 
           group = interaction(MOTOR_VEND_ID, HGA_SUPPLIER)))) +
  geom_line()

The appearance of my axis from the above code will be all good until...

ggplotly()

I wanted to convert my ggplot to plotly. After the ggplotly() command, my x-axis will be jumbled up again.

I need the type = "category" option in my x-axis as I only want to see the dates that are present in the data. Also, plotly will misinterpret 2016-09 to be september 2016 instead of the 9th fiscal week of 2016. I'm building a shiny application where my x-axis can be changed by the user to be not only a datetime variable, but other variable as well. Sometimes, it may be serial numbers like 001, 002, 120. So it doesn't make sense to make it numeric/datetime as this will make a huge gap between 002 and 120. So the type = "category" option is needed here.

I've also tried to factorize the FISCAL_YEAR_WEEK but it doesn't work:

myData$FISCAL_YEAR_WEEK <- factor(myData$FISCAL_YEAR_WEEK,levels=sort(unique(myData$FISCAL_YEAR_WEEK)))

my x-axis will still be jumbled up in plotly

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Until recently, the only way to sort a plot.ly categorical axis was to sort the data of the first categorical trace of the graph (ref: Etienne's answer on plot.ly community site).

That's changed, and you can achieve the categorical axis sort you desire by setting categoryorder in either of the following manners:

ax <- list(
  type = "category",
  categoryorder = "category ascending",
  showgrid = TRUE,
  showline = TRUE,
  autorange = TRUE,
  showticklabels = TRUE,
  ticks = "outside",
  tickangle = 0
)

Or:

ax <- list(
  type = "category",
  categoryorder = "array",
  categoryarray = sort(unique(myData$FISCAL_YEAR_WEEK)),
  showgrid = TRUE,
  showline = TRUE,
  autorange = TRUE,
  showticklabels = TRUE,
  ticks = "outside",
  tickangle = 0
)

For more on plot.ly's categorical axis sort, see their reference doc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...