如何在R中绘制从-X到X的X轴

时间:2021-09-20 00:28:59

I have a data frame that looks like this:

我有一个如下所示的数据框:

> dataf
      gene_name          log.pval             log.fc
1         TNIP3 0.631094308480128    1.1506542453426
2   NP_808217.1 0.772551410812959 -0.808513121902601
3  Q6ZQS7_HUMAN 0.120675567248996   1.20907596997608
4  Q8TA88_HUMAN  1.44494939509263   2.06552619626619
5         THAP5 0.766534371246673   1.13887517622511
6        BHLHB5 0.596510549476608 -0.890552737707591
7   XP_372337.2 0.495557665215951 -0.896828380212937
8  Q7Z2R7_HUMAN 0.448309045489994  -1.81608512967947
9        IFITM2 0.453896048090579   1.63881366592662
10            -  1.28765417650748  -0.95944121604072

What I want to do is to have a plot with x-axis using log.fc column and y-axis using log.pval column.

我想要做的是使用log.fc列和使用log.pval列的y轴绘制带x轴的图。

But why this command failed?

但为什么这个命令失败了?

plot(dataf$log.fc,dataf$log.pval,xlim=c(-2,2),ylim=c(0,5));

It gave me the following error message:

它给了我以下错误消息:

Error in if (equidist) seq.int(1/(2 * ny), 1 - 1/(2 * ny), by = 1/ny) else (yat[-1L] +  : 
  missing value where TRUE/FALSE needed
Calls: plot -> plot.factor -> spineplot -> spineplot.default
In addition: Warning messages:
1: In spineplot.default(x, y, ...) :
  x axis is on a cumulative probability scale, 'xlim' must be in [0,1]
2: In spineplot.default(x, y, ...) :
  y axis is on a cumulative probability scale, 'ylim' must be in [0,1]
Execution halted

Update: Input data and complete code.

更新:输入数据和完整代码。

Input file mydata.csv:

输入文件mydata.csv:

Name|ID_REF|xN|xK|xD|yN|yK|yD
NP_542387.1|H200009573|116.344280521|157.34028943|165.521765927|129.382514146|146.734212992|111.564081171
TRIM65|H200014442|139.28243473|142.8535596|183.543914333|151.18230485|126.012002369|117.926240275
Q96JT4_HUMAN|H200011360|6.30624488407|6.23249632016|8.84048629421|3.67264474301|1.78051470938|3.00185438056
MTCH1|H200017563|962.796774907|984.466886282|1225.05018099|1006.35251622|959.451292287|1004.59648311
RPL35|H200008624|13462.3246704|16392.4312316|16124.489617|15544.2347278|16130.3829312|12319.5940351

Code:

码:

data<-read.delim("~/Desktop/mydata.csv",sep="|",na.strings="",header=TRUE,blank.lines.skip=TRUE,fill=FALSE)
data<- data[complete.cases(data),]

gname <- as.matrix(data$Name);
idref <- as.matrix(data$ID_REF);
xN <- data$xN;
xK <- data$xK;
xD <- data$xD;
yN <- data$yN;
yK <- data$yK;
yD <- data$yD;


xK_yK <- as.matrix(cbind(xK,yK));
xD_yD <- as.matrix(cbind(xD,yD));
xN_yN <- as.matrix(cbind(xN,yN));


#--------------------------------------------------
# Functions 
#-------------------------------------------------- 
foldchange_geom_avg <- function(r,g) {
     # r and g is vector with value >=1
     # Essentially the same with M value
     # >0 if r is upregulated
     # <0 if r is downregulated
     return (sum(log2(r/g))/length(r));
}


ranked.ttest <- function(gn,v1,v2,test.type="t") {
        # By default t.test assume unequal variance
        # and applies Welsh modification
        # These samples are assumed also to be independent
        # and the distributions are strictly normal

        # Mann-Whitney (Wilcoxon) assume no-normality
        # and with arbitrary sample

        out <- c();

        for (i in 1:nrow(v1)) {

            test <- t.test(v1[i,],v2[i,],paired=FALSE)

            if (test.type == "mw") {
                test <- wilcox.test(v1[i,],v2[i,],alternative="two.sided",paired=FALSE)
            }

            pval <- test$p.value;
            log.pval <- -log10(pval);
            gene_name <- gn[i];
            log.fc <- foldchange_geom_avg(v1[i,],v2[i,]);

            abs.fc <- abs(log.fc)

            if (is.numeric(pval) ){
                out <- rbind(out,cbind(gene_name,log.pval,pval,log.fc));
            }
        }
     #out <- out[with(out,order(pvalue,genename))]
     return (data.frame(out));
}



test <- ranked.ttest(gname,xK_yK,xN_yN)
plot(test$log.fc,test$log.pval,xlim=c(-2,2),ylim=c(0,5));

And this is the session info:

这是会话信息:

> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: i386-apple-darwin9.8.0/i386 (32-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base  

And the plot looks like this: 如何在R中绘制从-X到X的X轴

情节看起来像这样:

1 个解决方案

#1


1  

Can you include your session info please? When I run the code I get something different.

你能包括你的会话信息吗?当我运行代码时,我得到了不同的东西。

dataset<- textConnection("gene_name log.pval log.fc
TNIP3 0.631094308480128 1.1506542453426
NP_808217.1 0.772551410812959 -0.808513121902601
Q6ZQS7_HUMAN 0.120675567248996 1.20907596997608
Q8TA88_HUMAN  1.44494939509263 2.06552619626619
THAP5 0.766534371246673 1.13887517622511
BHLHB5 0.596510549476608 -0.890552737707591
XP_372337.2 0.495557665215951 -0.896828380212937
Q7Z2R7_HUMAN 0.448309045489994 -1.81608512967947
IFITM2 0.453896048090579 1.63881366592662
NA 1.28765417650748 -0.95944121604072")

dataf <- read.table(dataset,header=TRUE) 

plot(dataf$log.fc,dataf$log.pval,xlim=c(-2,2),ylim=c(0,5))

如何在R中绘制从-X到X的X轴

#1


1  

Can you include your session info please? When I run the code I get something different.

你能包括你的会话信息吗?当我运行代码时,我得到了不同的东西。

dataset<- textConnection("gene_name log.pval log.fc
TNIP3 0.631094308480128 1.1506542453426
NP_808217.1 0.772551410812959 -0.808513121902601
Q6ZQS7_HUMAN 0.120675567248996 1.20907596997608
Q8TA88_HUMAN  1.44494939509263 2.06552619626619
THAP5 0.766534371246673 1.13887517622511
BHLHB5 0.596510549476608 -0.890552737707591
XP_372337.2 0.495557665215951 -0.896828380212937
Q7Z2R7_HUMAN 0.448309045489994 -1.81608512967947
IFITM2 0.453896048090579 1.63881366592662
NA 1.28765417650748 -0.95944121604072")

dataf <- read.table(dataset,header=TRUE) 

plot(dataf$log.fc,dataf$log.pval,xlim=c(-2,2),ylim=c(0,5))

如何在R中绘制从-X到X的X轴