美文网首页
R 散点图,plot(),spearman相关性系数 2018-

R 散点图,plot(),spearman相关性系数 2018-

作者: 11的雾 | 来源:发表于2018-11-05 18:16 被阅读0次

R 散点图,ggplot2,spearman相关性系数

散点图显示了在笛卡尔平面绘制的多个点。每个点代表两个变量的值。在水平轴上选择一个变量,在垂直轴中选择另一个变量。简单散点图使用plot()函数来创建。

在R中创建散点图的基本语法是 -

plot(x, y, main, xlab, ylab, xlim, ylim, axes)

以下是使用的参数的描述 :

  • x - 是数据集,其值是水平坐标。
  • y - 是数据集,其值是垂直坐标。
  • main - 是图表的标题。
  • xlab - 是水平轴(x轴)上的标签。
  • ylab - 是垂直轴(y轴)上的标签。
  • xlim - 是用于绘制的x的值的极限。
  • ylim - 是用于绘制的y值的极限。
  • axes - 指示是否应在绘图上绘制两个轴。

例子:
读取数据:
数据的读取可以用:

  • read.table() 表格式文件
  • read.csv() 逗号分隔的文件
  • read.delim() tab键分隔的文件
    使用外部文件参数读取数据:
args=commandArgs(T)
data = read.table(args[1],sep="\t",header=TRUE)

将第一列赋值给x,将第二列赋值给y

x = data[,1]
y = data[,2]
对x和y取对数lg:
x = log(data[,1],10)
y = log(data[,2],10)
计算相关性系数:
cor.test(x,y,method="spearman")
创建一个png图,
png(file = "scatterplot.png")
plot(x,y,main="haha",xlab="reversA", ylab="reversB",pch=16) # 不懂pch是什么?往下看!

屏幕打印出如下:

    Spearman's rank correlation rho
data:  x and y
S = 89876000, p-value < 2.2e-16
alternative hypothesis: true rho is not equal to 0
sample estimates:
      rho
0.9931938

Warning message:
In cor.test.default(x, y, method = "spearman") :
  Cannot compute exact p-value with ties

我们可以看到spearman系数为0.9931938。

plot()及相关函数的参数说明:

image.png image.png image.png image.png

ggplot无法安装?戳这里https://www.jianshu.com/p/edb234eed915

相关文章

网友评论

      本文标题:R 散点图,plot(),spearman相关性系数 2018-

      本文链接:https://www.haomeiwen.com/subject/dsxexqtx.html