gpt4 book ai didi

javascript - Plot.ly 在 R : Unwanted alphabetical sorting of x-axis

转载 作者:行者123 更新时间:2023-11-29 10:33:39 31 4
gpt4 key购买 nike

我尝试了几个小时,但我无法成功。我的数据框很简单

df <- as.data.frame(matrix(c("g","d","a","b","z",5,4,3,2,1),5,2))

library("plotly")
p <- plot_ly(data = df,x = ~V1,y = ~V2,type = "scatter",mode = "lines+markers") %>%
layout(title = "my title")
p

所以,这给我

myplot

但我不希望 x 轴按字母顺序排序,我只想保持顺序不变并看到递减的图形。

最佳答案

首先,一个矩阵只能保存一个类的数据。因此,您有一个要转换为 data.frame 的字符串矩阵。因为默认情况下 stringAsFactors = TRUE,您的字符矩阵会转换为 factordata.frame,其中两列的水平默认情况下排序。 V1 按字母顺序排列,V2 按升序排列。

如果您不想直接修改数据以从源头上解决问题 - 正如其他答案中所指出的,您可以选择使用 plotlycategoryorder = layout() 中的参数如下:

library(plotly)
xform <- list(categoryorder = "array",
categoryarray = df$V1)

plot_ly(data = df,
x = ~V1,
y = ~V2,
type = "scatter",
mode = "lines+markers") %>%
layout(title = "my title",
xaxis = xform)

enter image description here

关于javascript - Plot.ly 在 R : Unwanted alphabetical sorting of x-axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40701491/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com