R:textplot()如何改变点的颜色?

时间:2015-05-10 17:04:17

标签: r

require(wordcloud)
textplot(loc[,1],loc[,2],states)

给我这个

http://blog.fellstat.com/wp-content/uploads/2012/09/blog_text2.png

我想改变红点的颜色。

    textplot(loc[,1],loc[,2],states, col="blue")

这会改变文本的颜色。

texplot函数中的Somwehre是点的代码:

    > textplot
    function (x, y, words, cex = 1, new = TRUE, show.lines = TRUE, 
....
                    points(x[i], y[i], pch = 16, col = "red", cex = 0.5)
....
 <environment: namespace:wordcloud>

但这是否意味着它固定为红色,pch 16和cex 0.5?我将par改为par(col =&#34; blue&#34;),它仍然显示红点。

1 个答案:

答案 0 :(得分:2)

遗憾的是,硬编码到函数中。您只需在脚本中的某处修改函数即可实现此功能。

library(wordcloud)
dat <- sweep(USArrests, 2, colMeans(USArrests))
dat <- sweep(dat, 2, sqrt(diag(var(dat))),"/")
loc <- cmdscale(dist(dat))


    textplot2 <- function(x, 
                      y, 
                      words, 
                      cex = 1, 
                      pch = 16, 
                      pointcolor = "red", 
                      new = TRUE,
                      show.lines=TRUE, 
                      ...){
  if(new)
    plot(x,y,type="n",...)
  lay <- wordlayout(x,y,words,cex,...)
  if(show.lines){
    for(i in 1:length(x)){
      xl <- lay[i,1]
      yl <- lay[i,2]
      w <- lay[i,3]
      h <- lay[i,4]
      if(x[i]<xl || x[i]>xl+w ||
         y[i]<yl || y[i]>yl+h){
        points(x[i],y[i],pch= pch,col= pointcolor,cex= .5)
        nx <- xl+.5*w
        ny <- yl+.5*h
        lines(c(x[i],nx),c(y[i],ny),col="grey")
      }
    }
  }
  text(lay[,1]+.5*lay[,3],lay[,2]+.5*lay[,4],words,cex = cex,...)
}

然后调用新的textplot2

textplot2(loc[,1],loc[,2],rownames(loc),pch = 16, pointcolor = "blue")