从分箱数据生成等高线图

Producing a contour plot from binned data

我希望根据已合并的数据生成等高线图。我有两列,一列代表化合物的质量,另一列是其皮尔逊相关系数值。这是我到目前为止所做的一个小例子:-

column1 <- as.numeric(c("100.01", "100.015", "100.017", "100.071", "100.099", "100.111", "100.153", "100.167"))
column2 <- as.numeric(c("0.89", "0.64", "-0.14", "-0.79", "1", "0.31", "-0.27", "0.45"))
test <- cbind(column1, column2)
bin1 <- seq(100, 100.2, by = 0.05)
bin2 <- seq(-1, 1, by = 0.5)
 res <- data.frame(Map(function(x,y) cut(x, breaks=y),
                    as.data.frame(test), list(bin1, bin2)))

 res1 <- cbind(test, res)
 str(res1)
'data.frame':   8 obs. of  4 variables:
 $ column1: num  100 100 100 100 100 ...
 $ column2: num  0.89 0.64 -0.14 -0.79 1 0.31 -0.27 0.45
 $ column1: Factor w/ 4 levels "(100,100.05]",..: 1 1 1 2 2 3 4 4
 $ column2: Factor w/ 4 levels "(-1,-0.5]","(-0.5,0]",..: 4 4 2 1 4 3 2 3

据此,我想生成一个等高线图,其中根据第二列中绘制的值的频率绘制了从第一列分箱的值的频率。但是,它需要通过将第四列的 bins 按第三列分组来完成。所以通过这样做:-

combined <- split(res1[, 4], res1[, 3])
str(combined)
List of 4
 $ (100,100.05]  : Factor w/ 4 levels "(-1,-0.5]","(-0.5,0]",..: 4 4 2
 $ (100.05,100.1]: Factor w/ 4 levels "(-1,-0.5]","(-0.5,0]",..: 1 4
 $ (100.1,100.15]: Factor w/ 4 levels "(-1,-0.5]","(-0.5,0]",..: 3
 $ (100.15,100.2]: Factor w/ 4 levels "(-1,-0.5]","(-0.5,0]",..: 2 3

然后我想生成一个图,其中落入 bin 范围 100、100.05 的值的频率与落入四个单独因子水平的值的频率相对应。因此,如果 20 个值落入 100、100.05 的第一个 bin,我想看看这些值中有多少落入 -1、-0.5、-0.5、0 等的 bin,进而构建 3D 图。有没有办法做到这一点?我知道我能做到:-

cbind(table(res[, 3])

为了获得落入质量箱范围的值的频率,我只是不知道如何提取落入给定质量箱范围的皮尔逊相关系数箱范围内的值。

干杯

你可以试试

lapply(combined, table)

获取第 4 列中 bin 的频率