下面的图C展示的是样本的构成信息,看起来一目了然,搜了一下资料叫做华夫图或者方块图,感觉效果很好,尤其适合多样本信息的展示。其实我看起来很像离散的热图。
我们自己根据图大概准备了一下测试数据。
library(waffle)
library(ggwaffle)
library(ggplot2)
library(dplyr)
library(tidyverse)
library(reshape2)
library(forcats)
data <- read.table("data.txt",sep="\t",header=T)
df <- melt(data,id="Sample") #还是同样的道理,长矩阵变成短矩阵
colnames(df) <- c("Sample","Group","Value")
df$Group<- factor(df$Group) #X轴按照原先的顺序显示
df$Sample <- factor(df$Sample,levels=rev(unique(data$Sample))) #这个主要是因为要倒序,不然ICC1就要再下面了
#这个主要是为了几个表征特征的value也按照图中的顺序排序
df$Value <- factor(df$Value,
levels=c("Group 2",
"Group 3",
"Group 5",
"PS",
"PB",
"P",
"A",
"S",
"B"))
ggplot(df, aes(Group, Sample, fill = Value)) +
geom_waffle()+ #基本就按照value显示方格了
theme(panel.background = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
legend.title = element_blank(),
legend.position = "right",
axis.text.x = element_text(color = 'black',angle = 45,
size = 12,hjust = 0,vjust = 0.1),
axis.text.y = element_text(color = 'black',size = 12))+
scale_x_discrete(position = "top")+ #x轴坐标放在了上面
guides(fill = guide_legend(override.aes = list(size=10),
label.position ='left',
label.theme = element_text(size = 12)))+
scale_fill_manual(values = c("#B5C8E8","#4475C7","#1A3564","#F2B900","#FFE79A","#CD9AFF","#00B1F0","#C6E1B5","#79B84C"),
labels=c('Grade Group2','Grade Group3',"Grade Group5","PSA<10ng/ml","PSA>10ng/ml","Present","Absent","≤ pT3a","≥ pT3a")) #自定义了和图中一样的颜色
也可以换成circle样式,但是在ggplot里面要用color不是fill。
ggplot(df, aes(Group, Sample, color = Value)) +
geom_waffle(tile_shape='circle', size=8)+
theme(panel.background = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
legend.title = element_blank(),
legend.position = "right",
axis.text.x = element_text(color = 'black',angle = 45,
size = 12,hjust = 0,vjust = 0.1),
axis.text.y = element_text(color = 'black',size = 12))+
scale_x_discrete(position = "top")+
guides(fill = guide_legend(override.aes = list(size=10),
label.position ='left',
label.theme = element_text(size = 12)))+
scale_fill_manual(values = c("#B5C8E8","#4475C7","#1A3564","#F2B900","#FFE79A","#CD9AFF","#00B1F0","#C6E1B5","#79B84C"),
labels=c('Grade Group2','Grade Group3',"Grade Group5","PSA<10ng/ml","PSA>10ng/ml","Present","Absent","≤ pT3a","≥ pT3a"))
利用geom_tile也可以实现这个效果。
ggplot(df, aes(Group, Sample, fill = Value)) +
geom_tile(color = "white", size = 0.25) +
theme(panel.background = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
legend.title = element_blank(),
legend.position = "right",
axis.text.x = element_text(color = 'black',angle = 45,
size = 12,hjust = 0,vjust = 0.1),
axis.text.y = element_text(color = 'black',size = 12))+
scale_x_discrete(position = "top")+
guides(fill = guide_legend(override.aes = list(size=10),
label.position ='left',
label.theme = element_text(size = 12)))+
scale_fill_manual(values = c("#B5C8E8","#4475C7","#1A3564","#F2B900","#FFE79A","#CD9AFF","#00B1F0","#C6E1B5","#79B84C"),
labels=c('Grade Group2','Grade Group3',"Grade Group5","PSA<10ng/ml","PSA>10ng/ml","Present","Absent","≤ pT3a","≥ pT3a"))