如何在R中绘制双变量分布的密度图?

时间:2018-08-19 16:55:56

标签: r ggplot2 density-plot

我有一个具有不同相关性的双变量数据。我想为他们显示3d的密度图。如何在R中做到这一点?

n = 2000; m1 = 0; m2 = 0; s1 =1; s2 = 1;
plotcor = function(r){
  x = rnorm(n,m1,s1)
  y = s2*r*(x-m1)/s1+m2 + s2*rnorm(n,0,sqrt(1-r^2))
  plot(x,y, main = paste('Correlation =', r))
  abline(lm(y~x), col = 'red')
}
par(mfrow=c(3,3),oma=c(0,0,2,0))
for (i in c(-1,-.75,-.5,-0.25,0,0.25,0.5,.75,1)){
  plotcor(i)
}

enter image description here 与上述类似,我想编写一个函数,给我像this

这样的密度图

0 个答案:

没有答案