我的完整數據(結果dput()
)在問題的末尾。我正在嘗試製作一個與ggplot()
的瓷磚情節,並且有不均勻間隔的x
和y
度量,因此瓷磚不會填滿整個區域。這裏有一個例子:ddply具有不同的輸出,取決於用於應用功能的變量的排序/順序
library(ggplot2)
ggplot(data, aes(x = x, y = -y, z = d)) + geom_tile(aes(fill = d))
我不知道肯定,但我認爲ggplot
可能默認爲類似unique(data$x)[2] - unique(data$x)[1]
平鋪尺寸,我的數據,因此排在那裏,這是確實,連續的x
或y
之間的距離觸摸,但沒有休息。我想我會使用plyr
和ddply()
爲我的數據創建一個height
和width
列,但我遇到了奇怪的結果。
對於那些誰不會加載完整的數據,這裏的結構:
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))
寬度是正確的,而不是高度。經調查:
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
不變,然後重新定義我height
和width
列:
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")