2017-05-05 10 views
1

私は、位置データとその位置データの値を取り込んで、それをgeom_tileでプロットするコードを持っています。プロットの行列サイズは、データ項目からデータ項目まで一定ではなく、幾何学的タイルの各「セル」は、不一致なサイズの追加の行列を含む可能性があります。私が現在行っているコードは、追加の詳細セルが2x2ですが、他のサイズに適応しようとすると失敗してしまいます。たとえば、5x5です。このコードでは、追加の詳細セルのサイズだけでなく、主要なxとyの距離(x = c(0,2,4,6,8)の主な距離は2など)をユーザが入力する必要があります。以下のイメージは、2x2の追加ディテール・セルで成功したgeom_tileです。
geom_tile with 2x2 additional detail可変サイズのgeom_tileプロット

これを生成したコードは次のとおりです。

x <- c(0,0,4,3,3,5,5) 
    y <- c(0,4,0,3,5,3,5) 


    #USER INPUT 
    major_x_dist <- 4 #x distance between the major data points 
    major_y_dist <- 4 #x distance between the major data points 
    division_cells <- as.character("2x2") #size of the cell containing additional detail 



    ####################################### 
    if (division_cells == "2x2") { 
    div_cells <- 2 
    } else if (division_cells == "3x3") 
    { div_cells <- 3 
    } else if (division_cells == "4x4") 
    { div_cells <- 4 
    } else if (division_cells == "5x5") 
    { div_cells <- 5 
    } else 
    { div_cells <-1 
    } 
    data_width <- ifelse(x%% major_x_dist==0, major_x_dist, major_x_dist/div_cells) 
    data_height <- ifelse(y%% major_y_dist==0, major_y_dist, major_y_dist/div_cells) 


    data_val <- sample(0:100, 7) 
    alldata <-data.frame(x, y, data_width, data_height, data_val) 




    ggplot(data= alldata, aes(x=x, y=y, width=data_width, height=data_height)) + 
    geom_tile(fill = "white", color="black") + 
    geom_text(aes(label = data_val), colour="black") + 
    coord_fixed() 

5x5の追加セルに対する試行された適応は下記の通りです。全体的なマトリックスのサイズは、データポイント間の主要な距離は、追加の詳細のセルの位置、及び追加の詳細セルのサイズは2×2の追加の詳細細胞と連携した溶液から全て異なること

x <- c(0,0,0,2,2,2,4,4,4,-0.8,-0.8,-0.8,-0.8,-0.8,-0.4,-0.4,-0.4,-0.4,-0.4,0,0,0,0,0.4,0.4,0.4,0.4,0.4,0.8,0.8,0.8,0.8,0.8) 
    y <- c(0,2,4,0,2,4,0,2,4,3.2,3.6,4,4.4,4.8,3.2,3.6,4,4.4,4.8,3.2,3.6,4.4,4.8,3.2,3.6,4,4.4,4.8,3.2,3.6,4,4.4,4.8) 


    #USER INPUT 
    major_x_dist <- 2 #x distance between the major data points 
    major_y_dist <- 2 #x distance between the major data points 
    division_cells <- as.character("5x5") #size of the cell containing additional detail 



    ####################################### 
    if (division_cells == "2x2") { 
    div_cells <- 2 
    } else if (division_cells == "3x3") 
    { div_cells <- 3 
    } else if (division_cells == "4x4") 
    { div_cells <- 4 
    } else if (division_cells == "5x5") 
    { div_cells <- 5 
    } else 
    { div_cells <-1 
    } 
    data_width <- ifelse(x%% major_x_dist==0, major_x_dist, major_x_dist/div_cells) 
    data_height <- ifelse(y%% major_y_dist==0, major_y_dist, major_y_dist/div_cells) 


    data_val <- sample(0:100, 33) 
    alldata <-data.frame(x, y, data_width, data_height, data_val) 




    ggplot(data= alldata, aes(x=x, y=y, width=data_width, height=data_height)) + 
    geom_tile(fill = "white", color="black") + 
    geom_text(aes(label = data_val), colour="black") + 
    coord_fixed() 

注。テキストが正しい場所にあるように見えますが、セルは正しくありません。私は、この問題は、追加詳細セルの中心データポイントが大きなポイント(0,4)にあるという事実と関係していると思います。このコードが生成するプロットは以下の通りです。

