Trying to reproduce a 3D picture in R of a Rate or a Proportion with plot3D

208 views Asked by At

I saw this 3D picture the other day in a journal that describes the evolution over time of Birth Rates between 1980-1999 by age. The vertical line is the birth rates. The two horizontal lines are, age, and year.

enter image description here

I really would like to reproduce one like this. I can imagine the data would look something like (simplified)

dta = cbind(c(2000, 2005, 2015), 
  c(15, 20, 25), 
  c(20, 24, 35))

colnames(dta) <- c('year', 'age', 'rate')

 year age rate
 2000  15   20
 2005  20   24
 2015  25   35

I searched for some 3D libraries and package plot3D came out. I tried to figure out how the function outer() works but I couldn't understand!

Do you have any ideas how I could reproduce a 3D plot like the one above?

2

There are 2 answers

5
user1436187 On

Try this:

library(graphics)
dta = cbind(c(2000, 2005, 2015), 
        c(15, 20, 25), 
        c(20, 24, 35))

colnames(dta) <- c('year', 'age', 'rate')
dta = as.data.frame(dta)
persp(dta$year, dta$age, matrix(runif(9),3,3), theta = 30, phi = 30, expand = 0.5, col = "lightblue",
      ltheta = 120, shade = 0.75, ticktype = "detailed",
      xlab = "year", ylab = "age", zlab = "rate")

Ref: demo(persp) in R

Note: The thirs argument (z) must be a matrix

0
Ivo Fugers On

I prefer the plot3d function from "rgl" package. Since you just have a few point, I adjusted the limits so that you can see them.

library("rgl") plot3d(dta, xlim=c(1990, 2030), ylim=c(10,30), zlim=c(15,40))