如何使用两个数据集绘制一个热图

时间:2015-03-28 13:58:23

标签: r ggplot2 heatmap pheatmap

所有现有的答案都不适合我!这些问题正在使热图并排,
to display two heatmaps in same pdf side by side in RSide-by-side plots with ggplot2或其他人

我有两个dfs,我想绘制df1的10个最高行(基于平均值)和10个最低行(基于平均值)How to force ggplot to order x-axis or y axis as we want in the plot? df1取自上一篇文章,df2为as如下:

    df2<- structure(list(phenelzine = c(-0.0665, -0.077, -0.0168, -0.1448, 
0.0571, 0.0032, 0.5728, -0.0322, -0.4072, 0.2572, 0.1139, 0.0462, 
-0.0317, 0.1378, -0.1963, 0.5325, 0.0716, 0.522, -0.027, 0.3913
), denatonium.benzoate = c(0.0386, -0.1908, -0.1477, 0.0331, 
-0.0628, 0.0012, -0.0107, 0.0382, -0.2829, -0.1123, 0.3734, 0.094, 
-0.0433, 0.1303, -0.0427, 0.0232, 0.0366, 0.0858, 0.0066, -0.2011
), triamterene = c(-0.0486, 0.3781, -0.0366, 0.2605, -0.1519, 
0.5596, 0.0218, -0.2791, 0.3106, 0.0682, -0.0829, 0.1277, -0.1318, 
-0.2668, -0.2608, 0.3003, 0.0567, 0.0204, 0.1276, 0.0999), talampicillin = c(0.0064, 
0.0735, 0.0215, -0.275, -0.094, 0.2811, 0, -0.033, -0.0238, 0.3376, 
-0.3585, 0.1125, 0.0626, 0.0964, 0.0984, -0.1358, 0.0655, -0.2214, 
-0.1365, 0.5557), triamcinolone = c(0.0963, 0.2145, 0.1947, -0.0215, 
-0.205, 0.0725, 0.1323, -0.0567, 1.0381, 0.0571, 0.0085, -0.0393, 
0.1973, 0.0257, -0.0153, 0.0088, 0.07, 0.028, 0.2175, 0.1255), 
    sulfaphenazole = c(-0.1976, -0.2614, 0.0198, 0.0015, -0.34, 
    -0.3738, 0.0084, 0.0045, -0.2685, -0.266, 0.0132, 0.0631, 
    -0.0733, 0.1962, -0.2374, 0.303, -0.0107, -0.0321, 0.0904, 
    -0.3727), procyclidine = c(0.002, 0.0836, 0.3422, 0.1251, 
    0.0054, -0.311, -0.1294, -0.1779, 0.0121, -0.1016, -0.1387, 
    -0.0628, 0.1084, -0.1459, 0.1338, -0.5705, -0.013, 0.0074, 
    0.0726, -0.1155), pentoxifylline = c(-0.02, 0.0975, 0.1809, 
    0.0281, -0.1987, -0.1323, -0.0736, 0.0735, 0.1173, 0.0126, 
    -0.0063, -0.1334, -0.0789, -0.0272, 0.3842, 0.3319, -0.0681, 
    0.1229, 0.1486, -0.069), suloctidil = c(0.2032, 0.0321, -0.1741, 
    1.0401, -0.5159, 0.0512, 0.2993, -0.0122, 0.3772, -0.1023, 
    0.6339, -0.1573, 0.5941, 0.0015, 0.3225, -0.1261, -0.1186, 
    0.2664, 0.567, 0.0836), etacrynic.acid = c(0.0519, 0.1094, 
    0.2294, -0.8296, -0.4434, 0.0224, 0.0203, -0.0472, -0.3711, 
    -0.1684, 0, -0.0868, -0.3012, 0.119, 0.1864, 0.2474, -0.0082, 
    -0.0043, 0.2668, 0.0415), diphenylpyraline = c(0.1109, -0.248, 
    -0.0324, -0.0795, 0.0242, 0.1384, 0.0019, -0.0047, -0.112, 
    0.0233, 0.1739, 0.0804, 0.031, -0.1402, 0.0911, -0.0642, 
    0.122, 0.3183, -0.0321, -0.0315), carbenoxolone = c(0.0572, 
    0.0067, -0.0867, 0.0847, -0.0336, 0.1749, -0.0717, 0.0774, 
    -0.1392, -0.125, -0.2171, 0.0383, 0.2226, -0.1613, 0.4069, 
    -0.2584, -0.0611, -0.1726, -0.0979, -0.2289), arecoline = c(0.1024, 
    0.2231, -0.0456, -0.0569, 0.2038, 0.0168, 0.1495, 0.3623, 
    -0.2502, -0.1069, 0, 0.0973, -0.1981, 0.1, -0.3438, 0.395, 
    -0.1338, -0.3616, -0.3076, 0.0234), chenodeoxycholic.acid = c(0.0145, 
    -0.1226, 0.1928, -0.0623, 0.0172, -0.2535, 0.1912, -0.0082, 
    -0.2637, -0.0639, 0.2127, -0.06, 0.0581, 0.1153, 0.2101, 
    0.0434, 0.0259, -0.096, -0.0808, -0.0044), torasemide = c(0.0225, 
    0.1195, 0.1161, 0.0096, 0.2128, 0.2855, -0.1437, -0.2137, 
    -0.0714, 0.1109, 0.0229, 0.1165, 0.0074, 0.1891, -0.0789, 
    0.251, 0.0199, -0.3215, 0.0067, -0.143), troglitazone = c(0.1074, 
    0.1187, 3e-04, 0.1574, 0.3559, 0.0714, -0.1121, -0.1182, 
    0.1418, 0.196, -0.0248, 0.0437, 0.1814, -0.0927, 0.0938, 
    -0.0031, -0.0225, 0.0395, 0.0047, 0.14), mepenzolate.bromide = c(-0.1039, 
    0.0088, 0.2329, 0.0061, 0.0588, -0.0396, -0.1546, 0.1601, 
    -0.1256, 0.0615, -0.0537, -0.0209, 0.1138, -0.0518, 0.2233, 
    -0.0858, 0.0073, -0.1966, -0.0476, 0.0741), megestrol = c(0.0256, 
    0.0967, 0.0533, 0.215, 0.1449, 0.3936, 0.1269, -0.0805, 0.3126, 
    -0.3387, -0.1375, 0.0675, 0.1531, -0.273, 0.2687, 0.0247, 
    0.1113, -0.0559, 0.1653, 0.0195), dexpropranolol = c(0.0659, 
    0.2253, 0.1768, 0.0608, -0.1233, -0.202, -0.1021, -0.0619, 
    -0.2544, 0.1086, -0.2336, 0.0402, 0.018, -0.4825, -0.1842, 
    0.6686, 0, -0.0873, -0.0747, 0.2841), nipecotic.acid = c(0.0025, 
    0.0781, 0.0258, 0.0825, 0.1123, -0.1044, 0.1769, 0.1758, 
    -0.004, 0.0046, -0.2597, 0.0692, -0.0928, -0.089, -0.0945, 
    -0.0356, -0.1181, 0.0165, 0.0888, 0.1253), omeprazole = c(0.0172, 
    0.0859, 0.1016, 0.279, 0.0255, 0.162, 0.0601, -0.0358, 0.1944, 
    -0.1295, -0.1445, 0.0769, -0.0047, -0.0976, 0.1411, -0.0432, 
    0.1151, -0.0375, 0.0071, 0.203), etanidazole = c(-0.0776, 
    -0.0253, 0.1407, 0.2498, -0.0931, 0.0734, -0.0602, -0.1292, 
    0.2937, 0.052, 0.1856, 0.1195, 0.0524, 0.0944, -0.3275, -0.0198, 
    0.0598, -0.0356, 0.0854, 0.2093), acenocoumarol = c(0.1362, 
    0.0303, -0.255, 0.0841, 0.2195, -0.0732, -0.0129, 0.0101, 
    -0.0939, -0.023, 0.0063, 0.0776, 0.0397, -0.1646, -0.5415, 
    -0.0177, -0.0949, 0.1158, -0.0037, 0.1216), dacarbazine = c(0.2137, 
    -0.1382, 0.2363, 0.0733, 0.4311, 0.1098, 0.0351, 0.0831, 
    -0.3778, 0.3351, 0.058, 0.1463, 0.0753, 0.0238, -0.0843, 
    0.445, 0.0869, -0.2161, 0.0553, -0.0549), diphemanil.metilsulfate = c(0.0196, 
    -0.173, 0.0046, 0.0132, -0.0958, -0.0733, 0.0207, 0.0095, 
    -0.3009, -0.1452, 0.0215, 0.1302, 0.0312, -0.0731, 0.1877, 
    0.3668, 0.021, -0.0142, -0.3134, -0.0766), meprylcaine = c(-0.0212, 
    0.0024, 0.0805, -0.1316, 0.0713, -0.1855, 0.2942, 0.0096, 
    -0.1094, 0.0264, 0.0136, -0.0553, -0.0275, 0, 0.0804, 0.0057, 
    0.075, 0.338, -0.1472, -0.0797), mevalolactone = c(0.2585, 
    -0.1235, 0.0609, -0.0379, 0.275, -0.2666, 0.2689, 0.1559, 
    0.0218, 0.0945, -0.0604, 0.0515, -0.0608, 0.1759, -0.2929, 
    0.2396, 0.0191, -0.1368, -0.0224, 0.0181), buspirone = c(-0.0704, 
    -0.1544, -0.0646, 0.0569, -0.1585, 0.4518, -0.252, 0.1089, 
    -0.1774, 0.0844, -0.1922, -0.0342, 0.0871, -0.2897, 0.4065, 
    0.2244, 0.1293, -0.1597, -0.0436, 0.1639), sulfafurazole = c(0.2313, 
    -0.0074, -0.0496, -0.5903, -0.0579, 0.2431, 0.0645, 0.0299, 
    -0.0586, 0.0226, 0.0898, 0.0175, 0.139, 0.0856, -0.1143, 
    0.1475, 0.0073, -0.0925, -0.0804, -0.0076), ciclopirox = c(-0.202, 
    -0.298, 0.2809, -0.7159, -0.7991, 0.0232, -0.0257, -0.0454, 
    0.8951, 0.6343, 0.4207, -0.2504, -0.4547, 0.2399, 0.0774, 
    -0.1596, -0.1781, 0.3225, -0.0511, -0.0057)), .Names = c("phenelzine", 
"denatonium.benzoate", "triamterene", "talampicillin", "triamcinolone", 
"sulfaphenazole", "procyclidine", "pentoxifylline", "suloctidil", 
"etacrynic.acid", "diphenylpyraline", "carbenoxolone", "arecoline", 
"chenodeoxycholic.acid", "torasemide", "troglitazone", "mepenzolate.bromide", 
"megestrol", "dexpropranolol", "nipecotic.acid", "omeprazole", 
"etanidazole", "acenocoumarol", "dacarbazine", "diphemanil.metilsulfate", 
"meprylcaine", "mevalolactone", "buspirone", "sulfafurazole", 
"ciclopirox"), row.names = c("202659_at", "203110_at", "204213_at", 
"204279_at", "204301_at", "205049_s_at", "205242_at", "205267_at", 
"205476_at", "205891_at", "206641_at", "207076_s_at", "209040_s_at", 
"209138_x_at", "209208_at", "209374_s_at", "209397_at", "209569_x_at", 
"209970_x_at", "210885_s_at"), class = "data.frame")

