R语言作图plot函数以及参数设置解析

Python027

R语言作图plot函数以及参数设置解析,第1张

plot(x, y = NULL, type = "p", xlim = NULL, ylim = NULL, log = "", main = NULL, sub = NULL, xlab = NULL, ylab = NULL, ann = par("ann"), axes = TRUE, frame.plot = axes, panel.first = NULL, panel.last = NULL, asp = NA, xgap.axis = NA, ygap.axis = NA,   ...) x,y  用于横纵坐标作图的数据对象。 type 图类型(线条和点类型),使用格式type=" "。                                                    type="p"显示为空心点。plot                                                    type="l"显示为线条。line                                                    type="b"显示为空心点和线条。both                                                    type="c"显示为无点和线条。                                                    type="o"显示为空心点和线条,线穿过空心点。                                                    type="s"/"S"显示为阶梯线。                                                    type="h"显示为直方图样的垂直线。                                                    type="n"显示为无点和无线条。 xlim/ylim 用于指定图的x轴和y轴的范围,使用格式:xlim=c(x1,x2), ylim=c(y1,y2)。 xlab/ylab 用于给图的x轴和y轴添加标签,使用格式:xlab="xlab",ylab="ylab"。 main 用于对plot添加主标题, main="   "。 sub   用于对plot添加副标题, sub="   "。 log   用于对x或者y值取log。log="x"/"y"/"xy"。 ann  使用T/F对plot中的标题,x轴标签,y轴标签是否显示进行定义。 axes  使用T/F对坐标轴是否显示进行定义。 frame.plot  使用T/F对画图外框是否显示进行定义。 panel.first  参数还没弄懂,panel.first="grid(8,8)"对背景线进行定义。 asp  表示y/x的纵横比。 lty   用于线条类型的定义,指定值为整数,lty="1"。                                                    lty="0"显示为空白,即无线条。                                                    lty="1"显示为实线线条。                                                    lty="2"显示为虚线线条。                                                    lty="3"显示为点状线条。                                                    lty="4"显示为点虚线线条,点和虚线线条间隔。                                                    lty="5"显示为长虚线。                                                    lty="1"显示为双破折号线条。

一个形式为c(x1, x2, n)的向量,表示当par("xlog")=false时,x坐标轴的刻度线的区间及区间中的刻度线个数。如xaxp=c(1,100,10)表示x坐标轴0-100,10个刻度,如果设置的xlim与xaxp不同,以xaxp为准。

请问大家,在运行R语言的时候,出现了以下问题,不知道是源文件的问题,还是程序的问题,求解

源文件如下

There were 50 or more warnings (use warnings() to see the first 50)

#install.packages("survival")

>library(survival)             #引用包

>inputFile="expTime.txt"       #输入文件

>pFilter=0.001                #显著性过滤条件

>setwd("C:\\Users\\ASUS\\Desktop\\tmeimmune\\22.cox")       #设置共工作目录

>#读取输入文件

>rt=read.table(inputFile,header=T,sep="\t",check.names=F,row.names=1)

>rt$futime=rt$futime/365           #以年为单位,除以365

>outTab=data.frame()

>for(gene in colnames(rt[,3:ncol(rt)])){

+     #单因素cox分析

+     cox=coxph(Surv(futime, fustat) ~ rt[,gene], data = rt)

+     coxSummary = summary(cox)

+     coxP=coxSummary$coefficients[,"Pr(>|z|)"]

+     

+     #KM检验,后续需要可视乎,要和生存相关

+     group=ifelse(rt[,gene]<=median(rt[,gene]),"Low","High")

+     diff=survdiff(Surv(futime, fustat) ~ group,data = rt)

+     pValue=1-pchisq(diff$chisq,df=1)

+     

+     #保存满足条件的基因

+     if((pValue<pFilter) &(coxP<pFilter)){

+         outTab=rbind(outTab,

+                      cbind(gene=gene,

+                            KM=pValue,

+                            HR=coxSummary$conf.int[,"exp(coef)"],

+                            HR.95L=coxSummary$conf.int[,"lower .95"],

+                            HR.95H=coxSummary$conf.int[,"upper .95"],

+                            pvalue=coxP) )

+     }

+ }

There were 50 or more warnings (use warnings() to see the first 50)

>#输出基因和P值表格文件

>write.table(outTab,file="cox.result.txt",sep="\t",row.names=F,quote=F)

>#绘制森林图

>#读取输入文件

>rt <- read.table("cox.result.txt",header=T,sep="\t",row.names=1,check.names=F)

Error in read.table("cox.result.txt", header = T, sep = "\t", row.names = 1,  : 

  no lines available in input

>gene <- rownames(rt)

>hr <- sprintf("%.3f",rt$"HR")

>hrLow  <- sprintf("%.3f",rt$"HR.95L")

>hrHigh <- sprintf("%.3f",rt$"HR.95H")

>Hazard.ratio <- paste0(hr,"(",hrLow,"-",hrHigh,")")

>pVal <- ifelse(rt$pvalue<0.001, "<0.001", sprintf("%.3f", rt$pvalue))

>#输出图形

>pdf(file="forest.pdf", width = 7,height = 6)

>n <- nrow(rt)

>nRow <- n+1

>ylim <- c(1,nRow)

>layout(matrix(c(1,2),nc=2),width=c(3,2))

>#绘制森林图左边的基因信息

>xlim = c(0,3)

>par(mar=c(4,2.5,2,1))

>plot(1,xlim=xlim,ylim=ylim,type="n",axes=F,xlab="",ylab="")

>text.cex=0.8

>text(0,n:1,gene,adj=0,cex=text.cex)

>text(1.5-0.5*0.2,n:1,pVal,adj=1,cex=text.cex)text(1.5-0.5*0.2,n+1,'pvalue',cex=text.cex,font=2,adj=1)

Error in text.default(1.5 - 0.5 * 0.2, n:1, pVal, adj = 1, cex = text.cex) : 

  'labels'长度不能设成零

>text(3,n:1,Hazard.ratio,adj=1,cex=text.cex)text(3,n+1,'Hazard ratio',cex=text.cex,font=2,adj=1,)

>#绘制森林图

>par(mar=c(4,1,2,1),mgp=c(2,0.5,0))

>xlim = c(0,max(as.numeric(hrLow),as.numeric(hrHigh)))

Warning message:

In max(as.numeric(hrLow), as.numeric(hrHigh)) :

  no non-missing arguments to maxreturning -Inf

>plot(1,xlim=xlim,ylim=ylim,type="n",axes=F,ylab="",xaxs="i",xlab="Hazard ratio")

Error in plot.window(...) : 'xlim'值不能是无限的

>arrows(as.numeric(hrLow),n:1,as.numeric(hrHigh),n:1,angle=90,code=3,length=0.05,col="darkblue",lwd=2.5)

Error in arrows(as.numeric(hrLow), n:1, as.numeric(hrHigh), n:1, angle = 90,  : 

  不能将零长度的座标同其它长度的座标混合在一起

>abline(v=1,col="black",lty=2,lwd=2)

>boxcolor = ifelse(as.numeric(hr) >1, 'red', 'green')

>points(as.numeric(hr), n:1, pch = 15, col = boxcolor, cex=1.3)

Error in xy.coords(x, y) : 'x' and 'y' lengths differ

>axis(1)

>dev.off()

null device 

          1