R绘图应用实例:成组的t检验及绘图

本文主要是利用日常实验数据,尝试用R绘制与Graphpad一样的线条图和柱状图。

实验数据:
采用RNAi技术干扰ago1和dcr1两个基因,然后荧光定量检测干扰效果,计算出相应的CT值(参考:qRT-PCR 计算公式2–ΔΔCT (Livak) 方法),并绘图。
计算结果录入Excel,如下:

图片.png

#load the data
library(ggplot2)
result <- read.table("C:/Users/Administrator/Desktop/test.txt", header = T, sep = "\t")
head(result)

# convert data from a wide format to a long format.
library(reshape2)
result_long <- melt(result, id.vars = "Treated", measure.vars= c("AGO1","DCR1"))
View(result_long)

#########################################################################
#alternative packege "tidyr"                                            #
library(tidyr)                                                          #
#Names of new key and value columns, as strings or symbols.             #
(datalong2 <- gather(data, key = "variable",value ="value", A:D ))      #
#########################################################################

#统计学分析
#在进行 t 检验之前,应该对数据进行正态性检验以及方差齐性检验。
# 1st正态性检验
shapiro.test(result$AGO1)
shapiro.test(result$DCR1)

# 2nd方差齐性检验
bartlett.test(result_long$value[1:8]~result_long$Treated[1:8])
bartlett.test(result_long$value[9:16]~result_long$Treated[9:16])

# 3rd t检验,注意两组数据需要分开做t检验
t.test(result_long$value[1:8] ~ result_long$Treated[1:8], paired = T, var.equal = T)
t.test(result_long$value[9:16] ~ result_long$Treated[9:16], paired = T, var.equal = T)

# 4th summarySE 函数提供了标准差、标准误以及95%的置信区间
library(Rmisc)
result_long_count <- summarySE(result_long, measurevar="value", 
                             groupvars= c("Treated","variable"))
result_long_count
  1. 线图绘制
##################################线图###################################
# 线图和直方图,在横纵展示方面,表现的风格有点差别.
p1 <- ggplot(result_long_count, aes(x=Treated, y=value,color= variable, group=variable)) + 
  geom_errorbar(aes(ymin=value-se, ymax=value+se), width=.1) +
  geom_line() +
  geom_point()

# 发现误差棒重叠,我们使用 position_dodge 将它们进行水平移动
pd <- position_dodge(0.5) # move them .05 to the left and right

p2 <- ggplot(result_long_count, aes(x=Treated, y=value, group=variable, colour=variable)) + 
  geom_errorbar(aes(ymin=value-se, ymax=value+se), width=.1, position=pd) +
  geom_line(linetype="dashed", colour="black", position=pd) +
  geom_point(position=pd, size=3)

library(gridExtra)
grid.arrange(p1,p2,nrow=1)

Rplot01.png
  1. 直方图绘制
##################################直方图###################################
# 将Treated转换为因子变量
result_long_count2 <- result_long_count
result_long_count2$Treated <- factor(result_long_count2$Treated)

# 误差棒代表了均值的标准误
p3 <- ggplot(result_long_count2, aes(x=variable, y=value, fill=Treated)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=value-se, ymax=value+se),
                width=.2, position=position_dodge(.9))


#绘制带有显著性标记的条形图,顺序为result_long_count中Treated组的次序
marker <- c("", "", "**", "*")
p4 <- ggplot(result_long_count2, aes(x=variable, y=value, fill=Treated)) + 
  geom_bar(position=position_dodge(.9), stat="identity",
           colour="black",size=.3) +  
  geom_errorbar(aes(ymin=value-se, ymax=value+se),
                size=.3, width=.2, 
                position=position_dodge(.9)) +
  geom_text(aes(y = value +  1.5 * se, label = marker),
            position = position_dodge(0.9), size = 5, fontface = "bold") +
  scale_color_brewer("Set1") +
  xlab("RNAi Treated") +
  ylab("Relative Expression") +
  ggtitle("The Effect of RNAi on two genes")

grid.arrange(p3,p4,nrow=1)
Rplot02.png

你可能感兴趣的:(R绘图应用实例:成组的t检验及绘图)