开发者

Plotly in R: How to reference and extract figure values?

I want to know how can I access, extract, and reference values from a plotly figure in R.

Consider, for example, the Sankey diagram from plotly's own site of which there is an abbreviated version here:

library(plotly)

fig <- plot_ly(
  
  type = "sankey",
  
  node = list(
    
    label = c("A1", "A2", "B1", "B2", "C1", "C2"),
    
    color = c("blue", "blue", "blue", "blue", "blue", "blue"),
    
    line = list()
    
  ),
  
  
  link = list(
    
    source = c(0,1,0,2,3,3),
    
    target = c(2,3,3,4,4,5),
    
    value =  c(8,4,2,8,4,2)
    
  )
  
)

fig

If I do View(fig) in Rstudio, a new tab opens titled . (I don't know why this instead of 'fig'). In this tab I can go to x > visdat > 'strig of letters and numbers that is a function?' > attrs > node > x (as shown bellow).

Plotly in R: How to reference and extract figure values?

Here all the x coordinates for the Sankey nodes appear. I want to access these values so I can use them somewhere else. How do I do this? If I click on the right side of the Rsutudio tab to copy the code to console I get:

environment(.[["x"]][["visdat"]][["484c3ec36899"]])[["attrs"]][["node"开发者_StackOverflow中文版;]][["x"]]

which obviously doesn't work as there is no object named ..

In this case I have tried fig$x$visdat$`484c3ec36899`() but I cant do fig$x$visdat$`484c3ec36899`()$attr, and I don't know what else to do.

So, how can I access any value from a plotly object? Any documentation referencing this topic would also be helpful.

Thanks.


You can find the documentation of the data structure of plotly in R here: https://plotly.com/r/figure-structure/

To check the data structure you can use str(fig):

List of 8
 $ x            :List of 6
  ..$ visdat  :List of 1
  .. ..$ a3b8795a4:function ()  
  ..$ cur_data: chr "a3b8795a4"
  ..$ attrs   :List of 1
  .. ..$ a3b8795a4:List of 6
  .. .. ..$ node        :List of 3
  .. .. .. ..$ label: chr [1:6] "A1" "A2" "B1" "B2" ...
  .. .. .. ..$ color: chr [1:6] "blue" "blue" "blue" "blue" ...
  .. .. .. ..$ line : list()
  .. .. ..$ link        :List of 3
  .. .. .. ..$ source: num [1:6] 0 1 0 2 3 3
  .. .. .. ..$ target: num [1:6] 2 3 3 4 4 5
  .. .. .. ..$ value : num [1:6] 8 4 2 8 4 2
  .. .. ..$ alpha_stroke: num 1
  .. .. ..$ sizes       : num [1:2] 10 100
  .. .. ..$ spans       : num [1:2] 1 20
  .. .. ..$ type        : chr "sankey"
  ..$ layout  :List of 3
  .. ..$ width : NULL
  .. ..$ height: NULL
  .. ..$ margin:List of 4
  .. .. ..$ b: num 40
  .. .. ..$ l: num 60
  .. .. ..$ t: num 25
  .. .. ..$ r: num 10
  ..$ source  : chr "A"
  ..$ config  :List of 1
  .. ..$ showSendToCloud: logi FALSE
  ..- attr(*, "TOJSON_FUNC")=function (x, ...)  
 $ width        : NULL
 $ height       : NULL
 $ sizingPolicy :List of 6
  ..$ defaultWidth : chr "100%"
  ..$ defaultHeight: num 400
  ..$ padding      : NULL
  ..$ viewer       :List of 6
  .. ..$ defaultWidth : NULL
  .. ..$ defaultHeight: NULL
  .. ..$ padding      : NULL
  .. ..$ fill         : logi TRUE
  .. ..$ suppress     : logi FALSE
  .. ..$ paneHeight   : NULL
  ..$ browser      :List of 5
  .. ..$ defaultWidth : NULL
  .. ..$ defaultHeight: NULL
  .. ..$ padding      : NULL
  .. ..$ fill         : logi TRUE
  .. ..$ external     : logi FALSE
  ..$ knitr        :List of 3
  .. ..$ defaultWidth : NULL
  .. ..$ defaultHeight: NULL
  .. ..$ figure       : logi TRUE
 $ dependencies :List of 5
  ..$ :List of 10
  .. ..$ name      : chr "typedarray"
  .. ..$ version   : chr "0.1"
  .. ..$ src       :List of 1
  .. .. ..$ file: chr "htmlwidgets/lib/typedarray"
  .. ..$ meta      : NULL
  .. ..$ script    : chr "typedarray.min.js"
  .. ..$ stylesheet: NULL
  .. ..$ head      : NULL
  .. ..$ attachment: NULL
  .. ..$ package   : chr "plotly"
  .. ..$ all_files : logi FALSE
  .. ..- attr(*, "class")= chr "html_dependency"
  ..$ :List of 10
  .. ..$ name      : chr "jquery"
  .. ..$ version   : chr "1.11.3"
  .. ..$ src       :List of 1
  .. .. ..$ file: chr "lib/jquery"
  .. ..$ meta      : NULL
  .. ..$ script    : chr "jquery.min.js"
  .. ..$ stylesheet: NULL
  .. ..$ head      : NULL
  .. ..$ attachment: NULL
  .. ..$ package   : chr "crosstalk"
  .. ..$ all_files : logi TRUE
  .. ..- attr(*, "class")= chr "html_dependency"
  ..$ :List of 10
  .. ..$ name      : chr "crosstalk"
  .. ..$ version   : chr "1.1.0.1"
  .. ..$ src       :List of 1
  .. .. ..$ file: chr "www"
  .. ..$ meta      : NULL
  .. ..$ script    : chr "js/crosstalk.min.js"
  .. ..$ stylesheet: chr "css/crosstalk.css"
  .. ..$ head      : NULL
  .. ..$ attachment: NULL
  .. ..$ package   : chr "crosstalk"
  .. ..$ all_files : logi TRUE
  .. ..- attr(*, "class")= chr "html_dependency"
  ..$ :List of 10
  .. ..$ name      : chr "plotly-htmlwidgets-css"
  .. ..$ version   : chr "1.52.2"
  .. ..$ src       :List of 1
  .. .. ..$ file: chr "htmlwidgets/lib/plotlyjs"
  .. ..$ meta      : NULL
  .. ..$ script    : NULL
  .. ..$ stylesheet: chr "plotly-htmlwidgets.css"
  .. ..$ head      : NULL
  .. ..$ attachment: NULL
  .. ..$ package   : chr "plotly"
  .. ..$ all_files : logi FALSE
  .. ..- attr(*, "class")= chr "html_dependency"
  ..$ :List of 10
  .. ..$ name      : chr "plotly-main"
  .. ..$ version   : chr "1.52.2"
  .. ..$ src       :List of 1
  .. .. ..$ file: chr "htmlwidgets/lib/plotlyjs"
  .. ..$ meta      : NULL
  .. ..$ script    : chr "plotly-latest.min.js"
  .. ..$ stylesheet: NULL
  .. ..$ head      : NULL
  .. ..$ attachment: NULL
  .. ..$ package   : chr "plotly"
  .. ..$ all_files : logi FALSE
  .. ..- attr(*, "class")= chr "html_dependency"
 $ elementId    : NULL
 $ preRenderHook:function (p, registerFrames = TRUE)  
 $ jsHooks      : list()
 - attr(*, "class")= chr [1:2] "plotly" "htmlwidget"
 - attr(*, "package")= chr "plotly"

You could extract the coordinates with:

unlist(fig$x$attrs)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