【问题标题】:R: textplot() how change the colour of points?R: textplot() 如何改变点的颜色?
【发布时间】:2015-07-21 03:51:00
【问题描述】:
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="blue") 仍然显示红点。

【问题讨论】:

    标签: r


    【解决方案1】:

    不幸的是,它被硬编码到函数中。您可以在脚本中的某处修改函数来实现它。

    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")
    

    【讨论】:

    • 非常感谢!这回答了我的问题。
    猜你喜欢
    • 2022-01-13
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2013-11-16
    • 1970-01-01
    相关资源
    最近更新 更多