问题
Goal: I have an adjusted pairs
plot in R
and I would like to add a color key to it. The panel backgrounds in the pairs
plot show certain colors determined from a matrix of numbers. I would like to have a color key to the right of the pairs
plot which shows these colors and some labels indicating the numbers.
I found some ways how to add color keys to image()
plots (for example, image.plot
in the fields
package; colorbar
in the matlab
package; color.legend
in the plotrix
package). So, in principle, I would like to do something similar for my adjusted pairs
plot. However, I already run into trouble with reserving some space for the color key to be plotted to (can [or even should?] this be done with grid
?).
Here is a minimal example of a pairs
plot that has colored backgrounds. I also tried to adapt the example of color.legend
in plotrix
but this is more or less nonsense at the moment. Any ideas of how to do this in an elegant/flexible way?
count <- 0
color <- c("yellow", "orange", "red", "lightgreen", "darkgreen", "cyan", "lightblue",
"darkblue", "brown", "gray", "transparent", "transparent")
mypanel <- function(x, y, ...){
count <<- count+1
bg <- color[count]
ll <- par("usr")
rect(ll[1], ll[3], ll[2], ll[4], col=bg)
points(x, y, cex=0.5)
}
U <- matrix(runif(4*500), ncol=4)
## require(plotrix)
## par(mar=c(7,4,4,14)) # (bottom, left, top, right)
pairs(U, panel=mypanel, gap=0)
## color.legend(xl=10.2, yb=2, xr=11, yt=5,
## legend=c("bright","normal","dark"), rect.col=color, align="rb",
## gradient="y")
Info: The colors chosen above are determined from numbers (a vector of the same length). The numbers are all in between 0 and 1 and 0 (1) corresponds to the darkest (brightest) color. Those numbers should then determine the ticks and numbers printed at the color key (I don't need actual text labels -- I just tried that since the example of color.legend
used it). So a vector of numbers for colors
above could be c(0.95, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 1, 1)
(the larger the number the brighter the column). To be more precise: These values are p-values determined by goodness-of-fit tests for each pair and the dark color should point out small p-values. Indeed, the colors are not that evenly spaced, I only use some colors above the significance level of (for example) 0.05, and most of the colors below 0.05, so one can directly see which tests lead to rejection.
来源:https://stackoverflow.com/questions/9852343/how-to-add-a-color-key-to-a-pairs-plot