使用R語(yǔ)言的ggplot2包繪制密碼子偏向性分析中的ENC-plot
將數(shù)據(jù)整理到excel中,第一列GC3s,第二列ENC值,然后復(fù)制到剪切板,通過(guò)read.table()函數(shù)讀入
df <- read.table("clipboard",header=T)
自定義理論ENC計(jì)算函數(shù)
ENC<-function(x){
return(2 + x + 29/(x^2+(1-x)^2))
}
生成繪制曲線需要的數(shù)據(jù)
x <- seq(0,1,by=0.005)
y <- ENC(x)
df1<-data.frame(A=x,B=y)
繪圖
library(ggplot2)
ggplot(df1,aes(x=A,y=B))+geom_line(size=1)+
geom_point(df,aes(x=GC3s,y=ENC))+xlim(0,1)+
ylim(0,70)+labs(x="GC3s",y="ENC")+theme_bw()

28.png
到這一步有人遇到報(bào)錯(cuò)Error: `mapping` must be created by `aes(),具體是什么原因我不知道,對(duì)應(yīng)著把geom_point(df,)改成geom_point(data=df)就不會(huì)有這個(gè)報(bào)錯(cuò)了
ENC<-function(x){
return(2 + x + 29/(x^2+(1-x)^2))
}
生成繪制曲線需要的數(shù)據(jù)
x <- seq(0,1,by=0.005)
y <- ENC(x)
df1<-data.frame(A=x,B=y)
df<-data.frame(GC3s=sample(seq(0,1,by=0.05),15),
ENC=sample(1:60,15))
繪圖
library(ggplot2)
ggplot(df1,aes(x=A,y=B))+geom_line(size=1)+
geom_point(data=df,aes(x=GC3s,y=ENC))+xlim(0,1)+
ylim(0,70)+labs(x="GC3s",y="ENC")+theme_bw()

image.png
如果想為散點(diǎn)添加不同的顏色,自己想到的解決辦法是為數(shù)據(jù)集添加一列因子變量,用來(lái)給散點(diǎn)映射不同的顏色
我的數(shù)據(jù)集中有59個(gè)數(shù)據(jù)
d <- c(LETTERS,letters,paste(LETTERS[1:7],letters[1:7],sep=""))
df$class<-d
ggplot(df1,aes(x=A,y=B))+geom_line(size=1)+
geom_point(df,aes(x=GC3s,y=ENC,color=class))+xlim(0,1)+
ylim(0,70)+labs(x="GC3s",y="ENC")+theme_classic()+
theme(legend.position="none")

29.png
ggplot(df,aes(x=GC3,y=ENC))+geom_point(color=df$class)+
geom_text_repel(data=subset(df,ENC>46.8&ENC<46.82),
direction="x",nudge_y=-10,nudge_x=0.1,
segment.colour = "green",
arrow=arrow(length=unit(0.02,"npc"),type="open",
ends="first"),
segment.size = 1,label="Punica granatum")+
geom_line(data=df1,aes(x=A,y=B),size=1)+ylim(0,70)+theme_classic()+
labs(x="GC3S",y="ENC")
小知識(shí)點(diǎn)
R語(yǔ)言中中的 colors() 函數(shù)可以將所有顏色列出來(lái)
生成漸變色使用函數(shù) colorRampPalette(),用法colorRampPalette(c("red","green"))(10)