在R中为绘图添加颜色

时间:2013-01-13 13:54:58

标签: r graphics colors plot

我正在R使用包smacof

我有一个graph我希望在红色中看到1到10点,其他的则留下黑色。

我使用以下命令制作:

nations_indscal2<-smacofIndDiff(nations_list, ndim=2, metric=FALSE, 
                               constraint="indscal",itmax=10000)

nations_indscal2$gspace
plot(nations_indscal2)

nations_indscal2$cweights
weightspace(nations_indscal2,20,1,2)

我试过(第1点):

text(nations_indscal2$cweights[[1]], col="red")
weightspace(nations_indscal2,20,1,2)

我没有错误,但它也不起作用。我错的任何想法?

请在此处找到nations_list的可重现示例:

list(structure(c(6, 1, 6, 6, 5, 8, 6, 7, 5, 6, 5, 5, 7, 8, 8, 
8, 8, 7, 6, 7, 8, 7, 3, 5, 8, 6, 5, 8, 8, 7, 8, 2, 5, 6, 1, 8, 
5, 3, 7, 4, 7, 1, 2, 3, 8), Labels = c("Brazil", "Congo", "Cuba", 
"Egypt", "France", "India", "Israel", "Japan", "China", "USA"
), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
    upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(6, 1, 6, 6, 5, 8, 6, 7, 5, 6, 5, 5, 7, 8, 8, 
    8, 8, 7, 6, 7, 8, 7, 3, 5, 8, 6, 5, 8, 8, 7, 8, 2, 5, 6, 
    1, 8, 5, 3, 7, 4, 7, 1, 2, 3, 8), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(6, 5, 5, 5, 5, 6, 5, 5, 5, 6, 5, 6, 5, 6, 6, 
    6, 6, 6, 5, 7, 6, 6, 5, 5, 6, 4, 4, 6, 6, 6, 6, 5, 6, 6, 
    2, 6, 5, 4, 5, 4, 6, 3, 3, 3, 6), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(7, 5, 6, 7, 7, 7, 6, 7, 6, 7, 4, 5, 7, 7, 7, 
    7, 7, 7, 7, 8, 7, 7, 7, 7, 5, 7, 7, 7, 8, 5, 7, 6, 8, 7, 
    5, 7, 7, 4, 7, 6, 7, 4, 4, 5, 6), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(7, 5, 7, 5, 3, 7, 4, 3, 4, 7, 6, 6, 7, 7, 7, 
    7, 8, 7, 6, 6, 7, 6, 4, 5, 7, 7, 7, 7, 8, 7, 7, 6, 5, 6, 
    1, 6, 4, 2, 5, 7, 6, 7, 1, 5, 5), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(7, 5, 7, 7, 7, 7, 7, 7, 6, 7, 6, 6, 7, 7, 7, 
    7, 8, 7, 7, 7, 7, 7, 7, 5, 6, 5, 6, 7, 6, 7, 7, 7, 6, 7, 
    5, 6, 7, 6, 7, 7, 7, 6, 4, 7, 7), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(5, 3, 4, 7, 2, 3, 6, 3, 6, 4, 6, 7, 5, 4, 6, 
    6, 7, 1, 5, 2, 4, 6, 4, 3, 5, 4, 2, 5, 5, 6, 6, 5, 4, 3, 
    1, 5, 4, 4, 5, 5, 6, 6, 3, 2, 2), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(6, 5, 6, 5, 4, 7, 7, 6, 6, 5, 5, 6, 6, 8, 8, 
    7, 7, 7, 7, 7, 8, 8, 6, 7, 5, 6, 5, 8, 7, 7, 7, 5, 6, 7, 
    4, 8, 6, 6, 6, 7, 7, 3, 4, 5, 6), Labels = c("1", "2", "3", 
    "4", "5", "6", "7", "8", "9", "10"), Size = 10L, call = as.dist.default(m = data, 
        diag = TRUE, upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(5, 3, 6, 7, 6, 6, 7, 6, 6, 6, 5, 7, 6, 7, 6, 
    7, 7, 7, 7, 6, 6, 7, 6, 6, 7, 6, 5, 7, 7, 8, 7, 7, 6, 7, 
    3, 5, 5, 5, 7, 6, 6, 7, 1, 6, 6), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(4, 2, 2, 6, 6, 3, 8, 7, 6, 1, 2, 8, 4, 2, 8, 
    7, 7, 7, 7, 7, 7, 8, 4, 8, 7, 1, 2, 8, 2, 8, 8, 8, 8, 8, 
    2, 7, 7, 1, 8, 8, 7, 8, 2, 3, 8), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(3, 3, 4, 5, 3, 5, 6, 6, 6, 4, 2, 4, 5, 5, 5, 
    2, 7, 3, 4, 3, 5, 2, 3, 7, 6, 3, 4, 7, 5, 2, 5, 6, 8, 3, 
    7, 7, 7, 7, 7, 7, 5, 8, 2, 7, 1), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(7, 6, 7, 7, 7, 8, 8, 8, 6, 7, 6, 6, 8, 8, 8, 
    8, 8, 8, 8, 7, 8, 8, 5, 8, 8, 8, 6, 8, 8, 8, 8, 4, 5, 8, 
    2, 8, 7, 7, 8, 5, 8, 2, 7, 3, 8), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(5, 3, 4, 4, 4, 6, 3, 4, 4, 4, 5, 4, 5, 5, 4, 
    3, 4, 2, 4, 4, 4, 5, 2, 7, 4, 4, 6, 4, 2, 5, 4, 4, 4, 3, 
    3, 3, 4, 3, 2, 5, 4, 1, 4, 1, 5), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(6, 6, 7, 5, 7, 7, 6, 7, 6, 6, 6, 3, 6, 7, 7, 
    6, 7, 2, 6, 4, 6, 7, 2, 7, 4, 4, 5, 4, 2, 4, 6, 4, 2, 7, 
    3, 6, 7, 3, 7, 3, 6, 2, 6, 2, 6), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(6, 6, 7, 8, 6, 6, 7, 7, 8, 7, 6, 8, 6, 6, 7, 
    7, 8, 7, 8, 7, 8, 4, 3, 8, 7, 6, 5, 7, 7, 7, 8, 7, 7, 8, 
    2, 6, 7, 6, 8, 7, 7, 8, 1, 8, 8), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(8, 7, 7, 7, 8, 7, 7, 8, 7, 7, 7, 6, 7, 8, 8, 
    8, 8, 7, 8, 7, 7, 6, 6, 8, 7, 7, 6, 7, 7, 7, 7, 6, 7, 8, 
    6, 6, 7, 7, 7, 8, 8, 6, 5, 8, 8), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(6, 4, 6, 4, 5, 5, 6, 6, 7, 4, 5, 7, 6, 7, 5, 
    5, 8, 5, 7, 7, 4, 5, 2, 8, 5, 5, 4, 5, 5, 7, 7, 6, 3, 7, 
    2, 3, 4, 6, 6, 6, 4, 7, 3, 6, 8), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(5, 2, 6, 6, 7, 6, 7, 7, 5, 5, 4, 7, 5, 6, 7, 
    7, 7, 7, 7, 6, 6, 6, 7, 7, 6, 7, 3, 6, 5, 8, 7, 7, 7, 6, 
    3, 4, 4, 3, 7, 5, 7, 7, 0, 6, 7), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(7, 5, 5, 6, 6, 6, 6, 5, 6, 5, 5, 7, 7, 7, 6, 
    5, 7, 4, 8, 6, 8, 6, 4, 8, 5, 5, 6, 6, 5, 7, 4, 2, 4, 7, 
    1, 5, 4, 4, 6, 5, 6, 0, 2, 5, 8), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE), 
    structure(c(4, 1, 2, 7, 2, 3, 1, 1, 8, 2, 1, 8, 2, 2, 2, 
    1, 7, 1, 7, 1, 1, 0, 0, 7, 7, 2, 2, 2, 0, 8, 8, 8, 8, 8, 
    2, 1, 1, 0, 6, 2, 0, 7, 1, 8, 8), Labels = c("Brazil", "Congo", 
    "Cuba", "Egypt", "France", "India", "Israel", "Japan", "China", 
    "USA"), Size = 10L, call = as.dist.default(m = data, diag = TRUE, 
        upper = FALSE), class = "dist", Diag = TRUE, Upper = FALSE))

1 个答案:

答案 0 :(得分:2)

我重写了你的权重空间函数:

nindividuals <- 15
weights <- sapply(nations_indscal2$cweights[1:nindividuals],diag)
plot(x=weights[1,],y = weights[2,],type ='n')
text(x=weights[1,],y = weights[2,],
     labels = seq(1,nindividuals),cex=1,col = c(rep('red',10),rep(1,6)))

enter image description here