2013-08-24 81 views
1

我的完整數據(結果dput())在問題的末尾。我正在嘗試製作一個與ggplot()的瓷磚情節,並且有不均勻間隔的xy度量,因此瓷磚不會填滿整個區域。這裏有一個例子:ddply具有不同的輸出,取決於用於應用功能的變量的排序/順序

library(ggplot2) 
ggplot(data, aes(x = x, y = -y, z = d)) + geom_tile(aes(fill = d)) 

unevenly space tiles

我不知道肯定,但我認爲ggplot可能默認爲類似unique(data$x)[2] - unique(data$x)[1]平鋪尺寸,我的數據,因此排在那裏,這是確實,連續的xy之間的距離觸摸,但沒有休息。我想我會使用plyrddply()爲我的數據創建一個heightwidth列,但我遇到了奇怪的結果。

對於那些誰不會加載完整的數據,這裏的結構:

head(data, 5) 

    x y  d 
1 2.0 0 0.28125 
2 5.5 0 0.81250 
3 11.5 0 0.56250 
4 17.5 0 0.46875 
5 23.5 0 0.40625 

tail(data, 5) 

     x y  d 
191 47.5 80.5 0.000 
192 53.5 80.5 0.125 
193 59.5 80.5 0.000 
194 65.5 80.5 0.000 
195 71.0 80.5 0.000 

所以,我通過爲每個y獨特價值的x每個值循環。以下是我嘗試設置高度/寬度列:

# for each unique value of y, calculate diff for the x's and then add on 1 
data$width <- ddply(data, .(y), summarize, width = c(diff(x), 1))$width 

# for each unique value of x, calculate diff for the y's and then add on 1 
data$height <- ddply(data, .(x), summarize, height = c(diff(y), 1))$height 

我只是把一個1末以來的diff()長度爲n值是n-1,我想我會用正確的價值發揮到串聯後來。下面是我得到,但:

ggplot(data, aes(x = x, y = -y, z = d)) + 
    geom_tile(aes(fill = d, height = height, width = width)) 

wrong heights

寬度是正確的,而不是高度。經調查:

head(data, 5) 

     x y  d height width 
1 2.0 0 0.28125 5.5 3.5 
2 5.5 0 0.81250 6.5 6.0 
3 11.5 0 0.56250 6.0 6.0 
4 17.5 0 0.46875 6.0 6.0 
5 23.5 0 0.40625 6.0 6.0 

所以,我們可以看到,寬度是正確的:2 - > 5.5 = 3.5,5.5 - > 11.5 = 6,等等。

但高度不是,我們可以看到,如果我們只是看常x值的輸出:

head(data[data$x == 2, ], 5) 

    x y  d height width 
1 2 0.0 0.28125 5.5 3.5 
14 2 5.5 0.37500 4.5 3.5 
27 2 12.0 0.37500 4.5 3.5 
40 2 18.0 0.56250 6.0 3.5 
53 2 24.0 0.25000 6.0 3.5 

首先應該是5.5(正確的),但第二個應該是6.5,那麼6 , 等等。

如果我手動子集劃分自己跑我ddply功能,它似乎工作:

c(diff(data[data$x == 2, "y"]), 1) 
[1] 5.5 6.5 6.0 6.0 6.0 6.0 6.0 6.0 6.0 6.0 6.0 4.5 5.5 4.5 1.0 

在重新審查height值,它們似乎是相同,但重新安排 。繼這一意見,我重新整理我的數據,就好像我已經收集了每一個獨特的x數據同時舉行,而不是周圍的其他方法y不變,然後重新定義我heightwidth列:

data_sort <- data[order(data$y, data$x), c("x", "y", "d")] 
data_sort$width <- ddply(data_sort, .(y), summarize, width = c(diff(x), 1))$width 
data_sort$height <- ddply(data_sort, .(x), summarize, height = c(diff(y), 1))$height 

高地現在是正確的,但寬度混亂:

head(data_sort, 5) 
    x y  d width height 
