R Plotly:轴类型为category时,不能重新排列x轴 [英] R Plotly: Cannot re-arrange x-axis when axis type is category

查看:282
本文介绍了R Plotly:轴类型为category时,不能重新排列x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

pre $ 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))

我希望有一个包含 RTPAD_TOT FISCAL_YEAR_WEEK 的折线图,由其他可变元组分组。 FISCAL_YEAR_WEEK 代表会计年度的会计周。 2016-11意味着2016财年的第11周。这里是我的情节代码:

  ax<  -  list(
type =category,
showgrid = TRUE,
showline = TRUE,
autorange = TRUE,
showticklabels = TRUE,
ticks =outside,$ (
范围= c(0,300),
dtick = 50,
showgrid = 0,b $ b tickangle = 0


ay < 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)

然而,这导致我的x轴不按顺序排列:



很显然,我希望2016-09在2016-10年之前出现在图表中。但是我的 FISCAL_YEAR_WEEK 在x轴上全部混乱了。



使用 ggplot2 构造相同的图不会混乱我的x轴:

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



从上面的代码中我的轴的外观将会很好,直到......

  ggplotly()

我想将我的ggplot转换为绘图。在 ggplotly()命令之后,我的x轴将再次混乱。

我需要 type =category选项在我的x轴上,因为我只想看到数据中存在的日期。此外,将积极地将2016-09误解为2016年9月而不是2016年的第9财年。我正在构建一个闪亮的应用程序,其中我的x轴可以被用户更改为不仅是日期时间变量,而且其他变量以及。有时候,它可能是序列号,如001,002,120。因此,将它作为数字/日期时间是没有意义的,因为这会在002和120之间造成巨大差距。
因此 type =category选项在这里需要。



我也尝试将 FISCAL_YEAR_WEEK 但它不起作用:

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

我的x轴仍然会混乱起来

解决方案

直到最近,对plot.ly分类轴进行排序的唯一方法是对第一个分类轨迹的数据进行排序图(参考:Etienne的答案在情节.ly社区网站)。



这已经改变了,你可以a按照以下任一方式设置 categoryorder ,按照您的要求对分类轴进行排序:

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

$ b

或者:

$ $ $ $ $ $ $ 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

有关plot.ly的分类轴排序的更多信息,请参阅它们的参考文档


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:

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

解决方案

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.

这篇关于R Plotly:轴类型为category时,不能重新排列x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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