2015-10-13 157 views
2

我試圖形象化各種隨機抽樣方法,嚴格在單位方塊[0,1]x[0,1]內生成(x,y)對。我的想法是通過將正方形分成更小的正方形並計算每個正方形內的點來展示均勻性屬性。我將這些計數存儲在不同大小的矩陣中,例如調整geom_tile大小以消除邊距效應

cells1 <- structure(c(0, 1, 0, 0, 1, 0, 2, 3, 1, 1, 0, 1, 0, 0, 2, 0, 2, 
0, 1, 1, 2, 1, 0, 0, 3, 1, 2, 1, 1, 1, 0, 1, 3, 2, 2, 1, 1, 2, 
0, 0, 0, 1, 0, 2, 1, 3, 0, 0, 1, 1, 2, 1, 1, 2, 1, 0, 1, 0, 2, 
0, 0, 0, 3, 1, 1, 2, 0, 0, 1, 2, 0, 1, 0, 0, 1, 3, 0, 3, 3, 1, 
1, 0, 0, 0, 0, 1, 1, 2, 0, 3, 0, 2, 0, 1, 1, 3, 4, 0, 1, 5, 1, 
1, 1, 0, 0, 5, 0, 4, 2, 0, 0, 1, 1, 2, 0, 1, 3, 1, 0, 3, 2, 0, 
1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 2, 1, 3, 1, 0, 0, 
2, 2, 1, 1, 0, 2, 1, 1, 2, 0, 1, 0, 2, 1, 0, 0, 2, 0, 0, 0, 0, 
1, 0, 0, 1, 1, 2, 1, 2, 1, 1, 2, 0, 3, 1, 2, 1, 0, 0, 1, 1, 1, 
1, 2, 1, 0, 1, 1, 0, 1, 1, 0, 0, 2, 1, 1, 1, 1, 1, 4, 1, 0, 0, 
0, 1, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 2, 1, 3, 0, 1, 3, 0, 0, 0, 
1, 3, 1, 0, 1, 1, 1, 2, 1, 2, 2, 1, 1, 2, 0, 0, 1, 2, 0, 1, 2, 
1, 2, 1, 0, 0, 1, 1, 2), .Dim = c(16L, 16L)) 

cells2 <- structure(c(16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 16, 16, 16, 
15, 16, 16), .Dim = c(4L, 4L)) 

當然,我想到了geom_tile和我現在的代碼如下。

plot_step <- function(cells, filename = NULL) { 
    library(ggplot2) 
    library(reshape2) 
    plot_data <- melt(cells, varnames = c("x", "y")) 
    # transform from index to tile position 
    plot_data$x <- (plot_data$x - 1)/max(plot_data$x - 1) 
    plot_data$y <- (plot_data$y - 1)/max(plot_data$y - 1) 
    ggplot(plot_data, aes(x, y, fill = value)) + 
    geom_tile() + 
    geom_text(aes(label = value)) + 
    theme_bw() + 
    scale_fill_gradient(low="white", high="red") + 
    guides(fill = FALSE) 
} 

plot_step(cells1); plot_step(cells2) 

enter image description here

enter image description here

這幾乎是我想除了小問題是什麼。理想情況下,註釋轉換和拼貼大小結合起來應該產生一個完全覆蓋整個單位正方形的圖像,而不會掛在零以下或以上的單位。

我用第二張照片的出色手繪技巧突出顯示了所需的邊距。我感興趣的解決方案可以普遍適用於不同的尺寸(例如,cells1cells2,掛出不同)。這可能是一個輕微的數據轉換或對geom_tile或一些調整,或者兩者兼而有之。

任何幫助,非常感謝!

+0

這似乎解決了保證金的問題:'+ scale_y_continuous(擴大= C(0,0))+ scale_x_continuous(擴大= C(0,0))'。 'expand'的使用和行爲沒有很好的記錄。 – bdemarest

+0

@bdemarest也許我沒有正確解釋,但你可以在我的答案中看到我想要解決的問題。你的想法解決了另一個類似的問題,但我感謝你發佈它。 – tonytonov

回答

1

基於由@Thierry的想法,我能調整軸的方式如下:

plot_step <- function(cells, filename = NULL) { 
    library(ggplot2) 
    library(reshape2) 
    plot_data <- melt(cells, varnames = c("x", "y")) 
    n <- ncol(cells) 
    # here's the adjustment 
    br <- seq(1 - 0.5, n + 0.5, length = 5) 
    lab <- seq(0, 1, length = 5) 
    ggplot(plot_data, 
     aes(x, y, fill = value)) + 
    geom_tile() + 
    geom_text(aes(label = value)) + 
    theme_bw() + 
    scale_fill_gradient(low = "white", high = "red") + 
    guides(fill = FALSE) + 
    scale_x_continuous(breaks = br, labels = lab) + 
    scale_y_continuous(breaks = br, labels = lab) 
} 

這適用於所有片大小(即用於cells1cells2在我的例子)。這裏的樣品圖片:

enter image description here

1

瓷磚始終位於其質心處。解決方案將是爲每個軸計算一組中斷和標籤。

n.x <- length(unique(plot_data$x)) 
x.breaks <- seq(-nx/2, 1 + nx/2, length = 5) 
x.labels <- seq(0, 1, length = 5) 
scale_x_continuous(breaks = x.breaks, labels = x.label) 

您可能還想添加coord_fixed()。它會給你方形瓷磚。

+1

展開技巧不會影響拼貼的位置,它會擴展繪圖區域,所以它看起來沒有關係。我錯過了什麼嗎? – tonytonov

+0

我已經更新了我的答案。 – Thierry

+0

'nx/2'步驟似乎很重要,但我無法獲得完整的圖片。您能否將這個技巧添加到我提供的代碼中,以便我可以重現所需的圖像?我的嘗試導致了一個或多或少的規模破碎。 – tonytonov