0

I am struggling with getting the axes right on this graph of discrete returns that change for different sectors of the global financial markets year on year. The code I am working with is:

library(scatterplot3d)
data = c(32, 10.7, 37.5, -10, -50.2, 31.7, 20.1, -5.9, 29.8, 1.3,    24.7, 33.4, 32.2, 39.5, -53.2, 78.6, 19, -18.3, 18.2, -9.6,     22.8, 6.5, 15.2, 18.1, -5.2, 22.0, 15.7, -1.8, 15.1, -6.2,    20.8, 14.1, 27, 11.8, -43, 32.4, 8.4, -11.7, 17.9, 4.5,    11.1, 7.9, 14.7, 10.1, -23, 23.6, 10.5, -1, 11.8, 2.9,     11.1, 2.8, 11.8, 1.9, -26.2, 58.2, 15.1, 5, 15.8, 1.4,    10.9, 4.9, 15.8, 5.5, -37, 26.5, 15.1, 2.1, 16, 13.8,    9.1, 21.4, 2.1, 16.2, -35.6, 18.9, 16.8, -13.3, -1.1, -10.5,    9.1, -4.4, 6.6, 9.5, 4.8, 6.9, 5.5, 5.6, 4.3, -4.8,     8.3, 3.0, 0.4, 11.6, -2.4, 11.4, 6.3, 13.6, 7.0, -7.4,    5.4, 18.4, 23.0, 31.3, 5.5, 24.0, 29.7, 10.2, 7, -27,     5, 2.2, 4.3, 5.1, -3.1, 16, 8.5, 8.4, 9.4, -3.6,    4.5, 3.5, 4.8, 3.4, -2.5, 12.9, 2.4, 10.7, 6.8, -2.7,      3.3, 3, 3.1, 9, 13.7, -3.6, 5.9, 9.8, 2, -2.1,     1.2, 3, 4.8, 4.8, 1.8, 0.1, 0.1, 0.1, 0.1, 0) # should have length 150, check

my.datamat = matrix(data, nrow = 10)
rownames = c("2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "YTD")
colnames = c("Global RE", "EM Equities", "EM Fixed", "Non US Eq", "60/40", "High Yield", "US Equities", "Nat. Res.", "Global Fixed", "TIPS", "Gold", "Inv. Grade", "Municipals", "Gov't Bonds", "Cash")
dimnames(my.datamat) = list(rownames, colnames)
my.datamat = t(my.datamat)


 plot(0,0, xlim=c(1,length(colnames(my.datamat)))
        ,ylim = range(my.datamat)
     , type='n'
     ,xaxt = "n", main = "Northern Trust Data (jmi4)", xlab  = "Year", ylab = "Return (%)")

sapply(1:length(colnames(my.datamat))
       ,function(i){lines(my.datamat[i,], col = i)} )

op = par(cex = .5)
legend("bottomright"
       , legend=rownames(my.datamat)
       ,lty=rep(1,length(rownames(my.datamat)))
       ,col=c(1:length(rownames(my.datamat)))
       )
par(op)

axis(1, at = 1:length(colnames(my.datamat))
      , labels=colnames(my.datamat))


 ### or do a heatmap
 quartz()
 library(gplots)
 my.heatmap = heatmap.2(my.datamat, Rowv = NA, Colv = NA, col = redblue(256), dendrogram = "none", scale = "column", main  = "Northern Trust Heatmap", key = TRUE, trace = "none", density.info = "none", keysize = 1)

When I plot this, the axes are quite off. How can I (a) fix the axes and (b) interpolate linearly between these points like here. Thanks for any help.

Edit

Thanks to David Martin, these are the final graphs I was able to produce. My code has been updated. Here are the graphs: Heatmap of Probabilities Overlaid Line Graphs

Community
  • 1
  • 1
Moderat
  • 1,462
  • 4
  • 17
  • 21
  • For (b), since your data is already arranged in a grid, you can try `persp(my.datamat)` or `library(lattice); wireframe(my.datamat)` or `library(rgl); persp3d(my.datamat)`. – Vincent Zoonekynd Jul 15 '13 at 20:52
  • You shouldn't update your code to reflect the sollution. A user with a similar problem might find this discussion in the future and have difficulty understanding the discussion without the context of the code that caused the original problem. – David Marx Jul 17 '13 at 13:56

1 Answers1

1

Your axis labels are overlapping because you're providing twice as many labels as the function is plotting "ticks," but you're telling it to place a label at each tick. You need to explicitly provide the number of ticks you want, like so:

Try this:

library(reshape2)
m = melt(my.datamat)

s3d = scatterplot3d(m, type = "h"
              , lwd = 5, pch = " "
              , x.ticklabs = rownames(my.datamat)
              , y.ticklabs = colnames(my.datamat)
              , color = grey(150:1 / 200), main = "Northern Trust Data"
              ,lab=c(length(rownames(my.datamat)),length(colnames(my.datamat))) # defines how many ticks should appear on each axis
                    )

The surface in the link you posted isn't exactly "interpolating between the points," it's a regression plane through the points. If that's acceptable for your needs, here's how you add it to your plot:

# regression plane
s3d$plane3d(lm(value~., data=nt.dat), lty.box="solid" ) 

Honestly though, I think plotting this in 3D is confusing and makes your data unnecessarily difficult to read. I'd suggest you consider using an overlaid linechart instead:

plot(0,0, xlim=c(1,length(colnames(my.datamat)))
        ,ylim = range(my.datamat)
     , type='n'
     ,xaxt = "n")

sapply(1:length(rownames(my.datamat))
       ,function(i){lines(my.datamat[i,]
                          , col=i)} )

legend("topright"
       , legend=rownames(my.datamat)
       ,lty=rep(1,length(rownames(my.datamat)))
       ,col=c(1:length(rownames(my.datamat)))
       )

axis(1, at = 1:length(colnames(my.datamat))
      , labels=colnames(my.datamat))
David Marx
  • 8,172
  • 3
  • 45
  • 66
  • Thanks very much for the help. Why only use `lab = rep(length(rownames...` and not take into account how many `colnames` there are? – Moderat Jul 15 '13 at 21:07
  • And I figured out that I actually just want to draw lines interpolating between each individual `Global Eq` etc. i.e., going in the direction of Var2. Should I use `abline` or `lines`? – Moderat Jul 15 '13 at 21:08
  • Frankly, I think the direction you are going with this isn't an effective way to visualize the data anyway. I've offered a headache-free alternative as an overlaid scatterplot (see update). It has fewer bells and whistles, but it's much easier to interpret. As a rule of thumb, I'd avoid plotting anything in 3D unless you have a very good reason. Unless the different time series you are plotting have some ordinal property that justifies the order you are plotting them, you are suggesting an additional dimension in your data that I don't believe exists by plotting it in a 3D matrix. – David Marx Jul 15 '13 at 21:26
  • thanks very much for the data representation suggestion. I ended up using your suggestion, and it came out quite nicely. For some reason, converting to `data.matrix` took the data in by column, so I had to `my.datamat = t(my.datamat)` and then it worked nicely. I'll post the final pictures in my question – Moderat Jul 17 '13 at 08:56