Manussawee Sukunta
2011-Feb-22 16:03 UTC
[R] image.plot results in different colors than legends
To R community: I've been pulling my hair for the past few days over this issue now, and I may be at the point where I just can't spot my error. So I thought to reach out to the community and hope someone could kindly help me. I'm trying to create a correlation matrix and plot it. However, I'm trying to create a hybrid of correlation matrix from two different periods, say longer term vs shorter term correlations. I find correlation matrix for both periods and then replace one with the other, separating by the diagonal. Say the sample, after merging the data sets, is:> sample1 2 3 4 5 1 1.0000000 -0.7929869 0.5533907 0.3394069 -0.88815356 2 0.4344728 1.0000000 -0.3423528 -0.1710087 0.80005311 3 0.5271340 -0.2136791 1.0000000 0.6494039 -0.33841405 4 0.2199821 -0.3731925 0.4257412 1.0000000 -0.08108044 5 0.7100858 0.5146790 0.3749563 -0.2263762 1.00000000 I then generate my plot as follows (adaptation from assetsCorImagePlot function in fAssets package): n <- ncol(sample) names <- colnames(sample) image.plot(x=1:n, y=1:n, z=sample[,n:1], col=rainbow(20, start=0, end=.6, alpha=.8), axes=F, main='', xlab='', ylab='') X <- t(sample) require(fields) coord <- grid2d(1:n, 1:n) for (i in 1:(n * n)) { text(coord$x[i], coord$y[n * n + 1 - i], round(X[coord$x[i], coord$y[i]], digits = 2), col = colors()[285], cex = 0.7) } axis(2, at = n:1, labels = names, las = 2, cex.axis=0.8) axis(3, at = 1:n, labels = names, las = 0, cex.axis=0.8) box() invisible() I get a nice plot with colors ranging from blue (being 1) to red (being negative). However, after looking at the plot I notice that some of the grid's color is not in accordance to the legend shown. For example, 0.71 would be shown as red, which should indicate negative number according to the legend. Or 0.65 and -0.35 share the same color on the image plot, which shouldn't be. I'm not sure how to solve this issue. I searched the internet and R site search, but nothing that I could find had help. I hope someone could help me here. I'd greatly appreciate it. Thank you, Manussawee p.s. I'm using Windows 32-bit R.2.10-1. [[alternative HTML version deleted]]
Manussawee Sukunta
2011-Feb-22 20:24 UTC
[R] image.plot results in different colors than legends
By luck I was able to solve my own problem! (Big "pheeeew.") As a reference, I simple change my value of z in image.plot to be X[,n:1], where X = t(lower). Everything else remains the same. Cheers, Manussawee On Tue, Feb 22, 2011 at 10:03 AM, Manussawee Sukunta < msukunta@illinoisalumni.org> wrote:> To R community: > > I've been pulling my hair for the past few days over this issue now, and I > may be at the point where I just can't spot my error. So I thought to reach > out to the community and hope someone could kindly help me. > > I'm trying to create a correlation matrix and plot it. However, I'm trying > to create a hybrid of correlation matrix from two different periods, say > longer term vs shorter term correlations. I find correlation matrix for > both periods and then replace one with the other, separating by the > diagonal. Say the sample, after merging the data sets, is: > > > sample > 1 2 3 4 5 > 1 1.0000000 -0.7929869 0.5533907 0.3394069 -0.88815356 > 2 0.4344728 1.0000000 -0.3423528 -0.1710087 0.80005311 > 3 0.5271340 -0.2136791 1.0000000 0.6494039 -0.33841405 > 4 0.2199821 -0.3731925 0.4257412 1.0000000 -0.08108044 > 5 0.7100858 0.5146790 0.3749563 -0.2263762 1.00000000 > > I then generate my plot as follows (adaptation from assetsCorImagePlot > function in fAssets package): > n <- ncol(sample) > names <- colnames(sample) > image.plot(x=1:n, y=1:n, z=sample[,n:1], col=rainbow(20, start=0, end=.6, > alpha=.8), axes=F, main='', xlab='', ylab='') > X <- t(sample) > require(fields) > coord <- grid2d(1:n, 1:n) > for (i in 1:(n * n)) { > text(coord$x[i], coord$y[n * n + 1 - i], round(X[coord$x[i], coord$y[i]], > digits = 2), col = colors()[285], cex = 0.7) > } > axis(2, at = n:1, labels = names, las = 2, cex.axis=0.8) > axis(3, at = 1:n, labels = names, las = 0, cex.axis=0.8) > box() > invisible() > > I get a nice plot with colors ranging from blue (being 1) to red (being > negative). However, after looking at the plot I notice that some of the > grid's color is not in accordance to the legend shown. For example, 0.71 > would be shown as red, which should indicate negative number according to > the legend. Or 0.65 and -0.35 share the same color on the image plot, which > shouldn't be. > > I'm not sure how to solve this issue. I searched the internet and R site > search, but nothing that I could find had help. I hope someone could help > me here. I'd greatly appreciate it. > > Thank you, > Manussawee > > p.s. I'm using Windows 32-bit R.2.10-1. > >[[alternative HTML version deleted]]