library(tidyverse) library(openxlsx) library(ggpubr) path="C:/Users/47926492N/OneDrive - IDIBELL - Institut d'Investigació Biomèdica de Bellvitge/ImmunoPreserve/TestIP/" data<-read.xlsx(paste0(path, "Panel3.xlsx")) data1<-data %>% gather(phen, value, -sample, -Population) data1["TimePoint"]<-sapply(data1$sample, function(x) strsplit(x, "_")[[1]][2]) data1["sample"]<-sapply(data1$sample, function(x) strsplit(x, "_")[[1]][1]) data1<-data1 %>% group_by(Population, TimePoint, phen) %>% summarise(value=mean(value)) data1$phen<-gsub("."," ",data1$phen, fixed = T) data1$phen<-gsub("[A-Z0-9]*-", "", data1$phen) data1$phen<-gsub(" ", " ", data1$phen) data1$phen<-gsub("[ ]*$", "", data1$phen) data1$phen<-gsub("^[ ]*", "", data1$phen) data1$phen[data1$phen == ""]<-"All Negative" data1["phen1"]<-"CD38" data1[!grepl("CD38+", data1$phen),"phen1"]<-NA data1["phen2"]<-"HLADR" data1[!grepl("HLADR+", data1$phen),"phen2"]<-NA data1["phen3"]<-"GZMB" data1[!grepl("GZMB+", data1$phen),"phen3"]<-NA data1<-data1 %>% arrange(desc(value)) data2<-data1 %>% filter(!phen %in% c("All Negative","Other")) data1<-rbind(data2, data1 %>% filter(phen %in% c("All Negative","Other")) %>% arrange(desc(phen))) data1.list<-list() cont<-1 for (i in data1$Population %>% unique){ for (j in data1$TimePoint %>% unique){ data_temp<-data1 %>% filter(Population == i & TimePoint == j) data_temp$ymax<-cumsum(data_temp$value) data_temp$ymin<-c(0, head(data_temp$ymax, n=-1)) data1.list[[cont]]<-data_temp cont<-cont+1 } } data1<-do.call(rbind, data1.list) data1<-data1 %>% filter(TimePoint %in% c("W1","W2","W8","W12")) data1$TimePoint<-factor(data1$TimePoint, levels=c("W1","W2","W8","W12")) data1$Population<-factor(data1$Population, levels=c("CD8","CD4", "NK")) color<-colorRampPalette(brewer.pal(8, "Set3"))(length(bool.name)) names(color)<-bool.name %>% gsub("[A-Z0-9]*-", "", .) %>% gsub(" "," ",.) %>% gsub("[ ]*$", "", .) %>% gsub("^[ ]*", "", .) %>% gsub("^$","All Negative", .) color["All Negative"]<-"grey90" basic.color<-color[c("CD38+","HLADR+","GZMB+")] names(basic.color)<-c("CD38","HLADR","GZMB") g_coex<-ggplot(data1)+ facet_grid(Population~TimePoint)+ geom_rect(aes(ymax=ymax, ymin=ymin, xmax=4.5, xmin=0), fill=color[data1$phen])+ geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.4, xmin=5, fill=factor(phen1, levels=c("CD38","HLADR","GZMB"))))+ geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.9, xmin=5.5, fill=factor(phen2, levels=c("CD38","HLADR","GZMB"))))+ geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.4, xmin=6, fill=factor(phen3, levels=c("CD38","HLADR","GZMB"))))+ scale_fill_manual(values = basic.color, na.value="#FFFFFF00", drop=F, limits=c("CD38","HLADR","GZMB"), name="Activation")+ coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially xlim(c(0, 6.5)) +# Try to remove that to see how to make a pie chart theme_classic()+ theme(strip.background = element_blank(), strip.text = element_text(size=12, face="bold"), axis.line = element_blank(), axis.ticks = element_blank(), # plot.margin = margin(-200,0,0,0), axis.text = element_blank()) mtl_rec<-as.data.frame(matrix(nrow=0, ncol=3)) colnames(mtl_rec)<-c(colnames(data1)[1:2],"phen") for (rec in c("CD38+", "GZMB+", "HLADR+")){ temp<-data1[grep(rec, data1$phen),] if (nrow(temp) > 0){ mtl_rec<-rbind(mtl_rec, data.frame(temp, "Rec"=rec)) } } mtl_rec_sum<-mtl_rec %>% group_by(TimePoint,Population,Rec, phen) %>% summarise(value=sum(value)) ggplot(mtl_rec_sum, aes(Rec, value, fill=phen))+ facet_grid(Population~TimePoint)+ geom_bar(stat="identity", color="black")+ scale_fill_manual(values = color, name="IC") data2<-data %>% gather(phen, value, -sample, -Population) data2["TimePoint"]<-sapply(data2$sample, function(x) strsplit(x, "_")[[1]][2]) data2["sample"]<-sapply(data2$sample, function(x) strsplit(x, "_")[[1]][1]) data2$phen<-gsub("."," ",data2$phen, fixed = T) data2$phen<-gsub("[A-Z0-9]*-", "", data2$phen) data2$phen<-gsub(" ", " ", data2$phen) data2$phen<-gsub("[ ]*$", "", data2$phen) data2$phen<-gsub("^[ ]*", "", data2$phen) data2$phen[data2$phen == ""]<-"All Negative" data2<-data2 %>% filter(TimePoint %in% c("W1","W2","W8","W12")) mtl_rec2<-as.data.frame(matrix(nrow=0, ncol=3)) colnames(mtl_rec2)<-c(colnames(data2)[1:2],"phen") for (rec in c("CD38", "GZMB", "HLADR")){ temp<-data2[grep(rec, data2$phen),] if (nrow(temp) > 0){ mtl_rec2<-rbind(mtl_rec2, data.frame(temp, "Rec"=rec)) } } mtl_rec2<-mtl_rec2 %>% group_by(sample,Population,TimePoint,Rec) %>% summarise(value=sum(value, na.rm=T)) mtl_rec2$Rec<-factor(mtl_rec2$Rec, levels=c("CD38", "GZMB", "HLADR")) mtl_rec2$TimePoint<-factor(mtl_rec2$TimePoint, levels=c("W1","W2","W8","W12")) mtl_rec2$Population<-factor(mtl_rec2$Population, levels=c("CD8","CD4","NK")) g_Rec<-ggplot(mtl_rec2, aes(TimePoint, value))+ facet_grid(Population~Rec)+ geom_point(color="grey80")+ geom_line(aes(group=sample), color="grey80")+ geom_point(stat="summary", aes(color=Rec), size=2)+ geom_line(aes(group=Rec, color=Rec), stat="summary", size=1)+ scale_color_manual(values = basic.color, name="IC")+ labs(y="% CD8/CD4/NK T cells")+ guides(color="none")+ theme_bw() ggarrange(g_Rec, g_coex, ncol = 1) ggsave(paste0(path, "Analysis/Panel3.png"), width = 7.5, height = 12)