1 2 0.0 0.28125 3.5 5.5 
14 2 5.5 0.37500 6.0 6.5 
27 2 12.0 0.37500 6.0 6.0 
40 2 18.0 0.56250 6.0 6.0 
53 2 24.0 0.25000 6.0 6.0 
66 2 30.0 0.31250 6.0 6.0 

我缺少的是ddply了獨一無二的,但不連續的水平/值搜索時不會再讓東西整理?


數據:

dput(data) 
structure(list(x = c(2, 5.5, 11.5, 17.5, 23.5, 29.5, 35.5, 41.5, 
47.5, 53.5, 59.5, 65.5, 71, 2, 5.5, 11.5, 17.5, 23.5, 29.5, 35.5, 
41.5, 47.5, 53.5, 59.5, 65.5, 71, 2, 5.5, 11.5, 17.5, 23.5, 29.5, 
35.5, 41.5, 47.5, 53.5, 59.5, 65.5, 71, 2, 5.5, 11.5, 17.5, 23.5, 
29.5, 35.5, 41.5, 47.5, 53.5, 59.5, 65.5, 71, 2, 5.5, 11.5, 17.5, 
23.5, 29.5, 35.5, 41.5, 47.5, 53.5, 59.5, 65.5, 71, 2, 5.5, 11.5, 
17.5, 23.5, 29.5, 35.5, 41.5, 47.5, 53.5, 59.5, 65.5, 71, 2, 
5.5, 11.5, 17.5, 23.5, 29.5, 35.5, 41.5, 47.5, 53.5, 59.5, 65.5, 
71, 2, 5.5, 11.5, 17.5, 23.5, 29.5, 35.5, 41.5, 47.5, 53.5, 59.5, 
65.5, 71, 2, 5.5, 11.5, 17.5, 23.5, 29.5, 35.5, 41.5, 47.5, 53.5, 
59.5, 65.5, 71, 2, 5.5, 11.5, 17.5, 23.5, 29.5, 35.5, 41.5, 47.5, 
53.5, 59.5, 65.5, 71, 2, 5.5, 11.5, 17.5, 23.5, 29.5, 35.5, 41.5, 
47.5, 53.5, 59.5, 65.5, 71, 2, 5.5, 11.5, 17.5, 23.5, 29.5, 35.5, 
41.5, 47.5, 53.5, 59.5, 65.5, 71, 2, 5.5, 11.5, 17.5, 23.5, 29.5, 
35.5, 41.5, 47.5, 53.5, 59.5, 65.5, 71, 2, 5.5, 11.5, 17.5, 23.5, 
29.5, 35.5, 41.5, 47.5, 53.5, 59.5, 65.5, 71, 2, 5.5, 11.5, 17.5, 
23.5, 29.5, 35.5, 41.5, 47.5, 53.5, 59.5, 65.5, 71), y = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5.5, 5.5, 5.5, 5.5, 5.5, 
5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 12, 12, 12, 12, 12, 12, 
12, 12, 12, 12, 12, 12, 12, 18, 18, 18, 18, 18, 18, 18, 18, 18, 
18, 18, 18, 18, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 
24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 36, 36, 
36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 42, 42, 42, 42, 42, 
42, 42, 42, 42, 42, 42, 42, 42, 48, 48, 48, 48, 48, 48, 48, 48, 
48, 48, 48, 48, 48, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 
54, 54, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 66, 
66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 70.5, 70.5, 70.5, 
70.5, 70.5, 70.5, 70.5, 70.5, 70.5, 70.5, 70.5, 70.5, 70.5, 76, 
76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 80.5, 80.5, 80.5, 
80.5, 80.5, 80.5, 80.5, 80.5, 80.5, 80.5, 80.5, 80.5, 80.5), 
    d = c(0.28125, 0.8125, 0.5625, 0.46875, 0.40625, 0.3125, 
    0.25, 0.125, 0.09375, 0.0625, 0.1875, 0.25, 0, 0.375, 0.46875, 
    0.5, 0.4375, 0.4375, 0.3125, 0.28125, 0.1875, 0.125, 0.0625, 
    0.1875, 0.3125, 0.5, 0.375, 0.25, 0.375, 0.4375, 0.375, 0.3125, 
    0.28125, 0.15625, 0.125, 0.0625, 0.1875, 0.3125, 0.5, 0.5625, 
    0.375, 0.4375, 0.40625, 0.375, 0.3125, 0.25, 0.15625, 0.09375, 
    0.0625, 0.125, 0.28125, 0.3125, 0.25, 0.34375, 0.40625, 0.40625, 
    0.375, 0.3125, 0.21875, 0.125, 0.09375, 0.0625, 0.125, 0.25, 
    0.3125, 0.3125, 0.375, 0.40625, 0.40625, 0.375, 0.3125, 0.21875, 
    0.09375, 0.0625, 0, 0.09375, 0.15625, 0.25, 0.28125, 0.34375, 
    0.40625, 0.4375, 0.4375, 0.375, 0.3125, 0.1875, 0.15625, 
    0.0625, 0.125, 0.25, 0.3125, 0.3125, 0.375, 0.4375, 0.46875, 
    0.46875, 0.4375, 0.375, 0.28125, 0.5625, 0.0625, 0.125, 0.25, 
    0.34375, 0.3125, 0.4375, 0.4375, 0.5, 0.5, 0.5, 0.4375, 0.34375, 
    0.21875, 0.0625, 0.125, 0.25, 0.34375, 0.3125, 0.4375, 0.4375, 
    0.46875, 0.5, 0.5, 0.4375, 0.34375, 0.21875, 0.09375, 0.15625, 
    0.3125, 0.34375, 0.25, 0.34375, 0.34375, 0.375, 0.375, 0.6875, 
    0.3125, 0.1875, 0.125, 0.0625, 0.125, 0.25, 0.3125, 0.125, 
    0.21875, 0.28125, 0.28125, 0.25, 0.25, 0.1875, 0.09375, 0.0625, 
    0.0625, 0.1875, 0.3125, 0.4375, 0, 0.125, 0.1875, 0.1875, 
    0.21875, 0.1875, 0.1875, 0.28125, 0.15625, 0.125, 0.125, 
    0.375, 0.625, 0, 0.0625, 0.09375, 0.09375, 0.21875, 0.21875, 
    0.21875, 0.21875, 0.1875, 0.15625, 0.4375, 0.625, 0, 0, 0, 
    0, 0.09375, 0.125, 0.125, 0.09375, 0.0625, 0, 0.125, 0, 0, 
    0)), .Names = c("x", "y", "d"), row.names = c(NA, -195L), class = "data.frame") 

