ggplot作图之前我们使用过geom_rect函数为图形增加背景,但是仅仅限于图形内部,那么如何对整个ggplot背景进行改色呢?我们使用一个简单的火山图示例图,首先做一个火山图:
setwd("E:/生物信息学/ggplot火山图")
df <- read.csv("DEG.csv",header = T)
默认无修饰背景的图如下:
p <- ggplot(df, aes(x=avg_log2FC, y=-log10(p_val_adj))) +
geom_hline(aes(yintercept=1.3), color = "#999999", linetype="dashed", size=1)+
geom_vline(aes(xintercept= -0.25), color = "#999999", linetype="dashed", size=1)+
geom_vline(aes(xintercept= 0.25), color = "#999999", linetype="dashed", size=1)+
geom_point(size=3, shape=21, color="black", fill="grey") +
theme_bw()+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.border = element_blank(),
axis.line = element_line(colour = "black",size = 0.5),
axis.title =element_text(size = 14, color = "black"),
axis.text =element_text(size = 12, color = "black"))+
labs(x = "Log2 fold change",y = "-Log10(P-value)", title = "") +
annotate("text", label = "Down-regulated DEGs\nin treatment",
x = -2.2, y = 250, size=5)+
annotate("text", label = "Up-regulated DEGs\nin treatment",
x = 2.2, y = 250, size= 5)
去掉theme_bw(),增加ggthemes::theme_solarized(),即更改主题,就可以得到灰黄色的背景色了。这个图还是很有质感的,可以应用到其他图形中。
library(ggplot2)
ggplot(df, aes(x=avg_log2FC, y=-log10(p_val_adj))) +
ggthemes::theme_solarized()+
geom_hline(aes(yintercept=1.3), color = "#999999", linetype="dashed", size=1)+
geom_vline(aes(xintercept= -0.25), color = "#999999", linetype="dashed", size=1)+
geom_vline(aes(xintercept= 0.25), color = "#999999", linetype="dashed", size=1)+
geom_point(size=3, shape=21, color="black", fill="grey") +
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.border = element_blank(),
axis.line = element_line(colour = "black",size = 0.5),
axis.title =element_text(size = 14, color = "black"),
axis.text =element_text(size = 12, color = "black"))+
labs(x = "Log2 fold change",y = "-Log10(P-value)", title = "") +
annotate("text", label = "Down-regulated DEGs\nin treatment",
x = -2.2, y = 250, size=5)+
annotate("text", label = "Up-regulated DEGs\nin treatment",
x = 2.2, y = 250, size= 5)
当然了,更加高级的方式则是使用渐变色:
ggplot(df, aes(x=avg_log2FC, y=-log10(p_val_adj))) +
geom_hline(aes(yintercept=1.3), color = "#999999", linetype="dashed", size=1)+
geom_vline(aes(xintercept= -0.25), color = "#999999", linetype="dashed", size=1)+
geom_vline(aes(xintercept= 0.25), color = "#999999", linetype="dashed", size=1)+
geom_point(size=3, shape=21, color="black", fill="grey") +
theme_bw()+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.border = element_blank(),
axis.line = element_line(colour = "black",size = 0.5),
axis.title =element_text(size = 14, color = "black"),
axis.text =element_text(size = 12, color = "black"))+
labs(x = "Log2 fold change",y = "-Log10(P-value)", title = "") +
annotate("text", label = "Down-regulated DEGs\nin treatment",
x = -2.2, y = 250, size=5)+
annotate("text", label = "Up-regulated DEGs\nin treatment",
x = 2.2, y = 250, size= 5)
library(RColorBrewer)
library(grid)
color <- colorRampPalette(brewer.pal(11,"RdBu"))(15)
grid.raster(scales::alpha(color, 0.3),
width = unit(1, "npc"),
height = unit(1,"npc"),
interpolate = T)
之前在一篇文章中见到过这种做法,找不到原文了,如果找到了再补上吧。这种新奇的背景应用到柱状图、小提琴图、折线图等等图形修饰中,都会有更好的效果!
转自:KS科研分享与服务
如有侵权,请联系本站删除!