美文网首页
R: 分组bar, point, box

R: 分组bar, point, box

作者: 胡童远 | 来源:发表于2021-07-14 09:55 被阅读0次

1 输入数据处理

对数据做归一化处理
excel合并低丰度Phlyla
加载所有需要的R包
制作配色表
按最高的Phylum对样本排序

## count 归一化
df = read.table("all_phylum_count.txt", header=T, sep="\t")
sum = apply(df[, 2:ncol(df)], 2, FUN=sum)
result = df
for(i in 1:nrow(df))
{
    for(j in 2:ncol(df))
    {
        result[i, j] = df[i, j]/as.numeric(sum[j-1])
    }
}
write.table(result, file="count_norm.txt", quote=F, row.names=F, sep="\t")

##
library("ggplot2")
library("RColorBrewer")
library("reshape2")
df = read.table("count_input.txt", header=T, sep="\t")

## color
col = read.table("C:/Users/hutongyuan/Desktop/group_color.list", header=F, sep="\t", check.names=F, comment.char="")
colors = col[1:nrow(df),]
names(colors) <- unique(df$Phylum)
#colors = list(phylum = colors)
#pheatmap需要做成list, ggplot不用

## Firmicutes对样本排序
tmp = df[1, 2:ncol(df)]
new_order = order(tmp, decreasing=T)
df = df[, c(1, new_order + 1)]

xlabs配色,按照phylum排序重拍配色

## x配色
group = read.table("group.txt", header=T, sep="\t", row.names=1,
                   comment.char="")
col_text = group[colnames(df[-1]),]
col_text = col_text$color

2 stackplot

input = melt(df, id='Phylum')
input$Phylum = factor(input$Phylum, levels=c(unique(input$Phylum)))
result =
ggplot(input, aes(x=variable, y=value*100, fill=Phylum)) +
  geom_col(position="stack") +
  theme_classic() +
  scale_fill_manual(values = colors) +
  theme(legend.text=element_text(size=15),
        legend.title=element_text(face='bold', size=20)) +
  labs(x="", 
       y="Relative abundance", 
       fill="Phylum") +
  theme(title = element_text(size = 15, face="bold")) +
  scale_y_continuous(expand = c(0, 0)) +
  theme(axis.title = element_text(size = 25),
        axis.text.y = element_text(size = 18),
        axis.line = element_line(size = 1),
        axis.ticks = element_line(size = 1)) +
  theme(axis.text.x = element_text(angle = 60, 
                                 hjust = 1, 
                                 size = 20,
                                 color = col_text))
                                   
ggsave(result, file="phylum_stack.png", width = 10)
ggsave(result, file="phylum_stack.pdf", width = 10)

axis.text.x color参数能用col代替

3 grouped bar

这里仅修改geom_bar的参数即可,
position_dodge > width可拆分成Groued bar,

ggplot(input, aes(x=variable, y=value*100, fill=Phylum)) +
  geom_bar(position = position_dodge(1), 
           width = 0.9,
           stat="identity") +
  theme_classic() +
  scale_fill_manual(values = colors) +
  theme(legend.text=element_text(size=15),
        legend.title=element_text(face='bold', size=20)) +
  labs(x="", 
       y="Relative abundance", 
       fill="Phylum") +
  theme(title = element_text(size = 15, face="bold")) +
  scale_y_continuous(expand = c(0, 0)) +
  theme(axis.title = element_text(size = 25),
        axis.text.y = element_text(size = 18),
        axis.line = element_line(size = 1),
        axis.ticks = element_line(size = 1)) +
  theme(axis.text.x = element_text(angle = 60, 
                                 hjust = 1, 
                                 size = 15,
                                 color = col_text))

4 grouped point

这里重新分组,以phylum为大分组,添加小分组信息,每个小分组包含多个样本,这样就可以实现绘制散点或boxPlot(不再是单一的bar)

group = read.table("group.txt", header=T, sep="\t")
data = merge(input, group, by.x='variable', by.y='id', all.X=T)
ggplot(data, aes(x=Phylum, y=value*100, color=source)) +
  geom_point(position = position_dodge(0.6), 
               width = 0.5,
               size = 1) +
  # size粗细 width宽度 dodge间距
  theme_classic() +
  scale_color_manual(values = c("Colon"="#00BA38",
                               "Stool"="#F8766D",
                               "Ileum"="#619CFF")) +
  theme(legend.text=element_text(size=15),
        legend.title=element_text(face='bold', size=20)) +
  labs(x="", 
       y="Relative abundance", 
       color="Source") +
  theme(title = element_text(size = 15, face="bold")) +
  scale_y_continuous(expand = c(0, 0),
                     limits=c(0, 100)) +
  theme(axis.title = element_text(size = 25),
        axis.text.y = element_text(size = 18),
        axis.text.x = element_text(angle = 60, 
                                   hjust = 1, 
                                   size = 20),
        axis.line = element_line(size = 1),
        axis.ticks = element_line(size = 1))

5 grouped box

类似grouped bar的是,这里
position_dodge(间距宽) > width(柱宽),这么理解似乎没错
相比上面的grouped point,仅仅改了geom_box,ggplot参数的通用性
size 可以调节的是箱体的粗细
outlier.size 忙猜是离群点的大小

pic = 
ggplot(data, aes(x=Phylum, y=value*100, color=source)) +
  geom_boxplot(position = position_dodge(0.6), 
               width = 0.5,
               size = 1) +
  # size粗细 width宽度 dodge间距
  theme_classic() +
  scale_color_manual(values = c("Colon"="#00BA38",
                               "Stool"="#F8766D",
                               "Ileum"="#619CFF")) +
  theme(legend.text=element_text(size=15),
        legend.title=element_text(face='bold', size=20)) +
  labs(x="", 
       y="Relative abundance", 
       color="Source") +
  theme(title = element_text(size = 15, face="bold")) +
  scale_y_continuous(expand = c(0, 0),
                     limits=c(0, 100)) +
  theme(axis.title = element_text(size = 25),
        axis.text.y = element_text(size = 18),
        axis.text.x = element_text(angle = 60, 
                                   hjust = 1, 
                                   size = 20),
        axis.line = element_line(size = 1),
        axis.ticks = element_line(size = 1))

ggsave(pic, file="phylum_box.png", width = 10)
ggsave(pic, file="phylum_box.pdf", width = 10)

参考:
R ggplot2 修改默认颜色
R语言ggplot2绘制分组箱型图和分组柱状图

相关文章

网友评论

      本文标题:R: 分组bar, point, box

      本文链接:https://www.haomeiwen.com/subject/zeaepltx.html