【问题标题】:3dscatter plot for trading data交易数据的 3d 散点图
【发布时间】:2020-03-25 01:15:34
【问题描述】:

我正在尝试使用 trade.sqlite3 数据库的表退出一段时间。我想出了一个导入和导出表的组合表。

A csv-file of the table can be downloaded here :

Added a virus scan of the file as well

str 了解它的外观:

类“tbl_df”、“tbl”和“data.frame”:300 obs。 4 个变量:
$ 年 : num 2002 2002 2002 2002 2002 ...
$ 类别:因子 w/10 级别“0--食物和活的动物”,..:1 2 3 4 5 6 7 8 9 10 ...
$ 值:数字 4.94e+08 2.88e+08 9.40e+08 4.96e+07 7.76e+06 ...
$ type : Factor w/ 2 个级别 "Exp","Imp": 1 1 1 1 1 1 1 1 1 1 ...

现在我想使用plot_ly 绘制此表。

我想出了以下想法:

library(plotly)
total_val <- read.csv("Total_Value")
plot_ly(data = total_val, x = total_val$year, 
y = total_val$value, z = total_val$category, color = total_val$category, 
stroke = total_val$category, type = "scatter3d" )

我仍然在与一些问题作斗争:
- 我想指出标记是导入类型还是导出类型。
- 像我一样使用颜色代码会产生以下警告:

n 太大,调色板 Set2 允许的最大值为 8
用这么多颜色返回你要求的调色板

有 10 个类别,我需要将它们全部显示出来。尽管有颜色编码,有没有更好的方法来做到这一点?

强烈建议您提出任何其他意见,以使情节更具信息性。

【问题讨论】:

    标签: r plotly color-coding


    【解决方案1】:

    this 之后,我想出了一个解决方案来绘制不同形状的 Imp 和 Exp。颜色问题就像使用 colors 参数指定颜色一样简单。

    total_val$markers <- ifelse(total_val$type == "Exp", "circle", "diamond")
    library(plotly)
    p <- plot_ly(data = total_val, x = ~year, 
            y = ~value, z = ~category, color = ~category,
            colors = rainbow(length(levels(total_val$category))),
            text = ~paste('category:', category, '<br>value:', value, '<br>year:', year,
                          '<br>type:', type)) %>%
      add_markers() %>%
      layout(scene = list(xaxis = list(title = 'year'),
                          yaxis = list(title = 'Value'),
                          zaxis = list(title = 'Category')))
    
    p <- plotly_build(p)
    
    for (i in 1:length(p$x$data)) {
      values <- total_val$markers[total_val$category == levels(total_val$category)[i]]
      p$x$data[[i]]$marker$symbol <- values
    }
    p
    

    希望这会有所帮助!

    【讨论】:

    • 我在表格中添加了一个新项目和一个新类别 TOTAL。 TOTAL 应按原样显示在图表中,但现在不显示 Exp(圆形)和 Imp(菱形)之间的符号区别,而是每个符号都是圆形。你知道为什么会这样吗?
    • 再次您好,感谢您的提示,因为这是最后一个 for loop 的问题。当我做length(p$x) 时,这只是与您的数据类别数(10 个类别)的巧合,所以当您添加一个新类别时,length(p$x) 不等于类别数(11 个类别)。相反,length(p$x$data) 应该用于考虑情节的所有“层”。我已经用新的解决方案编辑了答案。
    • 顺便说一下,创建我使用的新 TOTAL 变量:total_val$year &lt;- factor(total_val$year) total_val$type &lt;- factor(total_val$type) total &lt;- aggregate(total_val$value, list(year = total_val$year, type = total_val$type), sum) names(total)[3] &lt;- "value" total$category &lt;- "TOTAL" total_val$markers &lt;- NULL # Erase the past markers total_val &lt;- rbind(total_val, total) total_val &lt;- total_val[order(total_val$year),] total_val$markers &lt;- ifelse(total_val$type == "Exp", "circle", "diamond")
    • 我尝试了您的解决方案,但遗憾的是它对我不起作用。这是新表。也许你看到了问题,谢谢到目前为止。除此之外,代码运行良好。 Table Update
    • 我不知道该说什么,新代码适用于我的新数据
    猜你喜欢
    • 2014-11-07
    • 2021-04-07
    • 2017-02-11
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 2021-03-05
    相关资源
    最近更新 更多