Plotly R:当每个 x 因子水平存在两个值时,无法获取要绘制的点

Plotly R: Cant get points to plot when two values present per x factor level

我有一个这样的数据框:

dput(df)
structure(list(x = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
4L, 5L), .Label = c("41197708", "41223094", "41244000", "41244435", 
"41244936"), class = "factor"), y = c(1, 1, 1, 1, 1, 2, 2, 2, 
2, 2)), .Names = c("x", "y"), row.names = c(NA, -10L), class = "data.frame")

正在尝试打印具有以下点的散点图:

plot_ly(df, x = levels(x), y = y, mode = 'markers')

我只看到对应于 y = 1 而不是 y = 2 的点。知道为什么吗?

以下内容以绘图方式工作,但随后将 x 轴值视为数字/连续(不是我想要的):

plot_ly(df, x = x, y = y, mode = 'markers')

这不是您的 previous question and related to this github issue 的副本吗?

Currently you need to manually specify that you have a categorical axis.

plot_ly(df, x = x, y = y, mode = "markers") %>% layout(xaxis = list(type = "category"))

Right now we just sent the factor levels as a string to plotly.js

plotly_build(plot_ly(df, x = x, y = y, mode = "markers"))$data[[1]]$x

Based on those strings, plotly.js (correctly) thinks that you want a numeric axis. In the future, we should set smarter axis type defaults based on the variable mappings.

如果你这样做:

plot_ly(df, x = x, y = y, mode = "markers") %>% 
      layout(xaxis = list(type = "category"))

您将获得: