简体   繁体   English

ggplot2在一张3d图表中绘制了几条密度线

[英]ggplot2 plot several density lines in one 3d chart

I conducted IQ test on the same group (100 students) over 5 five years. 在过去的5年中,我对同一小组(100名学生)进行了智商测试。 like: 喜欢:

In 2005, Jack's IQ is 100, Amy' IQ is 99, John... 2005年,杰克的智商为100,艾米的智商为99,约翰...

in 2006, Jack's IQ is 105, Amy' IQ is 95, John... 在2006年,杰克的智商为105,艾米的智商为95,约翰...

... ...

I want to estimate the density of IQ in different years. 我想估算不同年份的智商密度。 And plot the density lines of different years in one chart. 并在一张图中绘制不同年份的密度线。 Here is some data example. 这是一些数据示例。

year2005<-rnorm(100,100,2)
year2006<-rnorm(100,98,1)
year2006<-rnorm(100,101,4)

How can I draw them like the following chart? 我如何像下图所示绘制它们? 在此处输入图片说明

在此处输入图片说明

above is the 2D chart. 上面是2D图表。 It is very difficult to read the trend between years beacuse I have to know red is 2016 and black is 2015. And there is no difficult with 3D and this is the reason I insist in 3D 阅读几年之间的趋势非常困难,因为我必须知道红色是2016年,黑色是2015年。3D并不困难,这就是我坚持使用3D的原因

Is there any reason you want to make a hard-to-read 3D plot instead of something like this? 您是否有任何理由想要制作难以阅读的3D图而不是类似的图?

library("ggplot2")

n <- 100
year2005<-rnorm(n,100,2)
year2006<-rnorm(n,98,1)
year2007<-rnorm(n,101,4)

dt <- data.frame(year = rep(c("2005", "2006", "2007"), each = n),
                 value = c(year2005, year2006, year2007))

ggplot(dt, aes(value, fill = year, colour = year)) +
  geom_density(alpha = 0.1)

在此处输入图片说明

See more examples here. 在此处查看更多示例。

Even if you insist on a 3D plot like you show, how you do propose to do the interpolation between the years? 即使您坚持使用所显示的3D图,您如何建议在年份之间进行插值?

Edit 编辑

Here is a very related stack overflow post. 是一个非常相关的堆栈溢出文章。 I am aware this is not 3D, like you request. 我知道这不是3D,就像您的要求一样。 But in order to do so, you still need to consider how to interpolate between years (which seem to be a categorical variable in your case). 但是,为此,您仍然需要考虑如何在年份之间进行插值(在您的情况下,这似乎是一个绝对变量)。

Thank @Anders Ellern Bilgrau for the website he/she suggested from which I learnt something about ggridges 感谢@Anders Ellern Bilgrau他/她建议的网站,我从该网站中学到了关于ggridges的知识

I use ggridges to finish my work. 我用ggridges完成我的工作。 Can get help from 可以从获得帮助

https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html

library(devtools)
install_github("clauswilke/ggridges")
ggplot(dt, aes(x = value, y = year, fill =0.5-abs(0.5-..ecdf..))) +
  stat_density_ridges(geom="density_ridges_gradient",scale=1, calc_ecdf=TRUE,
                      jittered_points = T) + 
  scale_fill_viridis(name = "Tail Probability", direction = -1) 
  +theme_minimal()+
  theme(plot.title=element_text(size=17,color="black",face="bold",hjust=0.5), 
        axis.title.y = element_blank())+ 
  labs(x="...")+
  scale_y_discrete(expand = c(0.01, 0)) +   
  scale_x_continuous(breaks=seq(0,240,40),expand = c(0, 0)) 

在此处输入图片说明

Although it do not fully meet my requirement, it can help to analyse the change of the probability shape. 尽管它不能完全满足我的要求,但可以帮助分析概率形状的变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM