1

I conducted IQ test on the same group (100 students) over 5 five years. like:

In 2005, Jack's IQ is 100, Amy' IQ is 99, John...

in 2006, Jack's IQ is 105, Amy' IQ is 95, John...

...

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? enter image description here

enter image description here

above is the 2D chart. 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

nan
  • 401
  • 4
  • 13
  • year2005 is the raw data. I think if I want to plot the density, I would do: `density5=density(year2005)` – nan Sep 30 '18 at 11:56
  • Have you considered plotting the year/time on the x-axis and the IQ on the y-axis? You can either display the raw points or summarize them with e.g. a violin- or boxplot. – Jannik Buhr Sep 30 '18 at 23:09
  • @JannikBuhr ,Thank you for your suggestion. Violin would be a good way – nan Oct 01 '18 at 03:33

2 Answers2

1

Is there any reason you want to make a hard-to-read 3D plot instead of something like this?

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)

enter image description here

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?

Edit

Here is a very related stack overflow post. I am aware this is not 3D, like you request. 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).

Anders Ellern Bilgrau
  • 9,928
  • 1
  • 30
  • 37
  • Actually I know how to plot in 2D. I don't konw how to plot the interpolation but I can tell you why I insist in 3D plot: when the number of year increases, the denisty lines would be very close because the real data is not as sinificantly different as the example data. – nan Sep 30 '18 at 12:35
  • I would still argue considering your description, that the information is best depicted with the 2D approach and that the 3D approach will still be hard(er) to read. The phenomenon you describe will be easily visible in the "2D approach". – Anders Ellern Bilgrau Sep 30 '18 at 12:41
  • I put up a photo with my real data. And it is quite difficult to read the trend between year: if you want to see if there is any different as time goes by, it is very hard , because I have to figure out which colour is year 2005 and then which colour is 2006. But if I use a 3D chart, there is no difficulty – nan Sep 30 '18 at 13:09
  • 1
    I see what you mean that the 3D chart helps to tell the order of the series more easily. However, it makes it very difficult (if not impossible) to actually tell what the values are, which seems like a critical failing. I would suggest working to clarify the series in the 2D chart: 1) Map the colors using sequential colors and/or alpha, so perhaps the most recent years are boldest and the older ones are muted. 2) Use direct labeling using `ggrepel` or `directlabels` packages. – Jon Spring Sep 30 '18 at 17:09
  • @JonSpring Good points. To me, the critical failing is that the (faux) 'perspective' will give an impression of increasing values where there are none across years. The values are arguably of less importance. What does a density of say 0.2 tell you exactly anyway? It is the shape and relative heights of the curves that is interesting. – Anders Ellern Bilgrau Sep 30 '18 at 17:37
1

Thank @Anders Ellern Bilgrau for the website he/she suggested from which I learnt something about ggridges

I use ggridges to finish my work. Can get help from

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

enter image description here

Although it do not fully meet my requirement, it can help to analyse the change of the probability shape.

nan
  • 401
  • 4
  • 13