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, "Panel1.xlsx"), sheet = "IC") 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("p","+",data1$phen) data1$phen<-gsub("n","-",data1$phen) data1$phen<-gsub("_"," ",data1$phen) data1$phen<-gsub("n","-",data1$phen, fixed = T) data1$phen<-gsub("p","+",data1$phen, fixed = T) data1$phen<-gsub("_"," ",data1$phen) data1[data1$value < 1, "phen"]<-"Other" data1$phen<-gsub("[A-Z]*-*[0-9T]- *", "", data1$phen) data1$phen<-gsub("+ $", "", data1$phen) data1$phen[data1$phen == ""]<-"All Negative" data1["phen1"]<-"PD1" data1[!grepl("PD1+", data1$phen),"phen1"]<-NA data1["phen2"]<-"TIM3" data1[!grepl("TIM3+", data1$phen),"phen2"]<-NA data1["phen3"]<-"CTLA4" data1[!grepl("CTLA4+", data1$phen),"phen3"]<-NA data1["phen4"]<-"LAG3" data1[!grepl("LAG3+", data1$phen),"phen4"]<-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")) color<-c(c("CTLA4+ LAG3+ PD1+ TIM3+"="black","All Negative"="grey90","Other"="grey50", "PD1+"="#C07AFF", "CTLA4+"="#3EB3DE","TIM3+"="#5EF551","LAG3+"="#DEBB3E"), c("CTLA4+ PD1+"="#6666FF","PD1+ TIM3+"="#849CA8", "LAG3+ PD1+"="#C47F9F", "CTLA4+ TIM3+"="#4ED498", "CTLA4+ LAG3+"="#8EB78E", "LAG3+ TIM3+"="#9ED848"), c("CTLA4+ PD1+ TIM3+"="#B81515", "LAG3+ PD1+ TIM3+"="#0f5860")) basic.color<-color[c("PD1+","TIM3+","CTLA4+","LAG3+")] names(basic.color)<-c("PD1","TIM3","CTLA4","LAG3") # Make the plot 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("PD1","TIM3","CTLA4","LAG3"))))+ geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.9, xmin=5.5, fill=factor(phen2, levels=c("PD1","TIM3","CTLA4","LAG3"))))+ geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.4, xmin=6, fill=factor(phen3, levels=c("PD1","TIM3","CTLA4","LAG3"))))+ geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.9, xmin=6.5, fill=factor(phen4, levels=c("PD1","TIM3","CTLA4","LAG3"))))+ scale_fill_manual(values = basic.color, na.value="#FFFFFF00", drop=F, limits=c("PD1","TIM3","CTLA4","LAG3"), name="IC")+ coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially xlim(c(0, 7))+ # Try to remove that to see how to make a pie chart theme_classic()+ guides(fill="none")+ 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("PD1+", "TIM3+", "LAG3+","TIGIT+", "CTLA4+")){ 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("n","-",data2$phen, fixed = T) data2$phen<-gsub("p","+",data2$phen, fixed = T) data2$phen<-gsub("_"," ",data2$phen) # data2[data2$value < 1, "phen"]<-"Other" data2$phen<-gsub("[A-Z]*-*[0-9T]- *", "", 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("PD1", "TIM3", "LAG3","CTLA4")){ 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("PD1", "TIM3", "CTLA4","LAG3")) mtl_rec2$TimePoint<-factor(mtl_rec2$TimePoint, levels=c("W1","W2","W8","W12")) mtl_rec2$Population<-factor(mtl_rec2$Population, levels=c("CD8","CD4")) 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 T cells")+ guides(color="none")+ theme_bw() ggarrange(g_Rec, g_coex, ncol = 1) ggsave(paste0(path,"Analysis/IC_coex.png"), width = 7.5, height = 7.5)