Skip to content

"text"-function: adding text in an x,y-plot

3 messages · Helmut Kudrnovsky, Uwe Ligges, John Fox

#
Hello R-friends,

i have a question to the "text"-function.

a little test-dataset for better understanding:

-the dataset was imported with read.table(....,header=TRUE)
s1-s10 are the samplenames

     var1 var2 var3
s1     1    1    2
s2     2    3    1
s3     2    2    3
s4     5    4    3
s5     4    2    3
s6     6    3    2
s7     8    5    4
s8     7    2    1
s9     9    3    2
s10    8    6    4

-then i??ve performed a pincipal component analysis with prcomp

-for displaying the result in a x,y-graph i transformed out.pca$x into the 
dataframe points
  points <- out.pca$x
 > points
            PC1         PC2        PC3
s1  -4.7055777 -0.14781544 -0.3683602
s2  -3.1854599  0.19661476  1.5212455
s3  -3.2687980  0.78193513 -0.6352458
s4   0.2278948  1.00061498  0.2164108
s5  -1.4382847  0.02500633 -0.9114340
s6   0.6216283 -0.68606440  0.2071083
s7   3.4951878  1.17343675 -0.3266629
s8   1.0153619 -2.37274378  0.1978058
s9   3.3673983 -1.82145761 -0.2071739
s10  3.8706492  1.85047328  0.3063065

- with plot(points$PC1, points$PC2, type="n") i start a graph without 
displaying anything at the x,y-coordinates

- i want now to display the samplenames at the right x,y-coordinate;  i 
generated a dataframe called lab with the samplenames

 > lab
     V1
1   s1
2   s2
3   s3
4   s4
5   s5
6   s6
7   s7
8   s8
9   s9
10 s10

- i??ve studied the "text"-helppage and so i tried the following:

text(pca$PC1, pca$PC2, labels=lab)

- in the plot there is now c(1,3,4,5,6,7,8,9,10,2) displayed instead of s1-s10

is out there any idea what i'am doing wrong? is there maybe another way of 
displaying the samplenames at the right x,y-coordinate?

greetings from the sunny tirol with thanks in advance
helli

platform i386-pc-mingw32
arch     i386
os       mingw32 - win xp
system   i386, mingw32
status
major    2
minor    1.0
year     2005
month    04
day      18
language R
#
Helmut Kudrnovsky wrote:

            
I think points should be a matrix rather than a list or data.frame, 
hence the code above won't work. Instead:


  plot(points[,"PC1"], points[,"PC2"], type="n")
Hmm, what is pca ???


  lab <- rownames(points)
  text(points[,"PC1"], points[,"PC2"], labels=lab)

works for me.
Quite probably you lab was a *factor* ...
We cannot say what you did wrong when constructing your plot, but we 
know that your descibtion above is wrong, because it does not even work 
to generate the empty plot the way ...

Please read the posting guide and rethink at least one time whether the 
code you are giving us is reproducible. Your code was not.

Uwe Ligges
#
Dear Helmut,

The problem is that you're implicitly coercing the entire data frame lab to
character, producing
[1] "c(1, 3, 4, 5, 6, 7, 8, 9, 10, 2)"

The numbers themselves come from the numeric coding of the factor V1 in this
data frame; as.numeric(lab$V1) shows you what's going on.

Instead you could specify labels=lab$V1 in the text() command. But why put
the row names in a data frame in the first place; why not simply use
labels=rownames(out.pca)?

I hope this helps,
 John

--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
--------------------------------