Adding custom tooltip to plotly using R and ggplot2 syntax

后端 未结 1 967
生来不讨喜
生来不讨喜 2021-01-21 16:22

I am trying to create a plotly plot from R using a ggplot object, which has custom labels.

#library(\'devtools\')
#install_github(\"ropensci/plotly\")
library(\'         


        
相关标签:
1条回答
  • 2021-01-21 16:42

    I've been looking at the same problem and I think what you need to do is something like this (via https://stackoverflow.com/a/27007513/829256 and h/t to @plotlygraphs on Twitter)

    # first use your Plotly connection and retrieve data for the ggiris plot you uploaded
    irisplot <- py$get_figure('username', n)  # where n = the number of this plot on your account
    
    # inspect the irisplot object
    str(irisplot)  # a list of 2
    
    # inspect irisplot$data
    str(irisplot$data)  # a list of 3, one list for each Species
    
    # overwrite 'text' for each Species list
    irisplot$data[[1]]$text <- labels[1:50]
    irisplot$data[[2]]$text <- labels[51:100]
    irisplot$data[[3]]$text <- labels[101:150]
    
    # re-upload to Plotly
    resp <- py$plotly(irisplot$data, kwargs = list(layout = irisplot$layout))
    
    # check out your new plot
    resp$url
    

    So the plot should now have a value from 'labels' for each data point, displayed as a tooltip with mouseover.

    You will presumably want to do something smarter in how you assign the labels to the points, but hopefully this gets you started.

    And thanks, I think working through this question will help me solve my own task too :-)

    0 讨论(0)
提交回复
热议问题