gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:06:01 25 4
gpt4 key购买 nike

我想创建这样的情节:

Nether et al

这里我们有两个变量 X = midyear 和 Y = yearend。我想为 Y 的 X 的每个级别创建密度图。

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

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")

enter image description here

编辑:
使用 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")

enter image description here

最佳答案

使用二维图可能会更好,该图将显示没有视觉失真的分布。例如,这是一条叠加在 fiddle 图上的回归线,因此您可以在二维图形上同时获得回归线和密度。下面的代码显示了对数据的线性和二次多项式拟合:

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)

enter image description here

关于r - 具有多个组的 3D 密度图(回归线),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32414515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com