一如既往的碎碎念:
相处了三个月的小伙伴走了,留下我们,又是一顿唏嘘;
成年后的相识都是这样,一转身就是一辈子,几乎是不会再见了;
前段时间看过这样的一句话:好像一个不怕颗粒无收的少年,共勉;
boxplot概念
-
利用数据中的五个统计量:最小值(Q1-1.5(IQR))、第一四分位数( Q1)、中位数(Q2)、第三四分位数(Q3)与最大值(Q3+1.5(IQR))来描述数据的一种方法。
-
IQR即Q3-Q1;
-
超出maximum(inner fence)和minimum(outer fence)的即outlier;
barplot-method1
#####这里加载了ggpubr,加载了ggplot2,以及我想用的stat_compare_means;
library(ggpubr)
data("ToothGrowth")
test1 <- ToothGrowth
head(test1)
#ggplot2
p1 <- ggplot(test1,aes(x = supp, y = len,color = supp )) + ylim(0,45)+
geom_boxplot()
my_comparisons <- list(c('OJ','VC'))
p1
p1+stat_compare_means(comparisons = my_comparisons,method = 't.test')
参数功能说明:
- comparison设置比较的对象,需要用list格式;
-
stat_compare_means可以设置检验的方式;
method1
barplot-method2
####ToothGrowth是内置数据集
data("ToothGrowth")
df <- ToothGrowth
my_comparisons <- list(c('0.5','1'),c('0.5','2'),c('2','1'))
p <- ggboxplot(df, x = "dose", y = "len",
add=c('median_iqr'),
add.params = list(size=0.5),
fill = "dose",
bxp.errorbar = T)+
stat_compare_means(comparisons = my_comparisons,label.y=c(35,45,50))
###通过ggpar可以对上面的结果p进行调整
ggpar(p,
ylim=c(0,60),
xlab='dose',
ylab='len',
legend = "right",
legend.title = "Dose (mg)")
参数功能说明:
- 依据自己的需要,选择add中的mean_se、median_iqr等;
- bxp.errorbar(T/F)设置是否展示箱线图的误差线;
-
通过ggpar可以对ggboxplot的结果进行调整;
method2
barplot-part1 数据准备
library(ggplot2)
data("ToothGrowth")
test1 <- ToothGrowth
head(test1)
library(plyr)
tgc <- ddply(test1, .(supp, dose), summarize,
mean = round(mean(len), 6),
sd = round(sd(len), 6),
se = round(sd(len)/sqrt(length(len)),6))
p_value<- ddply(test1,.(dose),summarize,
p= round(t.test(len~supp)[[3]],4))
参数功能说明:
ddply(.data, .variables, .fun = NULL, ..., .progress = "none",
.inform = FALSE, .drop = TRUE, .parallel = FALSE, .paropts = NULL)
- ddply(依据.variables)对数据框进行分组,将.fun作用于各个分组,并返回作用结果;
- summarize是一个对结果总结的函数,可返回一个数据框的结果;
- errorbar可采用sd(标准差)、se(标准误/标准误差)、置信区间的计算结果;
barplot-part2 绘图
p<- ggplot(tgc, aes(x=dose, y=mean, fill=supp)) +
geom_bar(position=position_dodge(), stat="identity") +
geom_errorbar(aes(ymin=mean-se, ymax=mean+se),
width=.2,
position=position_dodge(.4))+xlab('dose')+ylab('len')
p+ annotate("text", x = 0.5, y = 18, label = p_value$p[1]) +
annotate("text", x = 1.0, y = 27, label = p_value$p[2]) +
annotate("text", x = 2.0, y = 30, label = "ns")+
geom_segment(aes(x=0.4, y=16, xend=0.6, yend=16))+
geom_segment(aes(x=0.4, y=15, xend=0.4, yend=16))+
geom_segment(aes(x=0.6, y=15, xend=0.6, yend=16))
参数功能说明:
- annotate进行p值的书写;
- geom_segment进行相比较的组的连接(通过在x、y轴方向画线达到目的),这里只对左边第一个组进行了设置;
barplot
【参考内容】
1.https://www.shixiangwang.top/post/ggpubr-add-pvalue-and-siglevels/
2.http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/
3.https://ggplot2.tidyverse.org/reference/geom_segment.html
4.https://www.cnblogs.com/nxld/p/6059603.html
5.http://www.omicsclass.com/article/474











网友评论