我的完整数据(结果dput()
)在问题的末尾。我正在尝试制作一个瓷砖图ggplot()
并且具有不均匀的间距x
和y
测量值,因此瓷砖不会填满整个区域。这是一个例子:
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
测量接触之间的距离,但不是其余的。我想我会使用 and 为我的数据创建一个and列height
,但我遇到了奇怪的结果。width
plyr
ddply()
对于那些不打算加载完整数据的人,这里是结构:
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
所以,我正在循环遍历 .x
的每个唯一值的每个值y
。这是我尝试设置高度/宽度列的方法:
# 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
我只是在最后加上 a ,因为for值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")