简体   繁体   English

密度图矩阵,每个图覆盖两个分布

[英]Matrix of density plots with each plot overlaying two distributions

I have a data.frame with 5 columns and I'd like to generate a matrix of density plots, such that each density plot is an overlay of two density plots. 我有一个包含5列的data.frame ,我想生成一个密度图矩阵,这样每个密度图是两个密度图的叠加。 (This is akin to plotmatrix, except that in my case, the number of non-NA value in each column differ from column to column and I want overlaid distributions rather than scatter plots). (这与plotmatrix相似,除了在我的情况下,每列中非NA值的数量因列而异,我希望覆盖分布而不是散布图)。

My first attempt, which didn't work, is given below: 我的第一次尝试失败了,如下所示:

library(ggplot2)
library(reshape)

tmp1 <- data.frame(do.call(cbind, lapply(1:5, function(x) {
  r <- rnorm(100)
  r[sample(1:100, 20)] <- NA
  return(r)
})))

ggplot( melt(tmp1), aes(x=value, fill=variable))+
  geom_density(alpha=0.2, position="identity")+opts(legend.position = "none")+
  facet_grid(variable ~ variable)

My second approach got me nearly there, but instead of 5 different colors, I only want to use two colors across all the plots. 我的第二种方法使我快到了那儿,但是我只想在所有图中使用两种颜色,而不是5种不同的颜色。 And, I'm sure there is a more elegant way to construct this expanded matrix: 而且,我敢肯定,有一种更优雅的方法来构造此扩展矩阵:

tmp2 <- do.call(rbind, lapply(1:5, function(i) {
  do.call(rbind, lapply(1:5, function(j) {
    r <- rbind(data.frame(var=sprintf('X%d', i), val=tmp1[,i]),
               data.frame(var=sprintf('X%d', j), val=tmp1[,j]))
    r <- data.frame(xx=sprintf('X%d', i), yy=sprintf('X%d', j), r)
    return(r)
  }))
}))
ggplot(tmp2, aes(x=val, fill=var))+
  geom_density(alpha=0.2, position="identity")+opts(legend.position = "none")+
  facet_grid(xx ~ yy)

My solution was to manually loop through the pairs of columns and generate the overlaid density plots by hand, saving them to a list. 我的解决方案是手动遍历各对列并手动生成叠加的密度图,然后将它们保存到列表中。 I then arranged them in a grid using `grid.arrange' giving the image below. 然后我使用`grid.arrange'将它们排列在网格中,如下图所示。

But is it possible to achieve this using facet_grid instead? 但是是否可以使用facet_grid来实现呢?

叠加密度图矩阵

The easiest way is to reshape your data with all permutations (5 * 5 = 25 of them). 最简单的方法是使用所有排列(5 * 5 = 25)来重塑数据。

require(gregmisc)
perm <- permutations(5, 2, paste0("X", 1:5), repeats.allowed=TRUE)
# instead of gregmisc + permutations, you can use expand.grid from base as:
# perm <- expand.grid(paste0("X", 1:5), paste0("X", 1:5))
o <- apply(perm, 1, function(idx) {
    t <- tmp1[idx]
    names(t) <- c("A", "B")
    t$id1 <- idx[1]
    t$id2 <- idx[2]
    t
})
require(ggplot2)
require(reshape2)    
o <- do.call(rbind, o)
o.m <- melt(o, c("id1", "id2"))
o.m$id1 <- factor(o.m$id1)
o.m$id2 <- factor(o.m$id2)
p <- ggplot(o.m, aes(x = value))
p <- p + geom_density(alpha = 0.2, position = "identity", aes(fill = variable)) 
p <- p + theme(legend.position = "none")
p <- p + facet_grid(id1 ~ id2)
p

ggplot2_facet_grid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM