箱图geom_boxplot
例子数据格式
name | value |
---|---|
A | 3.248795062 |
A | 12.71400397 |
B | 12.46654939 |
B | 12.62122515 |
C | 27.32492551 |
C | 28.30969191 |
D | 10.21093023 |
D | 10.63176794 |
太长不写
# 基础箱图
p<-ggplot(data, aes(x=name, y=value)) + geom_boxplot()
# 90度旋转
p + coord_flip()
# 缺口式
ggplot(data, aes(x=name, y=value)) + geom_boxplot(notch=TRUE)
# 改变outlier的样式,颜色等
ggplot(data,aes(x=name,y=value,color=name)) + geom_boxplot(outlier.colour="red", outlier.shape=8, outlier.size=4)
# 使用stat_summary给箱添加均值
p + stat_summary(fun.y=mean, geom="point", shape=23, size=4)
# 自定义选择要展示的组
p + scale_x_discrete(limits=c("A", "D"))
# 使用geom_dotplot()或geom_jitter() 增加箱上的点
p + geom_dotplot(binaxis='y', stackdir='center', dotsize=1)
p + geom_jitter(shape=16, position=position_jitter(0.2))
0.2 : degree of jitter in x direction
# 改变颜色,改变外周颜色
p1<-ggplot(x=name,y=value,color=name)) + geom_boxplot()
也可以使用其他function改变颜色
to use custom colors: p1+scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
to use color palettes from RColorBrewer package: p1+scale_color_brewer(palette="Dark2")
to use grey color palettes: p1 scale_color_grey() + theme_classic()
# 改变颜色,改变填充颜色
单色
ggplot(data, aes(x=name,y=value,fill=name)) + geom_boxplot(fill='#A4A4A4', color="black")+
按组分色
p2<-ggplot(data,(x=name,y=value,fill=name)) + geom_boxplot()
也可以用function改变颜色
p+scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
p+scale_fill_brewer(palette="Dark2")
p + scale_fill_grey() + theme_classic()
# 改变legend位置
p + theme(legend.position="top")
p + theme(legend.position="bottom")
p + theme(legend.position="none") # Remove legend
# 改变排列顺序,使用scale_x_discrete
p + scale_x_discrete(limits=c("D","C","B","A"))
# 画多组箱
改变组的颜色
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) + geom_boxplot()
改变组的位置
p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) + geom_boxplot(position=position_dodge(1))
# 加点,改变点颜色
p + geom_dotplot(binaxis='y', stackdir='center', position=position_dodge(1))
p + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
#边框,背景
# 去掉背景,黑框框起来
p+theme(panel.background=element_blank(),panel.border=element_rect(linetype="solid",fill=NA))
# 横纵坐标字体和大小
p+theme(axis.text=element_text(size=10,color="black"),axis.title=element_text(size=12,face="bold",color="black"))
#小提琴或箱图+散点(抖动)
data=read.table("file.txt",header=T)
head(data)
Value Class Group
111 GP1 pseudo
222 GP1 pseudo
333 CTRL pseudo
444 GP1 live
555 CTRL live
data$Class=factor(data$Class,levels=c("GP1","CTRL"))
df1=subset(data,Class=="CTRL")
df2=subset(data,Class=="GP1")
ggplot(data,aes(x=Group,y=Value)) +
geom_violin(draw_quantiles=c(0.25,0.5,0.75)) +
geom_boxplot(width=0.3) +
geom_point(data=df1,aes(fill=Class),position=position_jitterdodge(jitter.width=0,jitter.height=0.5,dodge.width=0),shape=21,size=2) +
geom_point(data=df2,aes(fill=Class),position=position_jitterdodge(jitter.width=2,jitter.height=0.5,dodge.width=0),shape=21,size=2) +
scale_fill_manual(values=c("#CCCCCC","#3F9BFF")) +
theme(axis.text=element_text(color="black"),panel.background=element_blank(),panel.border=element_rect(fill="NA",color="black")) +
xlab("") + ylab("")
柱图geom_bar
堆叠柱图
library(ggplot2)
data=read.table("5-AMR_class.txt",sep="\t",header=T)
ggplot(data,aes(x=ID1,y=Ratio,fill=Class1))+
geom_bar(stat="identity",position="stack",width=0.6)+
facet_grid(.~ID2,scales="free",space="free_x")+
theme_bw()+theme(legend.title=element_blank(),plot.title=element_text(size=12,face="bold",vjust=0.5,hjust=0.5),axis.text=element_text(face="bold"))+
scale_fill_manual(values=c("#A69ADC","#617F2E","#8EC9CE","#C74F7F","#6F42AF","#A5EAA4","#7D3E2F","#4E2B53","#D14A33","#8242B5","#8DCE2A","#D5B8A7","#C457B3","#D8D34B","#CC8C39","#3D433B"))+
labs(x="",y="Relative abundance")
做统计
library(ggplot2)
library(ggpubr)
data=read.table("2-group_rank_revised.txt",header=T,sep="\t")
my_comparisons=list(c("group1","group4"),c("group2","group4"),c("group3","group4"),c("group5","group4"))
ggplot(data,aes(x=Group,y=inClass,fill=Group)) + geom_violin() + geom_jitter(height=0,width=0.1) +
stat_compare_means(comparisons=my_comparisons)
ggplot(data,aes(x=Group,y=Range,fill=Group)) + geom_violin() + geom_jitter(height=0,width=0.1) +
stat_compare_means(comparisons=my_comparisons)
data=read.table("2-noSymptom_78p_rank_revised.txt",header=T,sep="\t")
ggplot(data,aes(x=factor(inClass),y=Range,fill=factor(inClass))) + geom_violin() + geom_jitter(height=0,width=0.1) +
stat_compare_means()
详细请参考:
https://www.jianshu.com/p/b7274afff14f?from=timeline
R-连线图geom_segment geom_curve
geom_segment(data=d, mapping=aes(x=x,y=y,xend=xend,yend=yend), arrow=arrow(length=unit(0.2,"cm")), size=2, color="blue")
geom_curve(aes(x = x1, y = y1, xend = x2, yend = y2, colour = "curve"), data = df)
饼图饼图geom_bar
data=read.table("7-stat.txt",header=T,sep="\t")
data=subset(data,Class=="Class")
ggplot(data,aes(x="",y=Count/Total,fill=Value)) +
geom_bar(stat="identity",width=0.5,position="stack") +
coord_polar(theta="y",start=0) +
scale_fill_manual(values=c("#FFCCCC","#FFCC66","#FF6633","#FF0066")) +
theme(panel.border=element_rect(color="black",fill=NA)) +
theme(panel.background=element_blank()) + facet_wrap(Clade~.) +
xlab("") + ylab("") +ggtitle("Classes in clades")
详细内容请参考:
https://www.jianshu.com/p/8b66e0be3e70
ggplot一些细节修改
1. 添加一条参考线
geom_hline(yintercept=50,color="red",linetype="dashed")
geom_vline(xintercept=30)
...
geom_hline(aes(yintercept=wt,colour=wt),mean_wt)
2. 去掉背景灰色,加黑色边框
theme(panel.background=element_blank())
theme(panel.border=element_rect(fill="NA",color="black"))
3. 坐标轴显示,最大最小和手动间隔
scale_x_continuous(limits=c(1,30000))
scale_x_continuous(breaks=c("1","10000","20000","30000"))
scale_x_continuous(label=c("1","10K","20K","30K"))
4. 多个pheatmap一起输出
p1=pheatmap(d1)
p2=pheatmap(d2)
p3=pheatmap(d3)
p4=pheatmap(d4)
g1=as.ggplot(p1)
g2=as.ggplot(p2)
g3=as.ggplot(p3)
g4=as.ggplot(p4)
cowplot::plot_grid(g1, g2, g3,g4,ncol=2)
散点图
散点图,坐标轴上打点
ID | Cell | Trend | FC1 | FC2 | Flag |
---|---|---|---|---|---|
ENSMUSG00000079499.9 | G1 | FU | 0.107703308675386 | 2.27327271386235 | Lable |
ENSMUSG00000097195.9 | G2 | FU | 0.0931022969618995 | 1.55652034686497 | NO |
ENSMUSG00000103041.1 | G3 | FU | 0.737337372929388 | 1.27351982599697 | NO |
ENSMUSG00000024845.17 | G1 | FU | 0.624131572941305 | 1.32170094722654 | Lable |
ENSMUSG00000085227.1 | G2 | FU | -0.0780060190505009 | 2.33854218716707 | NO |
ENSMUSG00000100182.6 | G3 | FU | -0.242821120121383 | 1.0630250363417 | NO |
ENSMUSG00000087593.1 | G1 | FU | 0.523572061612209 | 1.15169763513402 | NO |
ENSMUSG00000022639.14 | G2 | FF | -0.69059564946632 | -0.437309491202036 | NO |
分组,并标出想标的ID
library(ggplot2)
data=read.table("test.txt",header=T)
ggplot(data,aes(x=FC1,y=FC2,colour=Trend,label=ID))+geom_point(size=2,alpha=.6)+
geom_text(aes(label=ifelse(Flag=="Lable",as.character(ID),'')),hjust=0,vjust=0)+
scale_colour_manual(values=c("#0066CC","#99CCFF","#FFCC00","#99CCFF","#D1D1CF","#FFCCFF","#FFCC00","#FFCCFF","#FF3300"))+
scale_fill_manual(values=c("#0066CC","#99CCFF","#FFCC00","#99CCFF","#D1D1CF","#FFCCFF","#FFCC00","#FFCCFF","#FF3300"))+
facet_grid(Cell~.)+xlab("x axis")+ylab("y axis")
#############
library(ggplot2)
data=read.table("test.txt",header=T)
ggplot(data,aes(x=FC1,y=FC2,colour=Trend,label=ID))+geom_point(size=2,alpha=.6)+
geom_text(data=subset(data,Flag=="Lable"),hjust=0,vjust=0)+
scale_colour_manual(values=c("#0066CC","#99CCFF","#FFCC00","#99CCFF","#D1D1CF","#FFCCFF","#FFCC00","#FFCCFF","#FF3300"))+
scale_fill_manual(values=c("#0066CC","#99CCFF","#FFCC00","#99CCFF","#D1D1CF","#FFCCFF","#FFCC00","#FFCCFF","#FF3300"))+
facet_grid(Cell~.)+xlab("x axis")+ylab("y axis")
#############
修改点的形状
show_point_shapes() #显示点的形状(ggpubr包内的函数)
scale_shape_manual(values = c(15, 19, 17)) + #自定义点的形状,分别为15, 19, 17
其他参数可参考的:
https://www.lizenghai.com/archives/22371.html