歡迎關(guān)注公眾號(hào)查看原文
本期我們用ggplot2繪制箱線圖渡紫,直接上圖:
1.加載包
rm(list=ls())
#加載包
library(tidyverse)
library(ggplot2)
library(ggsci)
library(cowplot)
library(openxlsx)
library(ggsignif)##可以加P值,ggstatsplot超強(qiáng)統(tǒng)計(jì)包
2.加載數(shù)據(jù)
setwd("D:/本人信息/生信小書(shū)生/R語(yǔ)言繪圖/2022-3-13箱線圖")#載入路徑
data <- read.xlsx("箱線圖.xlsx")
3.繪圖-箱線圖
###箱型圖
ggplot(data,aes(x=Name,y=Value))+
geom_boxplot(aes(fill=Name)#,notch = T,notchwidth = 0.7 #變成鋸齒狀
)+
geom_point(aes(color=Name),position=position_jitter(width=0.15),size=2)+
geom_signif(
comparisons = list(c("CTRL","TP1")),test="t.test",
tip_length = 0.02,size = 1,textsize = 4,#增加***尺寸
map_signif_level = T #用*表示顯著性,*---0.05考赛,**---0.01是钥,***---0.001
)+
scale_fill_lancet()+
theme_cowplot() + theme(legend.position = c(0.85, 0.2))+
theme_classic()+
ggtitle("boxplot")+
labs(title = 'boxplot', # 添加主標(biāo)題
x = 'Name', # x軸的名字
y = 'Value',
subtitle = 'Plot of name by value', # 添加次標(biāo)記
caption = 'Data source: LX')+ #添加腳注
theme(plot.title = element_text(color = 'black', size = 16, hjust = 0.5),
plot.subtitle = element_text(color = 'black', size = 16,hjust = 0.5),
plot.caption = element_text(color = 'black', size = 16,face = 'italic', hjust = 1),
axis.text.x = element_text(color = 'black', size = 16, angle = 0),
axis.text.y = element_text(color = 'black', size = 16, angle = 0),
axis.title.x = element_text(color = 'black', size = 16, angle = 0),
axis.title.y = element_text(color = 'black', size = 16, angle = 90),
legend.title = element_text(color = 'black', size = 16),
legend.text = element_text(color = 'black', size = 16),
axis.line.y = element_line(color = 'black', linetype = 'solid'), # y軸線特征
axis.line.x = element_line (color = 'black',linetype = 'solid'), # x軸線特征
panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA)) # 圖四周框起來(lái)
4.小提琴圖
#####小提琴圖----多了一個(gè)密度曲線
ggplot(data,aes(x=Name,y=Value))+
geom_violin(aes(fill=Name),alpha=0.7)+
#geom_point(aes(color=Species),position = "jitter")+#加抖動(dòng)點(diǎn)
geom_point(position = position_jitter(width=0.15),size=1)+###縮小抖動(dòng)點(diǎn)
geom_boxplot(width=0.1)+
geom_signif(
comparisons = list(c("CTRL","TP1")),test="t.test",
tip_length = 0.02,size = 1,textsize = 4,#增加***尺寸
map_signif_level = T #用*表示顯著性郑临,*---0.05恢口,**---0.01呆躲,***---0.001
)+
scale_fill_lancet()+
theme_cowplot() + theme(legend.position = c(0.85, 0.2))+
theme_classic()+
ggtitle("boxplot")+
labs(title = 'boxplot', # 添加主標(biāo)題
x = 'Name', # x軸的名字
y = 'Value',
subtitle = 'Plot of name by value', # 添加次標(biāo)記
caption = 'Data source: LX')+ #添加腳注
theme(plot.title = element_text(color = 'black', size = 16, hjust = 0.5),
plot.subtitle = element_text(color = 'black', size = 16,hjust = 0.5),
plot.caption = element_text(color = 'black', size = 16,face = 'italic', hjust = 1),
axis.text.x = element_text(color = 'black', size = 16, angle = 0),
axis.text.y = element_text(color = 'black', size = 16, angle = 0),
axis.title.x = element_text(color = 'black', size = 16, angle = 0),
axis.title.y = element_text(color = 'black', size = 16, angle = 90),
legend.title = element_text(color = 'black', size = 16),
legend.text = element_text(color = 'black', size = 16),
axis.line.y = element_line(color = 'black', linetype = 'solid'), # y軸線特征
axis.line.x = element_line (color = 'black',linetype = 'solid'), # x軸線特征
panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA)) # 圖四周框起來(lái)
5.蜂窩圖
#####蜂窩圖####
library(ggbeeswarm)
ggplot(data,aes(x=Name,y=Value))+
geom_quasirandom(aes(color=Name),shape=19,
position = "jitter",
method = "smiley")+#加抖動(dòng)點(diǎn)
geom_signif(
comparisons = list(c("CTRL","TP1")),test="t.test",
tip_length = 0.02,size = 1,textsize = 4,#增加***尺寸
map_signif_level = T #用*表示顯著性,*---0.05忍抽,**---0.01八孝,***---0.001
)+
scale_fill_lancet()+
theme_cowplot() + theme(legend.position = c(0.85, 0.2))+
theme_classic()+
ggtitle("boxplot")+
labs(title = 'boxplot', # 添加主標(biāo)題
x = 'Name', # x軸的名字
y = 'Value',
subtitle = 'Plot of name by value', # 添加次標(biāo)記
caption = 'Data source: LX')+ #添加腳注
theme(plot.title = element_text(color = 'black', size = 16, hjust = 0.5),
plot.subtitle = element_text(color = 'black', size = 16,hjust = 0.5),
plot.caption = element_text(color = 'black', size = 16,face = 'italic', hjust = 1),
axis.text.x = element_text(color = 'black', size = 16, angle = 0),
axis.text.y = element_text(color = 'black', size = 16, angle = 0),
axis.title.x = element_text(color = 'black', size = 16, angle = 0),
axis.title.y = element_text(color = 'black', size = 16, angle = 90),
legend.title = element_text(color = 'black', size = 16),
legend.text = element_text(color = 'black', size = 16),
axis.line.y = element_line(color = 'black', linetype = 'solid'), # y軸線特征
axis.line.x = element_line (color = 'black',linetype = 'solid'), # x軸線特征
panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA)) # 圖四周框起來(lái)
6.云雨圖
###云雨圖
library(gghalves)
ggplot(data,aes(x=Name,y=Value))+
geom_half_violin(aes(fill=Name),alpha=0.7)+
geom_point(position = position_jitter(width = 0.2))+#加抖動(dòng)點(diǎn)
geom_boxplot(width=0.1)+
scale_fill_lancet()+
theme_cowplot()
##調(diào)節(jié)云
ggplot(data,aes(x=Name,y=Value))+
geom_half_violin(aes(fill=Name),
alpha=0.7,side="r",
adjust=1/2,
position = position_nudge(x=0.2,y=0))+#云往左調(diào)整
geom_point(position = position_jitter(width =0.2),size=1)+#加抖動(dòng)點(diǎn)
geom_boxplot(width=0.1,
position = position_nudge(x=0.2,y=0))+
scale_fill_brewer(palette = "Dark2")+
theme_cowplot()+
coord_flip() + theme(legend.position = c(0.85, 0.3))+###旋轉(zhuǎn)坐標(biāo)軸
scale_fill_lancet()+
theme_cowplot() + theme(legend.position = c(0.85, 0.2))+
theme_classic()+
ggtitle("boxplot")+
labs(title = 'boxplot', # 添加主標(biāo)題
x = 'Name', # x軸的名字
y = 'Value',
subtitle = 'Plot of name by value', # 添加次標(biāo)記
caption = 'Data source: LX')+ #添加腳注
theme(plot.title = element_text(color = 'black', size = 16, hjust = 0.5),
plot.subtitle = element_text(color = 'black', size = 16,hjust = 0.5),
plot.caption = element_text(color = 'black', size = 16,face = 'italic', hjust = 1),
axis.text.x = element_text(color = 'black', size = 16, angle = 0),
axis.text.y = element_text(color = 'black', size = 16, angle = 0),
axis.title.x = element_text(color = 'black', size = 16, angle = 0),
axis.title.y = element_text(color = 'black', size = 16, angle = 90),
legend.title = element_text(color = 'black', size = 16),
legend.text = element_text(color = 'black', size = 16),
axis.line.y = element_line(color = 'black', linetype = 'solid'), # y軸線特征
axis.line.x = element_line (color = 'black',linetype = 'solid'), # x軸線特征
panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA)) # 圖四周框起來(lái)
詳細(xì)數(shù)據(jù)請(qǐng)參考微信公眾號(hào)!