在之前提到的tidytuesday里没有找到关于箱线图和小提琴图比较好的实例,这周的ggplot2每周一图活动用之前推文中的内容作为实例进行讲解,之前的推文是跟着Nature学作图:R语言ggplot2箱线图、小提琴图、抖动散点图
dfToPlot<-read.csv("dfToPlot.csv")
head(dfToPlot)
dfToPlot$RELATIONSHIP.0 <- factor(dfToPlot$RELATIONSHIP.0,
levels=c("RND.PAIR","PARTNERS","PARENT_CHILD","SIBLINGS"))
library(ggplot2)
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
y=BC_Spec,
color=RELATIONSHIP.0))+
geom_boxplot() -> p1
p1
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
y=BC_Spec,
color=RELATIONSHIP.0))+
geom_jitter() -> p2
p2
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
y=BC_Spec,
color=RELATIONSHIP.0))+
geom_violin() -> p3
p3
library(patchwork)
p1+p2+p3+
plot_layout(guides = "collect")
cbPalette <- c("#E69F00", "#CC79A7", "#56B4E9", "#009E73", "#CC79A7", "#F0E442", "#999999","#0072B2","#D55E00")
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
y=BC_Spec,
color=RELATIONSHIP.0))+
geom_jitter(alpha=0.2,
position=position_jitterdodge(jitter.width = 0.35,
jitter.height = 0,
dodge.width = 0.8))+
geom_boxplot(alpha=0.2,width=0.45,
position=position_dodge(width=0.8),
size=0.75,outlier.colour = NA)+
geom_violin(alpha=0.2,width=0.9,
position=position_dodge(width=0.8),
size=0.75)+
scale_color_manual(values = cbPalette)+
theme_classic() +
theme(legend.position="none") +
theme(text = element_text(size=16)) +
#ylim(0.0,1.3)+
ylab("Bray-Curtis distance of Species")
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
y=BC_Spec,
color=RELATIONSHIP.0))+
geom_jitter(alpha=0.2,
position=position_jitterdodge(jitter.width = 0.35,
jitter.height = 0,
dodge.width = 0.8))+
geom_boxplot(alpha=0.2,width=0.45,
position=position_dodge(width=0.8),
size=0.75,outlier.colour = NA)+
geom_violin(alpha=0.2,width=0.9,
position=position_dodge(width=0.8),
size=0.75)+
scale_color_manual(values = cbPalette)+
theme_classic() +
theme(legend.position="none") +
theme(text = element_text(size=16)) +
#ylim(0.0,1.3)+
ylab("Bray-Curtis distance of Species")+
#scale_x_discrete(labels=c("A","B","C","D"))+
annotate("segment", x = 1-0.01, y = 1, xend = 2.01,lineend = "round",
yend = 1,size=1,colour="black",arrow = arrow(length = unit(0.02, "npc")))+
annotate("segment", x = 2.01, y = 1, xend = 0.99,lineend = "round",
yend = 1,size=1,colour="black",arrow = arrow(length = unit(0.02, "npc")))+
annotate("text", x=1.5,y=1.01,
label=expression("**"~"FDR"~2.41%*%10^-10),vjust=0) -> p4
p4
p4+p4+scale_color_manual(values = cbPalette[5:8])
最终结果
image.png
image.png