Plot.ly in R: Unwanted alphabetical sorting of x-axis(R中的Plot.ly:x轴的不需要的字母排序)
问题描述
我尝试了几个小时,但我无法成功.我的数据框很简单
I tried for hours, but I could not succeed. My data frame is simply
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 = "bXkgdGl0bGU=")
p
所以,这个给我
但我不希望 x 轴按字母顺序排序,我只想保持原样并查看递减图.
But I don't want x axis to be sorted alphabetically, I just want to keep the order as it is and see a decreasing graph.
推荐答案
首先,一个矩阵只能保存一个类的数据.因此,您有一个要转换为 data.frame 的字符串矩阵.因为默认情况下 stringAsFactors = TRUE,您的字符矩阵将转换为 factor 的 data.frame,其中两列的级别默认排序.V1 按字母顺序排列,V2 按升序排列.
First of all, a matrix can only hold data of one class. Hence you have a matrix of strings that you are converting to a data.frame. Because by default stringAsFactors = TRUE, your character matrix is converted to a data.frame of factor's, where the levels of your two columns are by default sorted. Alphabetically for V1 and in increasing order for V2.
如果您不希望直接修改数据以从源头上解决问题 - 正如其他答案中指出的那样,您可以替代地使用 plotly 的 categoryorder =<layout() 中的/code> 参数,方式如下:
If you don't wish to modify the data directly to remedy the issue at the source - as pointed out in the other answers, you can altenatively use plotly's categoryorder = argument inside layout() in the following way:
library(plotly)
xform <- list(categoryorder = "array",
categoryarray = df$V1)
plot_ly(data = df,
x = ~V1,
y = ~V2,
type = "scatter",
mode = "lines+markers") %>%
layout(title = "bXkgdGl0bGU=",
xaxis = xform)
这篇关于R中的Plot.ly:x轴的不需要的字母排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:R中的Plot.ly:x轴的不需要的字母排序
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Fetch API 如何获取响应体? 2022-01-01
