ggplot2绘制柱形图(1)

在进行科研数据绘图时,柱形图是非常常见的一种图形,这里我使用柱形图来展示细胞流式的结果。

这是流式的部分结果:UR代表右上象限,LR代表右下象限

先加载要用的R包:

>library(reshape2)

>library(ggplot2)

>library(ggsignif)


读入数据并整理数据:

>mito48 <- read.csv(file = "clipboard",header = T,sep = "\t")  #读入数据

>colnames(mito48) <- c("Well","UR","LR")

>mito48$apop <- mito48$UR + mito48$LR

>mito48 <- mito48[,c(1,4)]

>head(mito48)

>mito48$dose <- rep(c("0","25","50","100","200","500","negtive"),3)

>mito48$dose <- as.factor(mito48$dose)

>mito48$Well <- c(rep(c("A","B","C"),each = 7))

>mito48$Well <- as.factor(mito48$Well)

>head(mito48)

sig <- mito48  # 把这个长数据的mito48赋值给一个新的数据框,因为这个格式在后面画图的时候会用到

准备画图所需要的格式:

>mito48 <- acast(mito48,dose~Well,value.var = "apop")   #先把长数据换成宽数据(reshape2包)

>mito48

>mean <- apply(mito48,1,mean)   #计算均数

>sd <- apply(mito48,1,sd)   #计算标准差

>mito48 <- cbind(mito48,cbind(mean,sd))   #合并

>mito48 <- as.data.frame(mito48)

mito48$dose <- c("control","25","50","100","200","500","negtive")

mito48$order <- c(1:7)

mito48$min <- mito48$mean - mito48$sd     #95%下限,用于制作柱形图的误差棒

mito48$max <- mito48$mean + mito48$sd

>mito48   #作图需要这么多列的内容

开始画图:

comparison <- list(c("0","25"),c("0","50"),c("0","100"),c("0","200"),c("0",500),c("0","negtive"))

#先设定要比较的两组,这里我用所有组和对照组(浓度为0)进行比较

ggplot(mito48,mapping = aes(x = reorder(dose,order),mean,fill = dose))+

  geom_bar(stat = "identity",color = "black",size = 1.5,alpha = 0.8,width = 0.6)+

  coord_cartesian(expand = F,xlim = c(0.3,7.7),ylim = c(0,75))+

  geom_errorbar(aes(ymin = min,ymax = max),width = 0.2,size = 1.5)+    #添加误差棒

  labs(x = "dose",y = "Double staining rate(%)(%)",title = expression(' '~mito-Ca^"2+"~'(24h)'))+      #~~之间表示要表达的公式

  geom_text(mapping = aes(x = 7,y = max[7] + 1.5,size = 10,label = "*"),size = 10)+

  geom_signif(data = sig,mapping = aes(dose,apop),stat = "signif",test = "t.test",comparisons = comparison,step_increase = 0.1,y_position = c(43,48,53,58,63,68),size = 1.52)+

  theme(panel.background = element_blank(),

        axis.line = element_line(colour = "black",size = rel(2)),

        axis.title = element_text(size = rel(1.2)),

        axis.text = element_text(size = rel(1.2)),

        axis.text.y = element_text(hjust = 1),

        axis.ticks = element_line(size = rel(1.5)),

        legend.position = "none",

        plot.title = element_text(size = rel(2),hjust = 0.5))

最终图片:

你可能感兴趣的:(ggplot2绘制柱形图(1))