2D Histogram Contour Plot with scatter plot overlay in R

Hi,

I have a largish dataset with ~ 30,000 data points and would like show both

  1. the overall distribution of the data (eg using a 2D contour plot) and
  2. highlight a subset of individual data points.

For example, I would like to combine a 2D histogram contour plot and highlight (only a subset of) data points as a scatter plot, similar to the python example on the plotly website.

Perhaps somebody has an example of how to achieve this with the R API as well? (The plotly examples also contain marginal histograms, which I don’t need.)

Many thanks for any pointer!
Thomas

Hey @sandmann

you can use add_histogram2dcontour() for the contour and add_markers() for the scatter plot:

x <- rnorm(30000)
y <- rnorm(30000)    
p <- plot_ly(x = x, y = y) %>% 
      add_histogram2dcontour(showscale=FALSE, ncontours=20, colorscale='hot', 
                             contours = list(coloring='heatmap')) %>%
      add_markers(x = x[1:500], y = y[1:500], marker=list(size=2), color=I("black"), 
                  opacity=.5) %>%
      layout(xaxis=list(showgrid=FALSE, zeroline=FALSE), 
             yaxis=list(showgrid=FALSE, zeroline=FALSE),
             showlegend=FALSE)

Great, thanks a lot for your quick response and the clear example @bcd !