enter image description here 提供できるトラブルシューティングのアドバイスは非常に高く評価されています。

答えて

1

小さな四角形を識別する方法はないと思います。理由は分かりませんが、私はこれを最初に戻すほうが簡単かもしれないと考えました。ここには一般化された解決策があります。まず、私はいくつかのデータを設定します - 寸法で、正方形の数とサブグリッドの場所すべてがランダムに選択...

large_x <- sample(2:5,1) #large grid no of x squares 
large_y <- sample(2:5,1) #large grid no of y squares 
small_x <- sample(2:5,1) #small grid no of x squares 
small_y <- sample(2:5,1) #small grid no of y squares 
large_w <- round(runif(1,0.5,1.5),2) #width of large squares 
large_h <- round(runif(1,0.5,1.5),2) #height of large squares 
df <- expand.grid(x=large_w*(1:large_x),y=large_h*(1:large_y)) #large grid 
divsq <- sample(nrow(df),1) #random row of df to determine square to divide 
sm_x <- df$x[divsq] #coordinates of divided square 
sm_y <- df$y[divsq] 
df <- rbind(df[-divsq,], #large grid without subdivided square 
      expand.grid(x=sm_x-large_w*((1+1/small_x)/2-(1:small_x)/small_x), #x coordinates of small grid 
         y=sm_y-large_h*((1+1/small_y)/2-(1:small_y)/small_y))) #y coordinates of small grid 
df$val <- sample(0:100,nrow(df)) 
df <- df[sample(nrow(df)),] #shuffle df for good measure! 

は今、私はすべてのランダムなパラメータを無視して、ただで作業するつもりですdfは、x,yおよびvalの列のみを含んでいます。アプローチは、定数yのx値の間の間隔を調べ(逆もまた同様)、小さい方の特性を計算するために使用します。この情報は、各データポイントが小さな四角に属しているかどうかをマークするために使用され、その後残りは単純なものになります。

xdists <- tapply(df$x,df$y,function(z) diff(sort(z))) #list of differences between x values for constant y 
ydists <- tapply(df$y,df$x,function(z) diff(sort(z))) #list of differences between y values for constant x 
smallw <- min(unique(unlist(xdists))) #identify small width 
smallh <- min(unique(unlist(ydists))) #identify small height 

#the next lines check for rows that contain diffs equal to the small values, and return the appropriate values of x or y 
smally <- as.numeric(names(xdists)[sapply(xdists,function(z) min(abs(z-smallw))<0.0000001)]) #values of y corresponding to small grid 
smallx <- as.numeric(names(ydists)[sapply(ydists,function(z) min(abs(z-smallh))<0.0000001)]) #values of x corresponding to small grid 

nx <- length(smallx) #x-size of small grid 
ny <- length(smally) #y-size of small grid 

#this checks which data points are in small squares (allowing some tolerance for rounding) 
df$small <- mapply(function(x,y) (min(abs(x-smallx))<0.0000001 & 
            min(abs(y-smally))<0.0000001),df$x,df$y) 

df$w <- ifelse(df$small,smallw,smallw*nx) 
df$h <- ifelse(df$small,smallh,smallh*ny) 

ggplot(data=df, aes(x=x, y=y, width=w, height=h)) + 
    geom_tile(fill = "white", color="black") + 
    geom_text(aes(label = val), colour="black") + 
    coord_fixed() 

enter image description here

+0

このソリューションは完璧ですが、この問題に関するあなたの助けのすべてのためにあなたをとても感謝し、あなたは信じられないほどの助けを借りてきました! – User247365

+0

喜び - 面白い質問のおかげで! –

+1

今後の文書化のためにこの回答に追加します。この回答は、私が提供した範囲でシームレスに動作します。データが1x1の場合、 'w'および' h'値の 'NaN'の生成のためにわずかに分解されます。これは、 'w'と' h'が定義された後、ggplot行の前に 'df [is.na(df)] < - 1'という行を追加することで修正されますが、これはNaN = 1 'w'と' h'変数だけではありません。私は現在、グリッドの代わりにXまたは+を形成する状況にソリューションを適応させています。また、マトリックスあたり1つのグリッドに複数のグリッドがある場合もあります。 – User247365

関連する問題