1

我的完整数据(结果dput())在问题的末尾。我正在尝试制作一个瓷砖图ggplot()并且具有不均匀的间距xy测量值,因此瓷砖不会填满整个区域。这是一个例子:

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],因此我的数据行实际上是连续xy测量接触之间的距离,但不是其余的。我想我会使用 and 为我的数据创建一个and列height,但我遇到了奇怪的结果。widthplyrddply()

对于那些不打算加载完整数据的人,这里是结构:

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()nn-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不变的同时收集了每个唯一的数据,而不是相反,然后重新定义了我的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")
4

1 回答 1

1

傻,傻,傻。

ddply的输出将事物重新排列成它处理它们的顺序,当我仅提取height列的输出时,我完全忽略了(忘记/不知道)这个事实。因此,即使我的数据先按y's 排序,然后按 's 排序x,当我调用ddply基于 unique x's 和 /then/ y's 计算某些东西时,它就是这样提供输出的。

只是为了表明这一点:

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在 as 中的该列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

但是当我为高度这样做时,数据按 unique 分组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与我的数据相比,我完全忽略了输出的形式。

为了解决这个问题,我可能应该创建两个数据框,其中包含xy值以及heightwidth(从 计算diff()),然后通过 和 的唯一组合将它们x合并y

于 2013-08-25T01:48:13.677 回答