title: Open hyperlink on click on an ggplot/plotly chart
body: <p>This is a follow up question on the answer provided for <a href="https://stackoverflow.com/questions/42107098/add-onclick-open-hyperlink-event-to-an-html-widget-created-in-r">Add onclick open hyperlink event to an html widget created in R</a>. Consider the following example:</p>
  
  <pre><code>library(ggplot2)
  library(plotly)
  library(htmlwidgets)
  library(htmltools)
  myData &lt;- data.frame(
    x=c(1,2,3), 
    y=c(3,2,1),
    label=c("Google", "Bing", "R"),
    category=c("search", "search", "other"),
    urls=c("http://google.de", "http://bing.com", "http://r-project.org")
  )
  
  f &lt;- function(p) {
    ply &lt;- ggplotly(p)
    javascript &lt;- HTML(paste("
     var myPlot = document.getElementById('", ply$elementId, "');
     myPlot.on('plotly_click', function(data){
     var urls = ['", paste(myData$urls, collapse = "', '"), "'];
     window.open(urls[data.points[0].pointNumber],'_blank');
     });", sep=''))  
    prependContent(ply, onStaticRenderComplete(javascript))
  }
  </code></pre>
  
  <p>This works as expected - a click on any point opens the corresponding url:</p>
  
  <pre><code>f(ggplot(myData, aes(x=x, y=y)) + geom_point(aes(text=label)))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/wufT1.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/wufT1.gif" alt="enter image description here"></a></p>
  
  <p>This does not work as expected - the indices do not match anymore:</p>
  
  <pre><code>f(ggplot(myData, aes(x=x, y=y)) + geom_point(aes(text=label, color=category)))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/TNA45.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/TNA45.gif" alt="enter image description here"></a></p>
  
  <p>The plotly objects differ and it seems as if <code>pointNumber</code> does not hold the absolute index of all points any more, but the index within a (color) grouping.  </p>
  
  <p>Any ideas on how to adapt the example so that it works for general use cases also with color/fill groupings? </p>