简体   繁体   中英

Representing discrete data in R using scatterplot3d, trouble with axes

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: 概率热图重叠线图

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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