install.packages("plotly")
install.packages("ggplot2")
install.packages("ggpubr")
library(ggplot2)
library(ggpubr)
ifelse(dir.exists("opFig"),FALSE,dir.create("opFig"))#建一個opFig的文件夾贴汪,后續(xù)你生成的所有圖片都會出現(xiàn)在這里
drug <- as.data.frame(drug)
for (dr in rownames(drug)) {#如果給定藥物 把循環(huán)rownames(drug)換成你的藥物向量,比如for (dr in "tamoxifen")
tit <- paste0("R:",round(plotly::filter(output,Drug == dr)[,3],2),",p value = ",round(plotly::filter(output,Drug == dr)[4],3))
colnames(drug) == colnames(exp)
df <- rbind(drug[dr,],exp) %>% t() %>% as.data.frame()
colnames(df)[1] <- c("drug")
p <- ggplot(data = df, aes(x = drug, y = MSI1)) + #數(shù)據(jù)映射
geom_point(alpha = 0.6,shape = 19,size=3,color="#DC143C") +#散點(diǎn)圖,alpha就是點(diǎn)的透明度
#geom_abline()+
labs(title = tit)+
geom_smooth(method = lm, formula = y ~ x,aes(colour = "lm"), size = 1.2,se = T)+
scale_color_manual(values = c("#808080")) + #手動調(diào)顏色c("#DC143C","#00008B", "#808080")
theme_bw() +#設(shè)定主題
theme(axis.title=element_text(size=15,face="plain",color="black"),
axis.text = element_text(size=12,face="plain",color="black"),
legend.position = "none",
panel.background = element_rect(fill = "transparent",colour = "black"),
plot.background = element_blank(),
plot.title = element_text(size=15, lineheight=.8,hjust=0.5, face="plain"),
legend.margin = margin(t = 0, r = 0, b = 0, l = 0, unit = "pt"))+
ylab(paste0("Activity z scores of ",dr)) + #expression的作用就是讓log10的10下標(biāo)
xlab(paste0("The expression of ",gene))
ggsave(filename = paste0("opFig/",gene,"-",dr,"-cor.pdf"),plot = p,width = 5,height = 5)
}
出圖是這樣的:舉個栗子