使用数据框在r中的排序图中的着色点

时间:2012-08-16 15:58:00

标签: r text labels

使用以下data.frame:

df<-data.frame("sites"=as.character(1:20),"type"=c(rep("small",10),rep("large",10)))

   sites  type
1      1 small
2      2 small
3      3 small
4      4 small
5      5 small
6      6 small
7      7 small
8      8 small
9      9 small
10    10 small
11    11 large
12    12 large
13    13 large
14    14 large
15    15 large
16    16 large
17    17 large
18    18 large
19    19 large
20    20 large

我想在下面的圣职地图中用标签(即“小”,“大”)为文本标签(即1-20)着色:

library(vegan)
library(stats)

data(dune)
dist <- vegdist(wisconsin(dune))

#Ordinate data
pc<-cmdscale(dist, k=10, eig=TRUE, add=TRUE, x.ret =TRUE)   

#Create ordination plot     
quartz(title="PCoA on coral data")
fig<-ordiplot(scores(pc)[,c(1,2)], type="t", main="PCoA")

enter image description here

1 个答案:

答案 0 :(得分:2)

看起来文本标签颜色在​​ordiplot中是硬编码的,因此您必须设置绘图,然后使用text()按组绘制标签:

score <- scores(pc)[, 1:2]
fig<-ordiplot(score, type="n", main="PCoA")
color <- c("red", "blue")
sz <- as.numeric(df$type[as.numeric(rownames(score))])
text(score, rownames(score), col=color[sz])