回答

1

傻,傻了,傻了。

ddply的輸出按照它處理它們的順序重新排列,並且當我只提取了height列的輸出時,我完全忽略了(忘記/是無知的)這一事實。所以,即使我的數據首先被yx排序,當我調用ddply來計算基於唯一的x和/然後/ y的東西時,這就是它如何提供輸出。

只是爲了證明這一點:

head(data) 
    x y  d 
1 2.0 0 0.28125 
2 5.5 0 0.40625 
3 11.5 0 0.56250 
4 17.5 0 0.46875 
5 23.5 0 0.40625 
6 29.5 0 0.31250 

而且看着我ddply調用的全輸出顯示他們是如何出現在原始數據y的分組,所以cbind荷蘭國際集團該列作爲data$width正常工作:

widths <- ddply(data, .(y), summarize, width = c(diff(x), 1)) 
head(widths) 
    y width 
1 0 3.5 
2 0 6.0 
3 0 6.0 
4 0 6.0 
5 0 6.0 
6 0 6.0 

但是,當我這樣做,爲的高度,該數據是由獨特的x的分組,這是不是我的數據是如何安排的:

heights <- ddply(data, .(x), summarize, height = c(diff(y), 1)) 
head(heights) 
    x height 
1 2 5.5 
2 2 6.5 
3 2 6.0 
4 2 6.0 
5 2 6.0 
6 2 6.0 

肯定沒有保證的一個問題 - 通過提取只是我想要的專欄中,我完全忽略了ddply輸出的形式相比,我的數據。

要解決這個問題,我大概應該已經創建既與heightwidth(從diff()計算)沿xy值的兩個數據幀,然後通過xy獨特的組合,它們合併。

相關問題