【问题标题】:Plot R matrix columns according to column name根据列名绘制 R 矩阵列
【发布时间】:2017-11-29 14:45:36
【问题描述】:

我想绘制以下矩阵x,因此列数据是根据它们的列名绘制的(即0.10.2、等)在 x 轴上。

> x
               0.1          0.2          0.3          0.4          0.5
 [1,] 5.000000e-01 5.000000e-01 5.000000e-01 5.000000e-01 0.5000000000
 [2,] 2.500000e-02 5.000000e-02 7.500000e-02 1.000000e-01 0.1250000000
 [3,] 2.437500e-03 9.500000e-03 2.081250e-02 3.600000e-02 0.0546875000
 [4,] 2.431559e-04 1.881950e-03 6.113802e-03 1.388160e-02 0.0258483887
 [5,] 2.430967e-05 3.756817e-04 1.822927e-03 5.475560e-03 0.0125901247
 [6,] 2.430908e-06 7.510810e-05 5.458812e-04 2.178231e-03 0.0062158067
 [7,] 2.430902e-07 1.502049e-05 1.636750e-04 8.693947e-04 0.0030885852
 [8,] 2.430902e-08 3.004053e-06 4.909445e-05 3.474555e-04 0.0015395229
 [9,] 2.430902e-09 6.008089e-07 1.472761e-05 1.389339e-04 0.0007685764
[10,] 2.430902e-10 1.201617e-07 4.418219e-06 5.556585e-05 0.0003839928

但是当我使用时

plot(x, pch=20, ylim=c(0, 1))

我得到以下信息:Plot of R matrix

我想要一个绘图,其中x[1, 1](即5.000000e-01)被绘制为 x 轴上 0.1 和 y 轴上 0.5 上的一个点。

【问题讨论】:

    标签: r plot


    【解决方案1】:
    set.seed(123)
    mat<-matrix(rnorm(25),5,5)
    colnames(mat)<-seq(0.1,0.5,length.out=5)
    plot(x=matrix(rep(as.numeric(colnames(mat)),5), 5,5,byrow=T),y=mat)
    

    这里第一个参数 x 将 x 轴上的数字重复 5,所以 5 x 5 我将得到一个矩阵,它将为每个 y 列提供正确的 x 位置。

    matplot(x=matrix(rep(as.numeric(colnames(mat)),5), 5,5,byrow=T),y=mat)
    

    也可以使用

    【讨论】:

    • 这是错误:Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ
    • 如果您查看结果(从中获得的图表),您会知道您的答案不起作用。还可以使用set.seed() 使您的数据可重现。
    猜你喜欢
    • 2021-01-25
    • 1970-01-01
    • 2014-11-20
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 2019-09-19
    相关资源
    最近更新 更多