具有多个组的3D密度图(回归线)

我想创建这样的情节:

《具有多个组的3D密度图(回归线)》

这里我们有两个变量X =年中和Y =年底.我想为Y的每个级别创建密度图.

我可以到这一点,但看起来不像我想到的特别是情节和线条的角度.有任何想法可以做这样的扭曲或其他包装.

set.seed(1234)
m50 <- rnorm(10000, 51, 5)
d50 <- hist(m50, breaks = 100)$density
md50 <- hist(m50, breaks = 100)$mids

m70 <- rnorm(10000, 73, 5)
d70 <- hist(m70, breaks = 100)$density 
md70 <- hist(m70, breaks = 100)$mids

m90 <- rnorm(10000, 90,5)
d90 <- hist(m90, breaks = 100)$density 
md90 <- hist(m90, breaks = 100)$mids

density = c(d50, d70, d90)
yearend = c(md50, md70, md90)
midyear = c(rep(50, length(d50)), rep(70, length(d70)), rep(90, length(d90)))

df <- cbind(midyear, yearend,  density)
require(scatterplot3d)
s3d <- scatterplot3d(df, type = "h", color = "blue",
       angle = 80, scale.y = 0.7, xlim = c(40, 100), 
   zlim = c(0,0.2), pch = ".", main = "Adding elements")

《具有多个组的3D密度图(回归线)》

编辑:
使用rgl包

require(rgl)
s3d <- plot3d(df, type = "h", 
                     angle = 80, scale.y = 0.7, xlim = c(40, 100), 
                     zlim = c(0,0.15), pch = ".", main = "Adding elements")

《具有多个组的3D密度图(回归线)》

最佳答案 使用二维图表可能会更好,这将显示没有视觉失真的分布.例如,这里是叠加在小提琴图上的回归线,因此您可以在2D图上获得回归线和密度.下面的代码显示了线性和二次多项式拟合数据:

library(ggplot2)

# Fake data
set.seed(19)
dat = data.frame(yearend=c(rnorm(10000, 51, 6), 
                           rnorm(10000, 60, 5), 
                           rnorm(10000, 75, 5), 
                           rnorm(10000, 85, 4)),
                 midyear = c(rep(51,10000), rep(58,10000), 
                             rep(70,10000), rep(90,10000)))

ggplot(dat, aes(midyear, yearend)) +
  geom_violin(aes(group=midyear)) +
  stat_summary(fun.y=mean, geom="point", colour="red", size=3) +
  geom_smooth(method="lm", se=FALSE) +
  geom_smooth(method="lm", se=FALSE, formula=y~poly(x,2), 
              colour="red", lty=2)

《具有多个组的3D密度图(回归线)》

点赞