绘制R:当每个x因子水平存在两个值时,无法获得要绘制的点 [英] Plotly R: Cant get points to plot when two values present per x factor level
问题描述
我有一个像这样的数据框:
I have a data frame like this:
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")
尝试如下打印点的散点图:
Trying to print a scatter plot with points as follows:
plot_ly(df, x = levels(x), y = y, mode = 'markers')
我只看到对应于y = 1的点,而不是y =2.知道为什么吗?
I only see points corresponding to y = 1 and not y = 2. Any idea why?
以下内容在绘图中有效,但是随后将x轴值视为数字/连续值(不是我想要的值):
The following works in plotly, but then it treats x axis values as numeric / continuous (not what I want):
plot_ly(df, x = x, y = y, mode = 'markers')
推荐答案
这不是您的上一个问题的重复副本,并且与此 github问题相关吗?
Isn't this a duplicate of your 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"))
现在,我们刚刚将因子水平作为字符串发送给 plotly.js
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
基于这些字符串,plotly.js(正确)认为您需要 数字轴.将来,我们应该设置更智能的轴类型默认值 基于变量映射.
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"))
您将获得:
这篇关于绘制R:当每个x因子水平存在两个值时,无法获得要绘制的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!