我想使用串扰手动对R散点图的x轴进行排序以按代号(Code)进行过滤,但我的x轴是时间序列分类变量(时间;即Jan-Feb 2019,Mar -2019年4月)。默认情况下,R会按字母顺序对x轴进行排序,这会使我的x轴乱序,并且这些点是混杂的(即第一个点连接到最后一个点,然后连接到第四个点...)
我尝试将时间转换为因子并手动设置顺序,但是图形上的点仍然混乱
非常感谢您的帮助!
#Create dataset
df <- data.frame(
"Code" = c("A","A","A","A","A","B","B","B","B","B"),
"Time" = c("2016-2018","Jan-Feb 2019","Mar-Apr 2019","May-Jun 2019","Jul-Aug 2019", "2016-2018","Jan-Feb 2019","Mar-Apr 2019","May-Jun 2019","Jul-Aug 2019"),
"Rate" = c(40.8, 50.8, 15.3, 39, 40.1, 70.2, 38.3, 25.2, 46.7, 41.9)
)
#Create filter
filter <- SharedData$new(df)
filter_select("Code", "Select Provider", filter, ~Code, multiple = FALSE)
#Graph
filter %>%
plot_ly(
x = ~`Time`,
y = ~`Rate`,
type = "scatter",
mode="lines+markers"
)
#ATTEMPT 1: Convert Time to factor, rerun filter, then graph
df$Time <- factor(df$Time, levels = c("2016-2018", "Jan-Feb 2019", "Mar-Apr 2019", "May-Jun 2019", "Jul-Aug 2019"))
filter <- SharedData$new(df)
filter_select("Code", "Select Provider", filter, ~Code, multiple = FALSE)
plot_ly(df,
x = ~`Time`,
y = ~`Rate`,
type = "scatter",
mode="lines+markers"
)
答案 0 :(得分:2)
如果在SharedData对象而不是原始数据帧上调用plot_ly
,则可以使用filter_select
选项以正确的顺序显示代码A和B:
bscols(filter_select("Code", "Select Provider", filter, ~Code, multiple = FALSE),
plot_ly(filter,
x = ~`Time`,
y = ~`Rate`,
type = "scatter",
mode="lines+markers"
))
答案 1 :(得分:1)