2017-09-08 93 views
0

我希望看到我的數據密度,所以我使用cut函數預處理的數據繪製了等高線圖,這是我的數據的一個小樣本:contour2D plot的x和y軸值不正確

> z[1:2,] 
      pc2_cut 
pc1_cut   (-1.61,-1.45] (-1.45,-1.3] (-1.3,-1.15] (-1.15,-1] (-1,-0.851] 
(-1.58,-1.38]    0   1   1   0   0 
(-1.38,-1.18]    5   1   4   1   0 

我用plot3D圖書館,

> contour2D(z,border="black",xlab="PC1",ylab="PC2") 

這是我得到:

enter image description here

您可以看到x軸和y軸值不正確,甚至不在間隔的中點附近。有誰知道如何解決這個問題?

回答

0

contour2D()函數默認情況下將軸縮放到0和1之間。要獲得不同的軸,您可以省略原始contour2D()呼叫的軸,並使用axis()atlabels指定的值來添加軸。如果您使用cut()中的factors,則必須在繪圖之前將它們轉換爲數字值。我在下面提供了一個示例,其中生成數據,繪製它,然後在將因子轉換爲數值後調整軸標籤。如果沒有確切的數據格式,我不知道從數據中提取刻度標記標籤的最佳方法。

enter image description here

# library for plot 
library(plot3D) 

# setting seed and generating some data 
set.seed(10) 

#### storing data in matrix #### 
datamatrix <- matrix(c(rnorm(500,-1,.4),rnorm(500,2,0.2),runif(500,-3,0),runif(500,0,3)),nrow=1000,ncol=2,byrow=F) 

# locations of cuts 
xcuts <- seq(min(datamatrix[,1]),max(datamatrix[,1]),length.out = 6) 
ycuts <- seq(min(datamatrix[,2]),max(datamatrix[,2]),length.out = 6) 

# calculating values for cutting 
xvals <- cut(datamatrix[,1], xcuts) 
yvals <- cut(datamatrix[,2], ycuts) 

# initializing matrix to store count in each bin 
z <- matrix(0,length(levels(yvals)),length(levels(xvals))) 

for(i in 1:length(levels(xvals))){ 
    for(j in 1:length(levels(yvals))){ 
    z[j,i] <- length(intersect(which(xvals == levels(xvals)[i]),which(yvals == levels(yvals)[j]))) 
    } 
} 

#### finding labels from factors cut #### 
factsx <- levels(xvals) # factsx <- levels_pc2_cut # or something like that 
xlabsFacts <- rep(NA,length(factsx)) 

for(i in 1:(length(factsx))){ 

    comma_sep <- unlist(gregexpr(pattern =',',factsx[i])) # location of the comma in the factor 

    #taking section of text and converting to numbers 
    xlabsFacts[i] <- as.numeric(substr(factsx[i],2,comma_sep-1)) 
    xlabsFacts[i+1] <- as.numeric(substr(factsx[i],comma_sep+1,nchar(factsx[i])-1)) 

} 

factsy <- levels(yvals) # factsy <- levels_pc1_cut # or something like that 
ylabsFacts <- rep(NA,length(factsy)) 

for(i in 1:(length(factsy))){ 

    comma_sep <- unlist(gregexpr(pattern =',',factsy[i])) # location of the comma in the factor 

    #taking section of text and converting to numbers 
    ylabsFacts[i] <- as.numeric(substr(factsy[i],2,comma_sep-1)) 
    ylabsFacts[i+1] <- as.numeric(substr(factsy[i],comma_sep+1,nchar(factsy[i])-1)) 

} 


#### formatting plot #### 
# contour plot without axes 
contour2D(z 
      ,yaxt='n' # no y axis ticks 
      ,xaxt='n' # no x axis ticks 
      ,ylab='y values' # y axis label 
      ,xlab='x values' # x axis label 
) 

# adding x axis with tick marks 
axis(side=1 # bottom 
    ,at=seq(0,1,length.out = length(xlabsFacts)) # change 6 to number of tick marks you want 
    ,labels=round(xlabsFacts,2) # change to labels for tick marks from your data 
) 

# adding x axis with tick marks 
axis(side=2 # bottom 
    ,at=seq(0,1,length.out = length(ylabsFacts)) # change 6 to number of tick marks you want 
    ,labels=round(ylabsFacts,2) # change to labels for tick marks from your data 

) 
+0

我設法省去從contour2D軸但是當我由>軸(添加x軸側= 1,在= SEQ(0,1,length.out = 9),標籤= round(pc2_cut,2)),我得到這個錯誤:Math.factor中出錯(c(10L,9L,10L,10L,10L,10L,10L,10L,11L,11L,'round')對於因素沒有意義。你知道爲什麼嗎?謝謝! – HYY

+0

@HYY,檢查'class(pc_cut)'。如果它是一個''字符'',那麼'R'就把它當作一個字符串來處理。 (')'的位置,然後你可以在'labels'參數中使用這些參數,如果你不想這樣做,你可能不得不使用'substr()'來獲取數字部分,並且那麼'as.nu meric()'使它成爲一個可以四捨五入的值。如果不想舍入,可以使用'substr()',因爲標籤可以是字符串。 –

+0

我查過了,這是因素。 – HYY