我有这样的数据:
N = 100
matr <- matrix(rnorm(3*6*N), N)
matr_T <- array(as.vector(t(matr)), dim=c(6, 3, N))
这是模拟的结果。我的列代表我的领土,行代表时间单位,3d 代表每列/行的估计参数的模拟集。
我想用这个数据集做几件事。我想做一个函数,允许我为多面板图中的每一列/行(此处为 18 个直方图)制作 3d 数据的直方图。但我还想生成一个数组 (2d)mean()
和另一个数组 (2d) 与sd()
之前的直方图(3 * 6 数组)。可以是apply()
中,我可以选择的功能(hist()
,mean()
,sd()
等),你能帮助我吗?
谢谢 !
现在它使用远优于lapply
情节的情节并以合理的结构进行绘制。
N = 100
matr <- matrix(rnorm(3*6*N), N)
matr_T <- array(as.vector(t(matr)), dim=c(6, 3, N))
apply(matr_T,1:2,mean)
apply(matr_T,1:2,sd)
library(ggplot2) # for histograms
library(cowplot) # for grid arrangement
# create list to access Dimensions in lapply
dims <- list(row = rep(1:nrow(matr_T),each = ncol(matr_T)),
col = rep(1:ncol(matr_T),times = nrow(matr_T)))
plots <- lapply(seq_along(dims$row),
function(i){
ggplot(data.frame(x = matr_T[dims$row[i],dims$col[i],]),aes(x = x)) +
geom_histogram(bins = 10, col = 'black', fill = 'white') +
theme_minimal() +
labs(title = paste('row:',dims$row[i],'\n','col:',dims$col[i]))
})
plot_grid(plotlist = plots, ncol = 3)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句