我尝试使用ggplot2
创建热图。
#DATA
set.seed(42)
df1 = data.frame(ID = paste0("I", 1:40),
group = rep(c("Dry", "Rain"), each = 20),
subgroup = rep(paste0("S", 1:4), each = 10),
setNames(data.frame(replicate(8, rnorm(40))), letters[1:8]))
library(reshape2)
df1 = melt(df1, id.vars = c("ID", "group", "subgroup"))
df1 = df1[order(df1$group, df1$subgroup),]
df1$fact = paste(df1$subgroup, df1$ID)
df1$fact = factor(df1$fact, levels = unique(df1$fact))
#PLOT
library(ggplot2)
ggplot(df1, aes(x = variable, y = fact, fill = value)) +
geom_tile() +
scale_y_discrete(labels = df1$subgroup[!duplicated(df1$ID)])
情节正是我想要的,除了标签S1
,S2
,S3
和S4
各自重复10次。有没有办法只显示一次,然后在S1
,S2
,S3
和S4
之间放置一些中断。
我也很好奇是否有办法将group
放在情节中subgroup
左侧作为辅助y轴,但这是可选的。