数据下载来源于:
https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/10_OneNumSevCatSubgroupsSevObs.csv
数据下载至本地使用
本次主要使用ggplot2绘制,小伙伴们一起学起来,一起进步
install.packages("tidyr")
library(ggplot2)
library(reshape2)
library(ggpubr)
library(ggsignif)
getwd()
y <- read.table("D:/biolearning/Rcourse/happy.txt", header=T, sep=",")
x <- drop_na(y)
head(x)
x=x[,c(-1,-4,-5,-6)] #删掉不是数值的数据,以及不想展示的数据
head(x)
data=melt(x,id="sex")#sex作为行名进行转置
view(data)
p<-ggplot(data,aes(x=variable,y=value,fill=sex))+
geom_boxplot()+
geom_jitter(shape=16,position = position_jitter(0.2))+
stat_boxplot(geom = "errorbar")+#添加误差线
stat_compare_means(method = "t.test", label="p.signif")+
theme_bw()+ #去掉灰色的背景
theme(panel.grid=element_blank())+
scale_fill_brewer(palette = "Dark2")
p
pdf("phe.pdf",width = 12,height = 8)
ggarrange(p, ncol = 2, nrow = 2)
dev.off()
非常整洁的箱线图就出来了