绘制双坐标图

library(ggplot2)
library(scales)
library(magrittr)
library(tidyr)
fst_s_d=read.table("LRP1B_fst_s_d_extend1000kb.txt",header=F)
fst_s_w=read.table("LRP1B_fst_s_w_extend1000kb.txt",header=F)
head(fst_s_d)
colnames(fst_s_d) <- c("chr","pos_start","pos_end","no","no1","fst")
colnames(fst_s_w) <- c("chr","pos_start","pos_end","no","no1","fst")
fst_s_d$pos_start_Mb=fst_s_d$pos_start/1000000
fst_s_d$pos_end_Mb=fst_s_d$pos_end/1000000
fst_s_w$pos_start_Mb=fst_s_w$pos_start/1000000
fst_s_w$pos_end_Mb=fst_s_w$pos_end/1000000
P=ggplot() + 
  geom_line(data=fst_s_d,aes(x = pos_end_Mb,y =fst),colour="red",size=1.5) +
geom_line(data=fst_s_w,aes(x = pos_end_Mb,y = rescale(fst_s_w$fst,c(0,0.05))),colour="black",size=1.5) + 
  scale_y_continuous(breaks=seq(0,0.05,0.01),limits=c(0,0.05),sec.axis = sec_axis( ~rescale(.,c(min(fst_s_w$fst),max(fst_s_w$fst)))))+    #这一步必须加上break后面的,因为图是以此尺度为依据进行数据转换
  labs(x = "Position(Mb)", y = "Fst(Sonid vs Wild)") +
  theme_bw(base_size = 16)+
  theme(panel.grid = element_blank(),axis.line = element_line(colour = 'black'),panel.background = element_rect(fill = "transparent"),axis.title.y = element_text(face = "bold"))+
  theme(axis.text.x = element_text(angle = 270,size=9.5,face="bold",hjust = 0.5, vjust = 0.5))+theme(plot.title = element_text(hjust =0.5,size=11,face="bold"))
ggsave(P,file="B3.tiff",width=12,height=3)

你可能感兴趣的:(绘制双坐标图)