假设我做了四次测量/实验(M1..M4),产生的强度值在 5 个位置(loc_1...5)之间变化。我观察了各种类型的元素(n = 7)。现在我想使用 ggplot2 中的 facet_grid 和 geom_raster 将结果总结为一个图表。我最终得到下图(见图)。
现在的问题是是否有一个简单的解决方案来强制行填充每个方面的空间(即删除每个面板中未使用的行)。
谢谢
rm(list=ls())
library(ggplot2)
library(reshape2)
set.seed(123)
# let's create a fake dataset
nb.mesure <- 4
n.row <- 200
n.col <- 5
nb.class <- 7
d <- matrix(round(runif(n.row * n.col),2), nc=n.col)
colnames(d) <- sprintf("Loc_%02d", 1:5)
# These strings will be the row names of each heatmap
# in the subsequent facet plot
elements <- sample(replicate(n.row/2, 1:100))
# let's create a data.frame d
d <- data.frame(d,
mesure = sort(rep(c("M1","M2","M3", "M4"), n.row/4)),
elements= elements,
class=sample(nb.class,
length(elements),
rep=T,
prob = seq(0.01, 0.25, length.out=7))
)
# Data are melt
dm <- melt(d, id.var=c( "mesure", "elements", "class"))
colnames(dm) <- c("mesure","elements", "class", "pos", "intensity")
# Plotting
p <- ggplot(dm, aes(x = pos, y = elements, fill = intensity))
p <- p + geom_raster()
p <- p + facet_grid(mesure~class , scales = "free", space="free_y")
p <- p + theme_bw()
p <- p + theme(text = element_text(size=8))
p <- p + theme(text = element_text(family = "mono", face = "bold"))
p <- p + theme(axis.text.y = element_blank(),
axis.ticks.y=element_blank(),
axis.text.x = element_text(colour="grey20",
size=6,angle=45,
vjust = 0.3))
print(p)