plotly.js介绍

简要工作流程

在创建plotly图像的时候,plotly_build()函数先将plot_ly的内容转换为一个R list 然后plotly_json()函数将其转换为JSON格式之后渲染为网络图片。具体流程如下图。

plotly_json

plotly_json将内容转化为JSON格式

plotly.js中,一张图片包含两个重要组成部分:

  • data(又叫trace)

  • layout

data

data定义的是数据和图之间的映射关系,每一个data下必须要有一个type 定义了映射的图表类型以及其它参数。

b <- plotly_build(p)
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram
length(b$x$data)
## [1] 8
purrr::map_chr(b$x$data, "name")
## [1] "IF"   "VVS1" "VVS2" "VS1"  "VS2"  "SI1"  "SI2"  "I1"
unique(purrr::map_chr(b$x$data, "type"))
## [1] "histogram"

再然后,我们会发现colorcolorsplotly_build() 的结果中并没有,这是因为build函数已经将其转换为了plotly.js所需要的信息 (marker.color 等)。