我一直在阅读关于R
中的千层面图。在链接的论文附录中,作者有很多实现这些图的代码。但它是一个pdf文件,我无法复制到RStudio中(至少我还没有弄清楚如何)。是否有人将这些代码制作成软件包或以更可用的格式提供了代码?
拉萨尼亚图看起来就像热力图,虽然名字很可爱。其他软件包中的热力图绘制得非常好。考虑 Carl Witthoft 指出的代码中的第一个示例图。您可以像这样在 ggplot
中复现它:
## Create the data
palette <- brewer.pal(4, "PuOr")[-2]
## the matrix containing data for Figure 02a
H.mat <- matrix(NA, nrow=4, ncol=6)
H.mat[1, 1:6] = 100*c(2, 1, 1, 1, 1, 2)
H.mat[2, 1:6] = 100*c(2, 2, 2, 3, 2, 1)
H.mat[3, 1:6] = 100*c(2, 2, 1, 1, 1, 3)
H.mat[4, 1:6] = 100*c(3, 3, 2, 1, 2, 3)
library(ggplot2)
library(reshape2)
rownames(H.mat)<-c('P1','T1','P2','T2')
colnames(H.mat)<-seq(ncol(H.mat))
names(dimnames(H.mat))<-c('Subject','Time')
H.df<-melt(H.mat)
根据您的需求,您可以获得不同类型的着色。
# For continuous values.
ggplot(H.df,aes(x=Time,y=Subject,fill=value)) + geom_tile(colour='black')
# If you consider the value to be categorical.
ggplot(H.df,aes(x=Time,y=Subject,fill=factor(value))) +
geom_tile(colour='black')
# If you want those exact colours the author used:
col<-palette[match(ordered(H.df$value),levels(ordered(H.df$value)))]
ggplot(H.df,aes(x=Time,y=Subject,fill=col)) +
geom_tile(colour='black') + scale_fill_identity()
ggplot
提供自定义颜色,那么请告诉我,我会更改答案。 - nograpes
heatmap()
使用可能会让你朝着某个方向前进... - texb