Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - how to use contents of one vector as the symbol in a plot?

Tags:

plot

r

I have a two vectors of numbers of equal length. How do I plot the first vector while using the corresponding element in the second vector as the printing character? (Background: I sorted the first column and the second column holds the original indices. I want to use the indices as the printable character so that I can see which data points are outliers, since each number represents one run of data).

 > x
$x
 [1]   25   29   30   34   38  572  700  733  870  879  899  934  982 1054 1135 1258
[17] 1315 1491 1685 1700 2069 2131 2284 3498 3506 4467 4656 5633 6642 8348

$ix
 [1] 23  3 18 30 13  8  4 14 11 17 12 29  9 15 19 16  7  1 20  2  6 28 21 10  5 22 24 26
[29] 25 27

First vector is x$x, second vector is x$ix (results of calling sort with index.return = TRUE)

I've tried plot(x$x, pch=str(x$ix)) but that treats x$ix numerically. If this were Python I would do something like strings = [str(x) for x in x$ix]. but this is R and I've forgotten most of what I used to know.

I found that you can do as.character(x$ix) in order to get the strings,

> as.character(x$ix)
 [1] "23" "3"  "18" "30" "13" "8"  "4"  "14" "11" "17" "12" "29" "9"  "15" "19" "16"
[17] "7"  "1"  "20" "2"  "6"  "28" "21" "10" "5"  "22" "24" "26" "25" "27"

and I can use this as the input to pch. But only the first character is used (and according to the docs, that's normal).

I know there's a way to do this; I did it in college. But I can't for the life of me remember how I did it.

Chart without labels: alt text

Chart with labels, but incorrect: alt text

like image 986
I82Much Avatar asked Feb 11 '10 22:02

I82Much


1 Answers

This should work:

x = 1:4
y = x
plot(x, y, ann=F, axis=F, col="blue", pch=16)
text(x, y, labels=c("1st", "2nd", "3rd", "4th"), col="red", pos=c(3,4,4,1), offset=0.6)

Just convert your non-data vector (the one containing the labels) to a character vector: labels = as.character(label_vector)

and then substitute this for the third argument in line 4 above.

The 'Text' function is fairly versatile because of the various arguments you can pass in--e.g., (as in the example above) you can set the text to a different color than your data points using "col"; You can also specify the position (relative to the data point annotated by a given text label) for each text label separately. That's often useful to keep text labels from, for instance, overlapping one of the axes, which is what happened the first time i ran this example without setting 'pos'. So by setting 'pos' (as c(3, 4, 4, 1)) i set the position of the text labels as "above", "right", "right", and "below"--moving the first data point up so it doesn't hit the bottom x-axis, and moving the fourth one down so it doesn't hit the top x-axis. Additionally, using 'offset' (which has a default value of 0.5) you can specify the magnitude of the position adjustment.

like image 122
doug Avatar answered Oct 12 '22 21:10

doug