如何在R中绘制Z分数

时间:2017-09-08 06:57:02

标签: r plot

我有一个表格,样本中的分数标准化。我已经计算了z分数,需要创建如下所示的图:

enter image description here

我的表格如下:

Gene_name E_2401_ctrl_1 E_2401_ctrl_2 E_2401_ctrl_3 E_2401_drt_1 E_2401_drt_2 LOC_Os01g01312 1.601736731 0.310548148 1.238589942 -0.899941148 -0.980640435 LOC_Os01g01360 -0.850254449 -0.420792594 0.083358279 0.86759297 0.102030534 LOC_Os01g01390 0.40382069 -0.377555928 -0.009849284 -0.285629267 0.219967368 LOC_Os01g01610 -1.102507436 -0.90329537 -0.458899223 1.042853272 0.904937227 LOC_Os01g01620 -0.806239145 -1.190898502 -0.229250108 0.812535653 1.004865332

我有近3000个基因和50个样本。因此,在Excel中绘图不是一种选择。

1 个答案:

答案 0 :(得分:3)

根据您的数据创建数据框:

df <- data.frame(Gene_name = c("E_2401_ctrl_1", "E_2401_ctrl_2", "E_2401_ctrl_3", "E_2401_drt_1", "E_2401_drt_2"),
              LOC_Os01g01312 = c(1.601736731, 0.310548148, 1.238589942, -0.899941148, -0.980640435),
              LOC_Os01g01360 = c(-0.850254449, -0.420792594, 0.083358279, 0.86759297, 0.102030534),
              LOC_Os01g01390 = c(0.40382069 , -0.377555928, -0.009849284, -0.285629267, 0.219967368),
              LOC_Os01g01610 = c(-1.102507436, -0.90329537, -0.458899223, 1.042853272, 0.904937227),
              LOC_Os01g01620 = c(-0.806239145, -1.190898502, -0.229250108, 0.812535653, 1.004865332))

library(ggplot2)
library(reshape2)

将ggplot重新整形为长格式是一个好主意

df_melt <- reshape2::melt(df, id.vars = "Gene_name")

检查数据现在的样子

head(df_melt, 10)

基因名称在一列中,相应的z分数在另一列中

ggplot(data = df_melt)+
      geom_line(aes(x = variable, y = value, group = Gene_name))+
      theme(axis.text.x = element_text(angle = 45, hjust = 1))+
      xlab("gene")+
      ylab("")

这是怎么读的:

ggplot(data = df_melt)

指定绘制图的数据

geom_line(aes(x = variable, y = value, group = Gene_name))

geom_line,因为您需要连接值的行。在ggplot中,所有变量都在aes()中。

theme(axis.text.x = element_text(angle = 45, hjust = 1))+
  xlab("")+
  ylab("z-score")

其余的只是化妆

如果你想要分面图,添加一个你将要面对的变量

df_melt <- data.frame(rbind(df_melt, df_melt), 
    letters=rep(c("A", "B"), each = nrow(df_melt)))

这里我只重复两次数据帧,

rbind(df_melt, df_melt)

并使用&#34; A&#34;标记来自第一个的行。第二个是&#34; B&#34;。

letters=rep(c("A", "B"), each = nrow(df_melt))
df_melt

现在你可以通过&#34;字母&#34;变量

ggplot(data = df_melt)+
  geom_line(aes(x = variable, y = value, group = Gene_name))+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))+
  xlab("gene")+
  ylab("z-score")+
  facet_wrap(~letters, ncol = 1)

编辑:可以通过将color = variable参数添加到要着色的geom内的aes()调用来着色群集标签。我将从头开始:

df <- data.frame(Gene_name = c("E_2401_ctrl_1", "E_2401_ctrl_2", "E_2401_ctrl_3", "E_2401_drt_1", "E_2401_drt_2"),
                 LOC_Os01g01312 = c(1.601736731, 0.310548148, 1.238589942, -0.899941148, -0.980640435),
                 LOC_Os01g01360 = c(-0.850254449, -0.420792594, 0.083358279, 0.86759297, 0.102030534),
                 LOC_Os01g01390 = c(0.40382069 , -0.377555928, -0.009849284, -0.285629267, 0.219967368),
                 LOC_Os01g01610 = c(-1.102507436, -0.90329537, -0.458899223, 1.042853272, 0.904937227),
                 LOC_Os01g01620 = c(-0.806239145, -1.190898502, -0.229250108, 0.812535653, 1.004865332))

df_melt <- reshape2::melt(df, id.vars = "Gene_name")

#the ifelse() part makes another column called "lett" where if it is a "crtl" gene will be "A" and "B" if not

df_melt <- data.frame(rbind(df_melt, df_melt), 
                      lett = ifelse(grepl("ctrl", df_melt$Gene_name), "A", "B"))


ggplot(data = df_melt)+
  geom_line(aes(x = variable, y = value,group = Gene_name, color=lett))+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))+
  xlab("gene")+
  ylab("z-score")+
  scale_color_manual(values=c("A" = "red", "B" = "blue"))

更多关于控制颜色的信息:

http://ggplot2.tidyverse.org/reference/scale_manual.html http://ggplot2.tidyverse.org/reference/scale_brewer.html