Question

I have a small data set that I'm plotting up some pair plots for. I have few enough data points that I'd like to add text labels to each point. Is there a way to do this? I've seen some examples of color coding, but I'd much rather have text labels from a column in my dataframe. For the example below I'd like to plot rr, position.cm, and precip and have the points labeled by storm. Thanks!

example <- structure(list(storm = structure(c(1L, 10L, 12L, 13L, 15L, 16L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), .Label = c("1", "2", "3", "4", 
"5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16"), class = "factor"), position.cm = c(-50.1260416666667, 
-11.5458333333333, -11.4005208333333, -13.303125, -8.91302083333333, 
-8.9984375, -20.0125, -18.7666666666667, -30.4583333333333, -52.8177083333333, 
-56.9135416666667, -18.5229166666667, -19.94375, -11.5765625), 
    precip = c(27.7, 11, 17.8, 60, 48.6, 62.6, 48.5, 38.4, 40.6, 
    16.6, 45.2, 21.8, 39.8, 68.4), rr = c(6.2182629900722, 19.5252223545455, 
    6.50223807303371, 94.2317387291667, 85.9112139917695, 110.894968051118, 
    56.2344034298969, 29.559527546224, 1.74602332820197, 0, 11.3076659103982, 
    28.3121063302752, 50.5574166212312, 63.6394516524123)), .Names = c("storm", 
"position.cm", "precip", "rr"), class = "data.frame", row.names = c(107L, 
108L, 110L, 111L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 
121L, 122L))

pairs(example[, c("position.cm", "rr", "precip")])
Was it helpful?

Solution

This listserv chain discusses labeling points using 'pairs()'.

This is the code example they give:

pairs(matrix(rnorm(15), ncol=3), pch=21, bg="grey", cex=4, 
      panel=function(x, y, ...) { points(x, y, ...); 
                                  text(x, y, letters[1:5]) })

For your example:

pairs(example[, c("position.cm", "rr", "precip")], pch=21, bg="grey", cex=4, 
      panel=function(x, y, ...) { points(x, y, ...); 
                                  text(x, y, example[,"storm"]) })

OTHER TIPS

Depending on the purpose, you might want to consider an interactive pairs plot. You could use the pairsD3() function or the shiny interface from the pairsD3 R package, which provides a way to interact with (potentially large) scatter plot matrices by selecting a few variables at a time. Hovering over the points reveals a tooltip with information about the observation (row name and optionally a grouping variable).

An example with the iris data set:

install.packages("pairsD3")
require("pairsD3")
shinypairs(iris)

For your example:

pairsD3(example[, c("position.cm", "rr", "precip")],group=example$storm)

More reference here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top