使用scatterplot3d在R中表示离散数据,遇到轴问题

时间:2013-07-15 20:21:44

标签: r plot

我正在努力在这个离散回报的图表中找到正确的轴,这些回报对于全球金融市场的不同部门同比变化。我正在使用的代码是:

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)

当我绘制它时,轴完全关闭。我如何(a)固定轴和(b)在这些点之间线性插值,如here。谢谢你的帮助。

修改

感谢David Martin,这些是我能够制作的最终图表。我的代码已更新。以下是图表: Heatmap of Probabilities Overlaid Line Graphs

1 个答案:

答案 0 :(得分:1)

您的轴标签是重叠的,因为您提供的标签数量是函数绘制“滴答”的两倍,但您要告诉它在每个刻度线上放置一个标签。您需要明确提供所需的刻度数,如下所示:

试试这个:

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
                    )

您发布的链接中的曲面并非完全“在点之间进行插值”,它是通过点的回归平面。如果这可以满足您的需求,请按照以下方式将其添加到您的图表中:

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

老实说,我认为在3D中绘制这些内容会令人困惑,并且会使您的数据难以阅读。我建议您考虑使用重叠的线图:

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