来自df1,在上一篇文章中,我们绘制了高于0.02的热图(简单地说只有10个),来自df2,我想绘制那些低于0.0阈值的图,但是将两个图像放在一个图中< / p>

我如何绘制df1和df2如下

对于df1,可以从上面的链接获取,我可以这样做

t<- data.frame(sort(rowMeans(df)))
rn <- rownames(t[t[,1] > 0.02,, drop=FALSE])
tab <- melt(transform(df, rowname = row.names(df)))
tab1 <-  subset(tab, rowname %in% rownames(t)[t > 0.02])
tab1$rowname <- factor(tab1$rowname, levels=rn)
ggplot(tab1,aes(x = rowname, y = variable, fill = value)) + 
  geom_tile() + 
  scale_fill_gradient2(high="black",mid="white",low="darkblue") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

要绘制df2,我按如下方式绘制

t2<- data.frame(sort(rowMeans(df2)))
    rn2 <- rownames(t2[t2[,1] < 0.0,, drop=FALSE])
    tabm <- melt(transform(df2, rowname = row.names(df2)))
    tab2 <-  subset(tabm, rowname %in% rownames(t2)[t2 < 0.0])
    tab2$rowname <- factor(tab2$rowname, levels=rn2)
    ggplot(tab2,aes(x = rowname, y = variable, fill = value)) + 
      geom_tile() + 
      scale_fill_gradient2(high="black",mid="white",low="darkblue") +
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

我想要的是在一幅图像中绘制两者的热图(一部分来自df1,另一部分来自df2)

1 个答案:

答案 0 :(得分:1)

这是我认为你要求的。我的df是您之前问题的数据框,df2来自此问题。

#calculate mean of each row
df$avg <- apply(df, 1, mean)
df2$avg <- apply(df2, 1, mean)

#add identifier to dataframe to preserve source id after rbind
df$id <- 1
df2$id <- 2

#merge datasets
df3 <- rbind(df, df2)

#add the rownames as a field for preservation after melt
df3$row <- rownames(df3)

#select rows of interest
goodrows <- (df3$id == 1 & df3$avg >= 0.02) | (df3$id == 2 & df3$avg <= 0)

#melt the dataframe retaining only rows of interest
df3m <- melt(df3[goodrows, ], id.vars = c('id', 'avg', 'row'))

#make a new row label that combines "row" and id
df3m$lbl <- paste(df3m$row, "_id_", df3m$id, sep = "")

#plot
ggplot(df3m,aes(x = variable, y = lbl, fill = value)) + 
      geom_tile() + 
      scale_fill_gradient2(high="yellow",mid="gray",low="darkblue") +
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5))