I have 2 vectors $1000 \times 1$, lets call them $y_{1}$ and $y_{2}$. Each vector represents a normal distribution with certain mean and variance.

I plot the contour plot using the following R code:

x <- y1
y <- y2
s <- subplot(
  plot_ly(x = x, type = "histogram", showlegend=FALSE),
  plot_ly(x = x, y = y, type = "histogram2dcontour", showlegend=FALSE),
  plot_ly(y = y, type = "histogram", showlegend=FALSE),
  nrows = 2, heights = c(0.2, 0.8), widths = c(0.8, 0.2),
  shareX = TRUE, shareY = TRUE, titleX = FALSE, titleY = FALSE

and i get the following plot

contour plot

What I would like to do now, is to plot a 3D contour plot (so that I can actually see the "mountain" that is created after plotting the histogram of $y_{1}$ against the histogram of $y_{2}$). So in the z-axis I would like to have the frequencies of the values. Any suggestions ?

  • $\begingroup$ Please make a reproducible example that we can cut and paste and run. Also, state which packages you are using, because this looks like you are using a "plotly" package of some sort. Also also, try posting to stackoverflow unless you have a real data science question. $\endgroup$
    – Spacedman
    Jul 18, 2016 at 13:07

1 Answer 1


This plot not use frequencies but kernel density:

freqz <- with(data.frame(x,y), MASS::kde2d(x, y, n = 50))
with(freqz, plot_ly(x = x, y = y, z = z, type = "surface")) 

enter image description here

  • $\begingroup$ Thank you Robert. Is there any link between the kernel density and the frequencies ? $\endgroup$
    – quant
    Jul 25, 2016 at 8:21
  • $\begingroup$ And more importantly, what is the kernel density ? $\endgroup$
    – quant
    Jul 25, 2016 at 8:36
  • $\begingroup$ Kernel density is like a histogram but with smoothed relative frequencies. See here stats.stackexchange.com/a/43234/77852 $\endgroup$
    – Robert
    Jul 25, 2016 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.