matplot R的编号问题

时间:2014-06-11 13:28:24

标签: r

我正在使用

matplot(data, type = c("b"), col =a ,pch=as.character(0:12))

绘制称为数据的矩阵的数据。

问题是参数pch=as.character(0:12)无法正常工作。它不是打印0到12之间的数字,而是打印从0到9的数字,并将其他数字(10,11,12)打印为1。我知道如果我使用函数plot(),我可以使用text()代替points()来解决此问题。但我真的想使用matplot()。有没有办法解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:1)

以下是@Carl Witthoft建议使用text生成多字符标签的示例:

# generate some data
A = matrix(runif(min=0, max=3, n=150), ncol=15, nrow=10)

# add a value each column to have some separation between plotted lines
A = sweep(x=A, MARGIN=2, FUN='+', STATS=1:ncol(A))

# function mattext takes matrix data and names (point labels) and colors for each column 
mattext = function(data, data_names, colors){

    # plot only lines (change type to 'n' to suppress lines and generate empty plot)
    # add `lty=x` with x in 1:6 if you want same lines for all
    matplot(data, pch=NA, type='b', col=colors)

    # add point label for each column
    for (i in 1:ncol(data)){
        text(x=1:nrow(data), y=data[,i], lab=data_names[i], col=colors[i])
    }
}

# run function
mattext(data=A, data_names=1:15, colors=rainbow(15))

# all black
mattext(data=A, data_names=1:15, colors=rep(1,15))