[英]3D density plot with multiple groups ( regression lines)
I would like to create plot something like this : 我想创建这样的情节:
Here we have two variables X = midyear and Y = yearend. 这里我们有两个变量X =年中和Y =年底。 I would like to create density plot for each level of X for Y.
我想为Y的每个级别创建密度图。
I could go to this point, but do not look pretty like what I have in mind particularly angle of the plot and line. 我可以到这一点,但看起来不像我想到的特别是情节和线条的角度。 Any idea to make such twist or other packages that can do.
有任何想法可以做这样的扭曲或其他包装。
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")
Edits: Using rgl package 编辑:使用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")
You might be better off with a 2-dimensional graph that will show the distributions without visual distortion. 使用二维图表可能会更好,这将显示没有视觉失真的分布。 For example, here's a regression line superimposed on a violin plot, so you get both the regression line and the densities on a 2D graph.
例如,这里是叠加在小提琴图上的回归线,因此您可以在2D图上获得回归线和密度。 The code below shows both linear and quadratic polynomial fits to the data:
下面的代码显示了线性和二次多项式拟合数据:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.