2016-08-05 6 views
0

私はデータを持っていますが、カラムのtidyrのspread()を使って生成したピボットテーブル(data_wide2)別のスクリプト)。R-ggplotとcorrplotは非常に異なるプロットを作成します(相関マップ/マトリックス)

そして私が生成するため、次のコードを使用する(など、83,35,86)乱雑であるインデックスを占め、他の変数(「開始」)に記載

私のデータのレベルを頼っ相関行列はcorrplot(これはうまくいっていて、ラベル以外の数字に合っていた)。この最初のプロットは、2つの明らかに暗い正方形を示しています。

heatmap <- function(){ 
    data_wide2 <- data_wide 
    data_wide2$Protein <- reorder(data_wide2$Protein,data_wide2$Start) 
    data_wide2 <- data_wide2[order(data_wide2$Start),] 
    pre_matrix <- data_wide2[,8:ncol(data_wide2)] 
    exp_data <- as.matrix(pre_matrix) 

    square <- cor(t(exp_data)) 
    square <- (corrplot(square, method = "circle")) 
    square 
    return(corrplot(square, method = "circle")) #plot matrix 
    } 
    heatmap() 

enter image description here

私の最後の数字はplot_gridの一部でなければならないので、それはggplotオブジェクトでなければなりませんでした。したがって、私は相関マップ/マトリックスに関するチュートリアルを検索しました。私がデータセットに1つを適用したとき、生成されたプロットは以前のものとは何の関係もありませんでした。私の直感は、データが算数順に再編成されたことですが、それを修正する方法はわかりません。

enter image description here

heatmap <- function(){ 
    data_wide2 <- data_wide 
    data_wide2$Protein <- reorder(data_wide2$Protein,data_wide2$Start) 
    data_wide2 <- data_wide2[order(data_wide2$Start),] 
    pre_matrix <- data_wide2[,8:ncol(data_wide2)] 
    exp_data <- as.matrix(pre_matrix) 

    square <- melt(cor(t(exp_data))) 

    plot <- ggplot(square,aes(Var1,Var2,fill=value)) + 
     geom_tile() + 
     scale_fill_gradient2(limits=c(-1,1),midpoint=0,low='000000',high='steelblue',space='Lab') + 
     #scale_x_reverse(lim=c(97,0)) 
     scale_y_reverse(lim=c(97,0)) + 
     theme(axis.text.x = element_text(angle = 45, vjust = 1, size = 12, hjust = 1)) 

     coord_flip() 
    plot 
    print(class(plot)) 
    return(plot) 
    } 
heatmap() 

は私が文字に、要因にVAR1を変換しようとしたが、どちらも動作します。

Error in eval(expr, envir, enclos) : object 'x' not found 
In addition: Warning messages: 
1: In min(x, na.rm = na.rm) : 
    no non-missing arguments to min; returning Inf 
2: In max(x, na.rm = na.rm) : 
    no non-missing arguments to max; returning -Inf 
3: In min(diff(sort(x))) : no non-missing arguments to min; returning Inf 

答えて

0

Iは、(I)は1:97(の元のインデックスを置き換える)これらの機能を追加することによって解決:

idx <- as.vector(square$Var1[1:97]) 
    square$Var1 <- sapply(square$Var1,function(x){return(which(idx == x))}) 
    square$Var2 <- sapply(square$Var2,function(x){return(which(idx == x))}) 

今は(それにもかかわらず、無関係であった)行の元のインデックスを失っ。

私はこの質問を開いたままにして、誰かがより良い解決策を提供できるようにします。

関連する問題