ggplot---展示带下标的log2FC和-log10(pvalue)

格式如下:

xlab(expression("-log"["2"]*"FC"))+
  ylab(expression("-log"["10"]*"P Value"))+

注意expression里面要用*把文本隔开,另外,上标用^,下标用[]
简单画一个火山图

library(ggplot2)
library(openxlsx)
library(dplyr)
library(ggrepel)


#测试数据
set.seed(2022)
data<-data.frame(logFC=rnorm(10000,0,4),
                 P.Value=rnorm(10000,0.05,0.03))%>%
  filter(P.Value<1&P.Value>0)

data<-data%>%
  mutate(label="")%>%
  arrange(logFC)%>%{.$label[1:10]=letters[1:10];.}%>%
  arrange(-logFC)%>%{.$label[1:10]=LETTERS[1:10];.}%>%
  mutate(color=ifelse(label=="","NA",label))
  
  


map_color<-unique(data$color)%>%{a=ifelse(.=="NA","grey","red");names(a)=setNames(nm = .);a}
ggplot(data,aes(x=logFC, y=-log10(P.Value),color=color,size=abs(logFC)))+
  # 横向水平参考线:
  geom_hline(yintercept = -log10(0.05), linetype = "dashed", color = "#999999")+
  # 纵向垂直参考线:
  geom_vline(xintercept = c(-1,1), linetype = "dashed", color = "#999999")+
  # 散点图:
  geom_point(data = data%>%filter(label==""),aes(x=logFC, y=-log10(P.Value),color=color,size=abs(logFC)))+
  geom_point(data = data%>%filter(label!=""),aes(x=logFC, y=-log10(P.Value),color=color,size=abs(logFC)))+
  # # 指定散点大小渐变模式:
  scale_size_continuous(range = c(0.5,3))+
  # 添加标签:
  geom_text_repel(aes(label=label),size=5,max.overlaps = 100,max.time = 20,max.iter = 10000)+
  guides(color=F,size=F)+
  # 修改坐标轴:
  xlab(expression("-log"["2"]*"FC"))+
  ylab(expression("-log"["10"]*"P Value"))+
  scale_color_manual(values = map_color)+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5),
        axis.title = element_text(size = 15),
        axis.text = element_text(size = 12,face= "bold"),
        axis.ticks = element_line(size = 0.8)) ->ggplot_res


ggsave("plot.tiff",ggplot_res,width = unit(8,"cm"),height = unit(6,"cm"),dpi = 300)
image.png

你可能感兴趣的:(ggplot---展示带下标的log2FC和-log10(pvalue))