使用@BrodieG 的data.long,这个情节可能更容易解释一些。
library(ggplot2)
library(RColorBrewer) # for brewer.pal(...)
ggplot(data.long) +
geom_bar(aes(x=x, y=success, fill=count),colour="grey70",stat="identity")+
scale_fill_gradientn(colours=brewer.pal(9,"RdYlGn")) +
facet_grid(group~.)
请注意,实际值可能不同,因为您在样本中使用了随机数。将来,考虑使用set.seed(n) 生成可重现的随机样本。
编辑 [回复 OP 的评论]
您会得到 x 轴和构面标签的数字,因为您从矩阵而不是 data.frames 开始。因此,将success 和samples 转换为data.frames,将列名设置为您的测试名称,并在group 列前添加“因素列表”。现在转换为长格式有点不同,因为第一列有组名。
library(reshape2)
set.seed(1)
success <- data.frame(matrix(runif(m*n,0,1),m,n))
success <- cbind(group=rep(paste("Factor",1:nrow(success),sep=".")),success)
samples <- data.frame(floor(MAX*matrix(runif(m*n),m))+1)
samples <- cbind(group=success$group,samples)
data.long <- cbind(melt(success,id=1), melt(samples, id=1)[3])
names(data.long) <- c("group", "x", "success", "count")
设置阈值颜色的一种方法是向data.long 添加一列并将其用于fill:
threshold <- 25
data.long$fill <- with(data.long,ifelse(count>threshold,max(count),count))
把它们放在一起:
library(ggplot2)
library(RColorBrewer)
ggplot(data.long) +
geom_bar(aes(x=x, y=success, fill=fill),colour="grey70",stat="identity")+
scale_fill_gradientn(colours=brewer.pal(9,"RdYlGn")) +
facet_grid(group~.)+
theme(axis.text.x=element_text(angle=-90,hjust=0,vjust=0.4))
最后,当您有 x 轴标签的名称时,它们往往会卡在一起,所以我将名称旋转了 -90°。