how can I plot one heatmap using two data sets - r

all existed answers are not working for me! these questions are making the heat maps side by side,
to display two heatmaps in same pdf side by side in R or Side-by-side plots with ggplot2 or others
I have two dfs which I want to plot the 10 highest rows (based on the average) of df1 and 10 lowest rows (based on average) How to force ggplot to order x-axis or y axis as we want in the plot? df1 is taken from the previous post, and df2 is as follows.
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")
from df1, in previous post, we plot the heatmap of those higher than 0.02 (lets say only 10 of them), from df2, I want to plot those that lower than 0.0 threshold but put both images together in one figure
what I do to plot the df1 and df2 is as follows
for df1 that can be taken from link above , I can do as follows
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))
To plot the df2 I do as follows
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))
what I want is to plot the heatmap of both in one image (one part from df1 and the other part from df2)

Here is what I think you are asking for. My df is your dataframe from the earlier question and df2 is from this question.
#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))

Related

xlim geom_histogram Error: Aesthetics must be either length 1 or the same as the data

I am trying to plot a histogram with a custom colour palette. The problem arises when I set the xlim of the histogram.
Please see below the reproducible example:
# sample dataframe
test_dt <- structure(list(col_1 = c(0.057, -0.063, -0.319, 0.02, 0.079,
0.007, -0.105, -0.084, 0.019, 0.28, -0.064, -0.243, -0.116, 0.079,
0.07, -0.187, -0.725, 0.134, 0.062, -0.056, -0.074, 0.392, -0.014,
-0.062, 0.214, 0.371, 0.069, -0.03, 0.036, -0.175, 0.097, 0.358,
0.153, -0.092, -0.038, -0.051, 0.017, -0.108, 0.133, 0.105, 0.187,
-0.056, -0.316, 0.15, -0.142, 0.076, 0.242, -0.069, 0.155, 0.214,
0.162, -0.037, -0.109, 0.111, -0.077, -0.435, 0.003, 0.187, 0.134,
0.027, 0.107, 0.175, -0.355, -0.572, 0.038, -0.209, -0.263, -0.147,
-0.23, -0.174, 0.203, -0.118, 0.008, -0.268, -0.001, 0.227, -0.019,
0.08, 0.044, -0.065, -0.131, 0.093, 0.127, -0.131, 0.039, 0.045,
0.032, 0.343, 0.053, -0.033, 0.453, 0.07, -0.225, 0.094, 0.002,
-0.119, 0.014, -0.125, 0.003, -0.48)), row.names = c(NA, -100L
), class = "data.frame")
# colour palette
RBW <- colorRampPalette(c("darkred","white","darkblue"))
# plot histogram without xlim
ggplot(test_dt) +
geom_histogram(aes(x=col_1),
position = "identity",
bins = 60,
color = "grey10",
fill = RBW(60))
When I run the following lines is when I get the error:
Aesthetics must be either length 1 or the same as the data
# plot histogram with xlim
ggplot(test_dt) +
geom_histogram(aes(x=col_1),
position = "identity",
bins = 60,
color = "grey10",
fill = RBW(60)) +
xlim(-2,2)
instead of xlim, add + coord_cartesian(xlim = c(-2,2))
library(ggplot2)
``` r
ggplot(test_dt) +
geom_histogram(aes(x=col_1),
position = "identity",
bins = 60,
color = "grey10",
fill = RBW(60)) +
coord_cartesian(xlim = c(-2,2))
Created on 2020-02-11 by the reprex package (v0.3.0)

Averaging the replicate data in omics / biostatistics

I have a dataframe for gene expression data. Samples are named as Genotype_Time_Replicate (e.g. AOX_1h_4).
E.g. data set
df <- structure(list(ID = c("AT5G54740.1", "AT5G55730.2", "AT5G57655.2", "AT5G64100.1", "AT5G64260.1", "AT5G67360.1", "AT1G30630.1", "AT1G62380.1", "AT1G70830.1", "AT3G14990.1", "AT4G18800.1", "AT4G24510.1", "AT5G15650.1", "AT5G19820.1", "AT5G59840.1", "AT5G47200.1", "AT1G12840.1", "AT1G76030.1", "AT1G78900.2", "AT3G42050.1", "AT4G11150.1", "AT1G11860.2", "AT1G17290.1" ),
Location = c("extracellular", "extracellular", "extracellular", "extracellular", "extracellular", "extracellular", "golgi", "golgi", "golgi", "golgi", "golgi", "golgi", "golgi", "golgi", "golgi", "ER", "ER", "ER", "mitochondrion", "mitochondrion", "mitochondrion", "mitochondrion", "mitochondrion"),
AOX_1h_1 = c(0.844651873, 0.50954096, 1.12e-08, 0.012981372, 0.978148381, 0.027579578, 0.068010151, 0.410629215, 0.253838635, 0.033631788, 0.335713512, 0.982799013, 0.025910457, 0.793810264, 0.762431665, 0.152154436, 0.027114103, 0.000227, 1.07e-05, 0.721209032, 0.086281162, 0.483130711, 0.014795515),
AOX_1h_2 = c(0.894623378, 0.011521413, 1.62e-06, 0.085249729, 0.02863972, 0.956962154, 0.225208718, 0.932679767, 0.002574192, 0.071700671, 0.233682544, 0.936572874, 1.12e-05, 0.241658735, 0.865205515, 0.000537, 0.103471292, 8.66e-07, 1.22e-08, 0.950878446, 0.145012176, 0.092919172, 0.599713247),
AOX_1h_3 = c(0.880951025, 0.00145276, 8.59e-10, 0.087023475, 0.675527672, 0.765543306, 0.305860948, 0.899172011, 0.020973476, 0.542988545, 0.735571562, 0.157569324, 0.025488075, 0.071006507, 0.262324019, 0.080470612, 0.0436526, 6.65e-09, 5.63e-10, 0.020557091, 0.069577215, 0.005502212, 0.852099232),
AOX_1h_4 = c(0.980823252, 0.158123518, 0.00210702, 0.006317657, 0.30496173, 0.489709702, 0.091469807, 0.958443361, 0.015583593, 0.566165972, 0.66746161, 0.935102341, 0.087733288, 0.744313619, 0.021169383, 0.633250945, 0.257489406, 0.024345088, 0.000355, 0.226279179, 0.004038493, 0.479275204, 0.703522761),
AOX_2h_1 = c(0.006474022, 0.246530998, 5.38e-06, 0.47169153, 0.305973663, 0.466202566, 0.191733645, 0.016121487, 0.234839116, 0.043866023, 0.089819656, 0.107934599, 2.09e-06, 0.413229678, 0.464078018, 0.004118766, 0.774970986, 3.79e-07, 2.3e-10, 0.428591262, 0.002326292, 0.385580707, 0.106216066),
AOX_2h_2 = c(0.166169729, 0.005721199, 7.77e-08, 0.099146712, 0.457164663, 0.481987525, 7.4e-05, 0.969805081, 0.100894997, 0.062103337, 0.095718425, 0.001686206, 0.009710516, 0.134651787, 0.887036569, 0.459218152, 0.074576369, 3.88e-09, 3.31e-15, 0.409645805, 0.064874307, 0.346371524, 0.449444779),
AOX_2h_3 = c(1.06e-05, 0.576589898, 4.03e-08, 0.787468189, 0.971119601, 0.432593753, 0.000274, 0.86932399, 0.08657663, 4.22e-06, 0.071190008, 0.697384316, 0.161623604, 0.422628778, 0.299545652, 0.767867006, 0.00295567, 0.078724176, 4.33e-09, 0.988576028, 0.080278831, 0.66505527, 0.014158693),
AOX_2h_4 = c(0.010356719, 0.026506539, 9.48e-09, 0.91009296, 0.302464488, 0.894377768, 0.742233323, 0.75032613, 0.175841127, 0.000721, 0.356904918, 0.461234653, 1.08e-05, 0.65800831, 0.360085919, 0.004814238, 0.174670947, 0.004246734, 7.31e-11, 0.778725214, 0.051334623, 0.10212841, 0.155831664 ),
AOX_6h_1 = c(0.271681878, 0.004822226, 1.87e-11, 0.616969208, 0.158860224, 0.684690326, 0.011798791, 0.564591916, 0.000314, 4.79e-06, 0.299871385, 0.001909713, 0.00682428, 0.039107415, 0.574143284, 0.061532691, 0.050483892, 2.28e-08, 1.92e-12, 0.058747794, 0.027147473, 0.196608218, 0.513693112),
AOX_6h_2 = c(5.72e-12, 0.719814288, 0.140016259, 0.927094438, 0.841229414, 0.224510089, 0.026567282, 0.242981965, 0.459311076, 0.038295888, 0.127935565, 0.453746728, 0.005023732, 0.554532387, 0.280899096, 0.336458018, 0.002024021, 0.793915731, 0.012838565, 0.873716549, 0.10097853, 0.237426815, 0.003711539),
AOX_6h_3 = c(3.16e-12, 0.780424491, 0.031315419, 0.363891436, 0.09562579, 0.104833988, 3.52e-05, 0.104196756, 0.870952423, 0.002036134, 0.016480622, 0.671475063, 2.3e-05, 0.00256744, 0.66263641, 0.005026601, 0.57280276, 0.058724117, 6.4e-10, 0.030965264, 0.005301006, 0.622027012, 0.371659724),
AOX_6h_4 = c(7.99e-10, 0.290847169, 0.001319424, 0.347344795, 0.743846306, 0.470908425, 0.00033, 0.016149973, 0.080036584, 0.020899676, 0.00723071, 0.187288769, 0.042514886, 0.00150443, 0.059344154, 0.06554177, 0.112601764, 0.000379, 2.36e-10, 0.78131093, 0.105861995, 0.174370801, 0.05570041 ),
WT_1h_1 = c(0.857, 0.809, 2.31e-05, 0.286, 0.87, 0.396, 0.539, 0.787, 0.73, 0.427, 0.764, 0.87, 0.386, 0.852, 0.848, 0.661, 0.393, 0.0415, 0.00611, 0.843, 0.576, 0.804, 0.304 ),
WT_1h_2 = c(0.898, 0.509, 0.0192, 0.729, 0.616, 0.902, 0.811, 0.9, 0.343, 0.712, 0.814, 0.901, 0.0446, 0.816, 0.896, 0.217, 0.747, 0.0143, 0.000964, 0.901, 0.776, 0.737, 0.876 ),
WT_1h_3 = c(0.939, 0.627, 0.0104, 0.867, 0.932, 0.935, 0.91, 0.939, 0.803, 0.926, 0.934, 0.888, 0.813, 0.859, 0.905, 0.864, 0.838, 0.0223, 0.00917, 0.802, 0.858, 0.724, 0.938 ),
WT_1h_4 = c(0.911, 0.782, 0.298, 0.396, 0.837, 0.871, 0.727, 0.91, 0.506, 0.88, 0.89, 0.909, 0.723, 0.896, 0.547, 0.887, 0.824, 0.566, 0.175, 0.814, 0.348, 0.869, 0.893),
WT_2h_1 = c(0.748, 0.911, 0.231, 0.929, 0.917, 0.928, 0.903, 0.801, 0.909, 0.849, 0.878, 0.884, 0.183, 0.925, 0.928, 0.719, 0.941, 0.108, 0.00817, 0.926, 0.678, 0.923, 0.884),
WT_2h_2 = c(0.935, 0.851, 0.163, 0.925, 0.951, 0.952, 0.63, 0.963, 0.926, 0.916, 0.925, 0.804, 0.868, 0.931, 0.961, 0.951, 0.92, 0.0706, 0.000265, 0.95, 0.917, 0.947, 0.951),
WT_2h_3 = c(0.0197, 0.894, 0.000613, 0.911, 0.922, 0.877, 0.122, 0.916, 0.739, 0.0125, 0.718, 0.905, 0.801, 0.875, 0.852, 0.91, 0.302, 0.729, 0.00015, 0.923, 0.731, 0.902, 0.504),
WT_2h_4 = c(0.696, 0.765, 0.0142, 0.931, 0.893, 0.931, 0.925, 0.925, 0.87, 0.45, 0.899, 0.908, 0.144, 0.921, 0.899, 0.631, 0.87, 0.62, 0.0014, 0.926, 0.807, 0.844, 0.865),
WT_6h_1 = c(0.898, 0.727, 0.00395, 0.921, 0.881, 0.924, 0.776, 0.919, 0.542, 0.234, 0.901, 0.67, 0.747, 0.83, 0.919, 0.848, 0.841, 0.056, 0.00144, 0.846, 0.815, 0.888, 0.916),
WT_6h_2 = c(2.38e-09, 0.88, 0.708, 0.898, 0.891, 0.768, 0.443, 0.777, 0.843, 0.505, 0.695, 0.842, 0.208, 0.859, 0.794, 0.813, 0.14, 0.887, 0.326, 0.894, 0.661, 0.775, 0.182),
WT_6h_3 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
WT_6h_4 = c(0.0357, 0.953, 0.792, 0.956, 0.967, 0.96, 0.711, 0.892, 0.931, 0.899, 0.866, 0.946, 0.917, 0.799, 0.925, 0.927, 0.938, 0.72, 0.025, 0.967, 0.936, 0.945, 0.923)),
class = "data.frame", row.names = c(NA, -23L))
I want to summarize data for each organelle (averaged by organelle and samples' replicates) and plot the Wildtype and mutant data side by side with standard error for each time point
df <-
melted <- melt(df)
head(melted)
melted$variable<- str_replace_all(melted$variable, '_[0-9]$', '')
melted$variable <- factor(melted$variable,levels=c("WT_1h","AOX_1h","WT_2h","AOX_2h","WT_6h","AOX_6h"))
my_comparisons <- list( c("WT_1h","AOX_1h"), c("WT_2h","AOX_2h"),c("WT_6h","AOX_6h"))
ggbarplot(melted, x = "variable", y = "value", add = "mean_se",
color = "variable", palette = c("grey","black","grey","black","grey","black"),
facet.by = "Location")+
stat_compare_means(comparisons = my_comparisons, label = "p.signif")
How can I use tidyverse (dplyr / tidyr) for this purpose?
How can I use tidyverse (dplyr / tidyr) to follow this pathway instead of above scripts?
You can use different functions to normalise this data. I use gather() in this example alongside stringr functions to extract the data from the character vector that has 3 columns of data in it.
dat %>%
gather(key, value, -ID, -Location) %>%
mutate(type = map_chr(str_split(key,"_"),~.x[1]),
hour = map_chr(str_split(key,"_"),~.x[2]),
n = map_chr(str_split(key,"_"),~.x[3])) %>%
group_by(type, hour) %>%
summarise(mean = mean(value))
Gives
# A tibble: 6 x 3
# Groups: type [?]
type hour mean
<chr> <chr> <dbl>
1 AOX 1h 0.3235302
2 AOX 2h 0.2709910
3 AOX 6h 0.2226648
4 WT 1h 0.6633866
5 WT 2h 0.7263108
6 WT 6h 0.7915662
This you can use in ggplot() to make a nice barplot.
To get it in a table you can use
dat %>%
gather(key, value, -ID, -Location) %>%
mutate(type = map_chr(str_split(key,"_"),~.x[1]),
hour = map_chr(str_split(key,"_"),~.x[2]),
n = map_chr(str_split(key,"_"),~.x[3])) %>%
group_by(type, hour) %>%
summarise(mean = mean(value)) %>%
spread(type, mean)
to get
# A tibble: 3 x 3
hour AOX WT
* <chr> <dbl> <dbl>
1 1h 0.3235302 0.6633866
2 2h 0.2709910 0.7263108
3 6h 0.2226648 0.7915662
Another version going from the df object:
The df object is a list, and expression values after cbind are character type, so you can do
tb <- as_tibble(do.call(cbind, df)) %>%
mutate_at(3:14, as.numeric)
NB that usually for gene expression data it is easier to read in count data using read_tsv or read.table and combine into matrix, data.frame or tibble.
NBB the df object specified has no "WT" samples (from my copy/paste anyway) so I renamed last 4 samples in tb as "WT_1h" replicates
colnames(tb)[11:14] <- paste0("WT_1h_",c(1:4))
Create means from replicates by function
rowMeanNrep <- function(tb, nm){
varname <- paste0(nm, "_mean")
selectn <- grep(nm, colnames(tb))
tb %>%
dplyr::mutate(!!varname := rowMeans(dplyr::select(., !!selectn)))
}
Specify which timepoints to use, and apply
tps <- c("AOX_1h", "WT_1h")
tb_1h_mean <- cbind(tb_1h[,1:2],
do.call(cbind, lapply(tps, function(f){
rowMeanNrep(tb=tb, nm=f) %>%
dplyr::select(paste0(f, "_mean"))
}))
)
A final NB, think about using boxplots instead of barplots, see this paper

outlining statistically significant pixels in plotly (heatmap)

In plotly's heatmap, is it possible to outline specific pixels? In the small reproducible example below, I'd like to know if I can use the matrix defined at the end, called "significant," to add a black outline (with lwd=2) to all pixels with a corresponding value of 1 in 'significant':
library(plotly)
library(RColorBrewer)
m1 <- matrix(c(
-0.0024, -0.0031, -0.0021, -0.0034, -0.0060, -1.00e-02, -8.47e-03, -0.0117, -0.0075, -0.0043, -0.0026, -0.0021,
-0.0015, -0.0076, -0.0032, -0.0105, -0.0107, -2.73e-02, -3.37e-02, -0.0282, -0.0149, -0.0070, -0.0046, -0.0039,
-0.0121, -0.0155, -0.0203, -0.0290, -0.0330, -3.19e-02, -1.74e-02, -0.0103, -0.0084, -0.0180, -0.0162, -0.0136,
-0.0073, -0.0053, -0.0050, -0.0058, -0.0060, -4.38e-03, -2.21e-03, -0.0012, -0.0026, -0.0026, -0.0034, -0.0073,
-0.0027, -0.0031, -0.0054, -0.0069, -0.0071, -6.28e-03, -2.88e-03, -0.0014, -0.0031, -0.0037, -0.0030, -0.0027,
-0.0261, -0.0223, -0.0216, -0.0293, -0.0327, -3.17e-02, -1.77e-02, -0.0084, -0.0059, -0.0060, -0.0120, -0.0157,
0.0045, 0.0006, -0.0031, -0.0058, -0.0093, -9.20e-03, -6.76e-03, -0.0033, 0.0002, 0.0045, 0.0080, 0.0084,
-0.0021, -0.0018, -0.0020, -0.0046, -0.0080, -2.73e-03, 7.43e-04, 0.0004, -0.0010, -0.0017, -0.0022, -0.0024,
-0.0345, -0.0294, -0.0212, -0.0194, -0.0192, -2.25e-02, -2.05e-02, -0.0163, -0.0179, -0.0213, -0.0275, -0.0304,
-0.0034, -0.0038, -0.0040, -0.0045, -0.0059, -1.89e-03, 6.99e-05, -0.0050, -0.0114, -0.0112, -0.0087, -0.0064,
-0.0051, -0.0061, -0.0052, -0.0035, 0.0012, -7.41e-06, -3.43e-03, -0.0055, -0.0020, 0.0016, -0.0024, -0.0069,
-0.0061, -0.0068, -0.0089, -0.0107, -0.0104, -7.65e-03, 2.43e-03, 0.0008, -0.0006, -0.0014, -0.0021, -0.0057,
0.0381, 0.0149, -0.0074, -0.0302, -0.0550, -6.40e-02, -5.28e-02, -0.0326, -0.0114, 0.0121, 0.0367, 0.0501,
-0.0075, -0.0096, -0.0123, -0.0200, -0.0288, -2.65e-02, -2.08e-02, -0.0176, -0.0146, -0.0067, -0.0038, -0.0029,
-0.0154, -0.0162, -0.0252, -0.0299, -0.0350, -3.40e-02, -2.51e-02, -0.0172, -0.0139, -0.0091, -0.0119, -0.0156),
nrow = 15, ncol = 12, byrow=TRUE)
# palette definition
palette <- colorRampPalette(c("darkblue", "blue", "white", "red", "darkred"))
# find max stretch value
zmax1 = max(abs(m1))
plot_ly(
x = c(format(seq(as.Date('2000-10-01'), as.Date('2001-09-30'), by='month'), "%b")),
y = rev(c("TH1", "IN1", "IN3", "GL1", "LH1", "ED9", "TC1", "TC2", "TC3", "UT1", "UT3", "UT5", "GC1", "BC1", "WC1")), ,
z = m1, colors = palette(50), type = "heatmap", width = 700, height = 600, zauto = FALSE, zmin = -1 * zmax1, zmax = zmax1
)
# Can this matrix somehow be used to outline pixels in the heatmap that are equal to one?
# (In this example, the outline pattern will look like a checker board)
significant <- matrix(rep(c(0,1), times=nrow(m1) * ncol(m1) / 2), nrow = 15, ncol = 12)

How to force ggplot to order x-axis or y axis as we want in the plot?

I have a data frame with the first column is like this
df<- structure(list(phenelzine = c(-0.0269, 0.0217, 0.4609, -0.0011,
0.0016, -0.0334, 0.5159, 0.4687, -0.5715, -0.1466, 0.1096, 0.1006,
-0.1021, 0.0093, -0.2616, 0.1496, 0.4463, -0.0833, 0.0573, 0.0602,
0.1345, -0.0911, -0.1444, -0.0624, -0.2009, -0.0579, 0.1701,
-0.161, -0.376, 0.1239, -0.1529, -0.1217, 0.2659, 0.1975, 0.0598,
-0.1397, 0.1959, -0.0342, -0.0771, 0.0017, 0.0562, 0.0605, -0.0976,
0.3257, -0.0385, -0.0687, 0.3087, 0.0992, -0.2253, -0.3171, -0.055,
0.2071, -0.1928, -0.113, -0.2142), denatonium.benzoate = c(-0.1734,
-0.2142, 0.2142, -0.1376, -0.0129, 0.0254, 0.06, 0.1768, 0.2295,
0.1772, 0.1978, -0.0556, 0.0971, 0.0979, 0.2073, 0.061, 0.2782,
-0.048, -0.0825, 0.1922, -0.0395, 0.1594, -0.2117, -0.0999, 5e-04,
-0.0595, -0.2083, 0.1004, -0.1279, -0.0613, -0.0576, -0.0567,
0.1006, -0.0213, -0.1109, 0.0313, 0.0641, -0.2919, -0.0119, 0.1644,
-0.1197, -0.1138, 0.078, -0.2729, -0.022, -0.1864, -0.0709, 0.0211,
0.0183, 0.0175, 0.1655, 0.0064, -0.0701, 0.0274, 0.1163), triamterene = c(-0.0253,
0.2896, 0.1606, -0.086, -0.1886, 0.0355, 0.0552, 0.0733, 0.0131,
0.6887, -0.0161, -0.3962, 0.1294, 0.0316, -0.1851, -0.1193, 0.0308,
-0.1663, 0.0417, -0.0568, 0.1047, 0.0162, 0.3497, 0.0708, -0.0317,
-0.1471, -0.1588, -0.4929, 0.078, 0.1263, -0.2024, -0.0021, -0.3611,
-0.0494, -0.4211, -0.0226, 0.5045, -0.0757, 0.0328, -0.0198,
-0.2622, 0.2297, -0.0454, 0.4094, 0.0826, -0.4326, 0.0316, 0.3048,
-0.2047, 0.1124, -0.2369, 0.1803, -0.1735, 0.2399, -0.0509),
talampicillin = c(-0.1761, -0.0355, 0.0295, 0.1979, 0.0545,
0.0673, -0.0797, -0.1566, 0.0496, 0.2623, -0.197, 0.0192,
-0.2673, 0.0687, -0.0058, -0.4072, -0.06, -0.2315, 0.2532,
-0.0395, 0.0911, -0.1348, -0.018, 0.3084, 0.3751, 0.1659,
-0.1882, 0.1874, -0.0979, -0.0829, -0.1693, 0.0179, -0.0264,
0.1013, 0.4272, 0.1253, -0.0733, 0, 0.1552, -0.3395, -0.0738,
0, 0, -0.1746, -0.2039, 0.2907, 0.0363, 0.2168, -0.1428,
-0.2898, 0.0793, 0.349, -0.043, 0.1547, -0.0557), triamcinolone = c(0.0115,
0.3329, -0.0752, 0.2784, -0.5543, 0.0139, -0.6692, -0.4599,
-4e-04, 0.0115, 0.0624, -0.2127, 0.1339, 0.1186, -0.0732,
0.1033, -0.0672, 0.0038, -0.0445, -0.2484, 0.0075, 0.1113,
0.3911, 0.0205, -0.0126, -0.0763, 0.0073, 0.4081, -0.2826,
-0.0348, 0.0957, -0.0069, 0.0103, 0.0607, 0.7144, 0.0012,
0.0216, 0.1009, -0.0395, -0.0432, 0.08, -0.1039, -0.38, 0.0532,
-0.0404, 0.4977, 0.0735, -0.0793, -0.4312, 0.0332, -0.0964,
0.1067, -0.0468, 0.0052, 0.1366), sulfaphenazole = c(-0.0886,
0.1081, 0.2002, 0.0625, 0.0403, 0.0256, -0.0074, -0.0678,
-0.0393, 0.3201, 0.4213, -0.3058, -0.1228, 0.0797, 0.0591,
-0.0541, -0.0721, -0.1914, -0.3058, 0.4353, 0.176, -0.1903,
0.142, -0.078, 0.0747, -0.3066, 0.2954, -0.2864, 0.0823,
-0.2976, 0.0354, 0.1075, 0.0696, 0.0413, -0.1505, -0.0958,
0.0996, 0.3221, -0.0582, 0.0723, 0.016, -0.3852, 0.0542,
0.0229, 0.1946, -0.0557, -0.0675, -0.2177, 0.2971, 0.3417,
-0.4023, -0.2623, 0.0103, -0.3864, 0.591), procyclidine = c(0.03,
0.0372, 0.335, 0.1666, -0.0048, -0.1153, 0.1449, -0.0488,
0.1038, -0.0245, 0.1008, 0.0194, -0.0315, -0.0325, 0.0161,
0.0316, 0.19, -0.3043, -0.1044, 0.2325, -0.0602, 0.0076,
0.1493, -0.049, 0.0593, -0.0343, 0.0667, 0.1323, 0.2388,
0.0962, -0.2329, -0.2198, 0.0395, -0.0078, 0.1016, 0.03,
-0.1293, -0.0076, -0.1876, -0.2648, 0.0044, 0.0609, 0.0403,
-0.0914, -0.1242, -0.0627, 0.067, 0.0665, 0.1154, 0.286,
-0.2009, 0.2039, -0.0567, -0.0365, -0.0198), pentoxifylline = c(0,
0.3439, -0.0614, 0.0181, -0.0149, -0.0216, -0.1211, -0.1816,
-0.0204, 0.1023, -0.0059, -0.008, -0.1121, 0.2029, 0.052,
-0.0935, 0, 0.0595, 0.0271, -0.0482, 0.0246, -0.1369, 0.1106,
0.4988, -0.0599, 0.0021, -0.0233, 0.2809, 5e-04, -0.0204,
0.0586, 0.0278, 0.0246, 0.0534, 0.0165, -0.1816, -0.071,
-0.0296, -0.1173, 0.0814, -0.0672, 0.0946, -0.2164, 0.0299,
-0.1341, 0.0439, 0.1124, 0.0125, -0.4091, -0.1134, 0.0098,
0.1957, 0.0044, 0.0922, 0.1851), suloctidil = c(-0.0772,
-0.1651, -0.0543, -0.037, 0.2182, -0.1884, 0.1866, 0.2013,
-0.3388, 0.0493, 0.0223, 0.3441, 0.0887, -0.1477, -0.0719,
-0.029, -0.0162, 0.0602, 0.1497, -0.0699, 0.1472, 0.3213,
-0.1266, 0.9143, 0.072, 0.4574, -0.1897, -0.7059, 0.3822,
-0.1055, -0.0744, 0.179, -0.5096, -0.4966, -0.8764, -0.3903,
0.174, 0.2181, -0.122, -0.0097, -0.2281, -0.1387, -0.0506,
0.1179, 0.1911, -0.9006, -0.0161, -0.2298, 0.1777, -0.1341,
-0.0264, -0.3478, -0.188, 0.035, 0.0441), etacrynic.acid = c(0.1017,
-0.4238, -0.0089, -0.1116, 0.1265, -0.0529, -0.121, -0.0243,
-0.0033, -1e-04, 0.0952, 0.4075, -0.3078, 0.1265, -0.0612,
-0.1239, 0.0381, 0.0491, 0.1062, -0.0398, 0.2044, 0.0341,
-0.0654, 0.2705, 0.0408, 0.0064, -0.0858, -0.19, -0.4934,
-0.0524, 2e-04, -0.1496, -0.1018, -0.3303, -0.665, -0.6764,
0, -0.0248, -0.091, 0.052, -0.0327, 0.2288, 0.8196, -0.1288,
0.038, -0.4468, 0.0013, -0.2137, 0.6752, 0.1275, 0.0198,
0.1209, -0.0735, 1e-04, -0.0706), diphenylpyraline = c(0.0495,
-0.1318, -0.0723, -0.0485, 0.0175, -0.1585, 0.0054, 0.0565,
-0.0637, 0.0366, 0.0155, 0.1003, -0.11, -0.129, 0.0673, -0.0519,
0.0284, -0.1663, 0.0224, -0.1806, -0.1265, 0.1559, -0.1337,
-0.0892, 0.08, -0.2713, -0.0064, -0.0193, -0.2862, -0.0627,
0.1242, 0.0607, -0.1815, -0.107, 0.038, -0.0171, 0.1978,
0.1513, -0.1016, -0.086, -0.0019, 0.042, -0.0073, -0.0751,
-0.0388, 0.0844, -0.0678, -0.0556, 0.1765, -0.0377, -0.0143,
0.3352, 0.0206, 0.1197, -0.1429), carbenoxolone = c(0.0769,
0.0987, 0.1992, -0.016, -0.0276, 0.2596, -0.0086, -0.0038,
-0.2432, -0.0237, 0.0696, -0.1436, 0.1942, -0.0411, -0.0948,
0.2645, -0.114, -0.0887, -0.6041, 0.0061, 0.0875, -0.1449,
-0.1735, -0.0606, -0.0785, 0.3483, 0.1196, 0.2659, 0.0614,
-0.1372, 0.1543, 0.0434, -0.0253, -0.0028, 0.0956, 0.3397,
-0.0771, 0.2627, -0.0028, 0.1337, 0.0046, -0.0757, -0.0112,
-0.084, 0.0312, 0.0935, -0.0709, 0.0829, -0.1061, -0.1504,
0.0122, 6e-04, 0.3138, 0.0632, 0.1019), arecoline = c(0.0185,
0.1999, -0.0313, -0.1868, -0.0626, 0.0298, 0.03, 0, -0.2209,
0.0101, -0.0693, -0.1656, -0.1048, 0.2098, 0.0393, -0.1354,
0.0328, -0.0311, -0.1967, -0.2653, 0.204, 0.1737, 0.36, -0.1034,
-0.3326, 0.0613, -0.2044, 0.1967, 0.0042, 0.2329, 0.2409,
-0.0335, 0.0068, 0.5101, 0.2002, 0.1867, 0.2134, -0.1072,
0.0854, 0.2414, -0.0653, 0.3851, 0.1358, -0.0102, 0.0268,
0.2497, -0.1376, 0.1798, 0.0427, 0.1593, -0.29, 0.0343, 0.2249,
-0.1301, -0.0987), chenodeoxycholic.acid = c(-0.0825, 0.2098,
-0.0911, -0.0287, 0.0473, 0.1348, -0.06, -0.0285, -0.1473,
-0.0162, -0.0863, 0.0652, -0.0256, 0.1345, -0.1175, -0.0141,
0.2985, 0.2963, 0.1925, -0.212, -0.2106, -0.1128, -0.3121,
0.0867, 0.0214, 0.0346, 0.0173, 0.1101, -0.3066, 0.1115,
0.0415, 0.285, 0.0787, -0.0985, -0.027, -0.2767, 0.1572,
-0.0518, 0.0815, 0.0168, -0.2047, -0.1517, -0.0076, -0.0359,
0.0596, -0.0706, 0.1006, 0.0099, 0.1661, -0.0435, 0.0331,
0.0996, 0.197, -0.0067, -0.0328), torasemide = c(0.0987,
0.1829, 0.1693, -0.0482, 0.0534, 0.1624, 0.0047, -0.1721,
0.248, 0.1715, -0.2109, -0.0909, 0.0513, -0.1358, 0.6297,
0.0762, -0.3473, -0.2189, -0.126, 0.3054, -0.0451, -0.2243,
0.0914, -0.0199, -6e-04, -0.1102, -0.0082, 0.0242, -0.053,
-0.0825, -0.1874, 0.1251, 0, -0.0457, -0.1179, -0.3169, 0.0559,
0.1999, 0.262, 0.0462, 0.1038, -0.0999, -0.1278, -0.1944,
0.0074, 0.1785, 0.0486, -0.027, -0.2748, -0.0428, 0.0696,
-0.1544, 0.117, 0.1665, 0), troglitazone = c(-0.1513, 0.2568,
0.1377, 0.2474, -0.0359, -0.0859, 0.2014, 0.096, 0.0751,
0.1304, -0.1376, -0.1718, 0.0063, 0.2732, -0.2237, 0.1951,
-0.0496, -0.0087, 0.0245, -0.1212, 0.194, -0.0971, -0.0184,
0.1071, 0.0129, -0.061, 0.1704, -0.0021, 0.3985, 0.0963,
0.1591, -0.381, 0.2277, 0.5395, 0.0611, -0.0204, 0.0614,
-0.2816, 0.0308, -0.0642, -0.1722, -0.0191, -0.0374, 0.0436,
-0.3296, 0.0379, -0.0517, 0.2978, 0.0834, -0.1304, 0.1039,
0.2279, 0.5988, 0.0424, -0.2677), mepenzolate.bromide = c(0.0392,
-0.0041, -0.0531, -0.1213, -0.0919, 0.3096, 0.0545, 0.0922,
0.112, 0.0606, -0.25, -0.0086, 0.2729, 0.1307, 0.2124, -0.039,
-0.1005, 0.0125, 0.0644, -0.0064, 0.0707, 0.1931, -0.5523,
-0.285, -0.3085, 0.0343, 0.0622, -0.1849, -0.0436, 0.1069,
0.072, 0.0312, 0.1016, -0.1611, -0.139, 0.0047, 0.0297, -0.1409,
-0.0834, -0.0087, 0.2839, -0.03, -0.0378, -0.0705, 0.0836,
-0.0824, 0.0803, -0.1378, 0.014, -0.0351, 0.0049, 0.0153,
-0.093, 0.0263, 0.0855), megestrol = c(-0.1106, 0.2547, -0.0488,
0, -0.0406, -0.0826, -0.1795, -0.3099, -0.0316, 0.1563, 0,
-0.1189, -0.0343, -0.0482, 0.8041, 0.5202, 0.0733, 0.0419,
0.2212, -0.0888, 0.0268, 0.0886, -0.0476, -0.3235, -0.1348,
-0.2665, 0.294, 0.122, -0.1695, 0.1186, 0.0927, 0, -0.0429,
-0.0492, 0.0963, -0.0934, 0.1289, 0.1447, -0.0308, -0.1145,
0.1861, -0.0464, 0.0992, 0, 0.0771, -0.1804, 0.0899, 0.1031,
0, 0.1035, 0.1404, 0, -0.1281, -0.2463, -0.2874), dexpropranolol = c(-0.1942,
-0.0051, 0.0561, 0.0166, 0.0029, -0.1707, 0.2173, 0.1178,
-0.0683, -0.2903, -0.2874, 0.1764, -0.1135, 0.2574, 0, -0.1194,
-0.4694, -0.285, 0.0364, -0.0362, 0.158, -0.0648, -0.0046,
0.1435, -0.0946, 0.11, 0.1712, 0.3853, 0.1692, -0.0977, 0.3577,
0.2089, 0.3066, -0.0788, 0.3092, 0.113, -0.1234, 0.5753,
0.0359, 0.1014, 0.2017, 0.2949, 0.019, -0.0855, 0.0428, 0.2171,
-0.0951, -0.0761, -0.1451, -0.2832, -0.2653, -0.166, 0.0376,
-0.0214, 0.0131), nipecotic.acid = c(0.2653, -0.3115, -0.0069,
0.118, -0.0998, -0.0289, 0.1051, 0.0132, -0.0028, -0.1072,
-0.1888, -0.0312, 0.3868, -0.0019, -0.1878, -0.0798, -0.3881,
-0.2162, -0.1189, 0.1569, -0.0537, 0.0196, -0.0013, -0.0818,
-0.1885, 0.2266, 0.2862, 0.1302, 0.0734, 0.0936, -0.0159,
-0.0974, -0.0253, 0.2637, -0.2069, 0.2298, -0.1518, -0.13,
-0.0344, -0.0623, -0.0307, 0.1186, -0.2535, 0.0387, 0.0374,
0.0443, -0.0783, 0.1588, -0.0239, 0.1104, 0.1518, 0.1921,
-0.0744, -0.0125, -0.19), omeprazole = c(0.0272, -0.3796,
-0.0125, 0.0585, -0.1073, -0.0761, 0.3838, 0.5699, 0.0027,
-0.3822, 0.1132, -0.1588, 0.2396, -0.217, 0.0434, 0.1336,
-0.0608, -0.0294, 0.1702, -0.2499, 0.0568, 0.0348, -0.0486,
0, 0.2792, -0.1318, 0.0249, 0.005, 0.1688, -0.3908, -0.2366,
-0.0204, -0.0672, -0.0181, -0.0514, 0.139, 0.0582, 0.109,
0.1018, -0.2249, 0.0432, -0.1882, 0.0818, -0.1082, -0.0456,
-0.0764, 0.4292, 0.0553, -0.1346, -0.2173, 0, 0.1391, 0.1908,
-0.0633, 0.0385), etanidazole = c(-0.1036, 0.0281, 0.0039,
0.0123, -0.2305, -0.0542, 0.0485, -0.0686, -0.1829, 0.2637,
-0.1158, -0.2029, 0.104, 0.3169, 0.1387, 0.0844, -0.1385,
-0.3046, 0.0706, -0.076, -0.1772, 0, -0.393, 0.0841, -0.2469,
-0.1261, 0.1085, 0.0265, 0.0141, 0.0183, 0.2398, -0.0353,
-0.5217, -0.1577, 0.4357, -0.1347, 0.0362, 0.4197, -0.0326,
0.1955, -0.0352, -0.0122, -0.0658, -0.1482, -0.1485, 0.3406,
0.0618, 0.0822, 0.2024, -0.3203, 0.1646, -0.1412, 0.3221,
-8e-04, 0.0472), acenocoumarol = c(-0.0087, 0.2005, -0.1051,
-0.067, -0.0655, -0.0467, -0.4081, -0.1085, -0.0556, 0.0145,
-0.0865, 0.1961, -0.1842, -0.0398, -0.0944, 0.046, 0.0632,
-0.1008, 0.0837, 0.0023, 0.0482, 0.0195, -0.2322, -0.0293,
0.0671, -8e-04, -0.013, -0.0905, -0.0019, -0.0333, 0.0421,
-0.0712, -0.0771, 0.1843, 0.0589, 0.0229, -0.0976, -0.0184,
0.1559, -0.0297, -0.1089, -0.0461, -0.0322, 0.1253, 0.0408,
-0.1147, 0.082, 0.1072, 0.1094, 0.1066, 0.104, 0.1947, 0.1116,
-0.1421, -0.0934), dacarbazine = c(-0.0065, -0.7082, 0.0979,
0.0048, -0.1409, 0.0541, -0.0793, -0.1102, -0.1513, -0.002,
-0.0898, 0.0443, 0.0686, -0.1122, -0.0239, 0.0126, -0.0093,
0.0366, 0.0061, -0.1217, 0.0259, -0.1117, -0.1178, -0.2467,
-0.1128, -0.0671, 0.0347, 0.4719, -0.3849, -0.0517, 0.0764,
0.2058, 0.3147, 0.3275, 0.551, 0.1175, 0.0383, -0.1004, 0.0425,
0.1717, 0.174, 0.0122, -0.1466, 0.0381, -0.0656, 0.2572,
-0.0851, 0.0996, -0.2686, -0.1501, 0.0994, 0.001, 0.0891,
0.0652, -0.2386), diphemanil.metilsulfate = c(-0.0312, -0.2611,
-0.0098, -0.216, 0.24, 0.0909, 0.1171, -0.156, 0.0986, -0.0946,
0.1027, -0.1465, 0.0611, 0.0637, 0.0677, -0.0017, -0.0884,
-0.0023, 0.012, -0.3062, -0.0396, -0.0197, -0.2654, -0.1362,
-0.1075, 0.0738, 0.0039, -0.047, -0.0562, -0.0751, 0.0048,
-0.0273, 0.1636, -0.003, 0.1565, 0.2033, -0.1181, -0.046,
0.0056, 0.0896, -0.0309, -0.0146, -0.0784, 0.002, 0.1111,
0.2156, 0.0915, 0.1174, 0.2337, -0.0151, -0.056, 0.0705,
0.0522, -0.0171, -0.3229), meprylcaine = c(-0.0382, -0.1127,
0, 0, 0.0964, 0.0097, -0.0231, -0.0747, 0.2147, 0.0618, -0.2313,
0.0878, -0.3681, 0.0497, -0.0105, 0.2084, 0.0492, 0.0423,
0.0634, -0.0457, -0.0883, 0.0104, 0.1429, -0.1485, 0.4838,
0.0139, 0.2511, -0.1276, -0.1517, -0.0574, -0.0525, -0.0053,
-0.0796, 0.1029, -0.0393, -0.0587, -0.1787, -0.2193, 0.1723,
0.1684, 0.1708, 0.1961, -0.027, 0.0921, -0.0293, -0.2186,
-0.1013, -0.043, 0.2408, -0.2149, 0.2316, -0.0559, 0.2917,
-0.0128, -0.2173), mevalolactone = c(-0.0393, -0.174, -0.1632,
0.4181, -0.038, 0.1698, -0.1771, -0.168, -0.3952, 0.1739,
0.1304, 0, -0.0317, -0.1822, -0.2021, -0.0743, 0.3155, 0.0782,
0.2533, -0.1159, 0.0135, 0.2274, -0.1785, -0.4384, -0.268,
0.1331, -0.4809, 0.6483, -0.7939, -0.1549, -0.0138, -0.0604,
0.043, 0.1208, 0.4608, 0.4028, 0.1985, 0.1172, 0.0787, 0.099,
-0.0329, 0.1553, -0.0503, -0.1355, 0.0382, 0.4655, -0.1948,
-0.1411, -0.0149, 0.0259, -0.4708, 0.0715, 0.0608, 0, 0.5629
), buspirone = c(-0.1276, -0.0097, -0.1365, 0.0801, -0.0499,
0.0499, 0.2245, 0.0739, -0.0485, -0.0042, -0.2356, -0.1325,
0, 0, -0.0756, 0.0099, 0.069, 0.062, 0.0404, 0.2093, -0.0973,
-0.237, -0.2371, 0.1403, -0.0553, -0.043, 0.0219, 0.0596,
0.0999, -0.1358, 0.1098, -0.06, 0.1429, -0.0758, 0.1528,
-0.0426, 0.1053, 0.2444, -0.0031, -0.2131, -0.1855, 0.144,
0.0037, 0.1078, 0.2742, 0.247, 0.0755, 0.0187, 0.0075, -0.0934,
-0.2814, 0.1086, -0.379, -0.1291, -0.0597), sulfafurazole = c(0.0336,
0.0083, 0.0309, 0.1891, -0.0991, 0.0259, 0.084, 0.0888, 0.0672,
-0.0859, -0.0196, -0.4085, 0.0676, 0.0357, -0.0486, -0.2493,
-0.053, -0.128, 0.103, -0.0471, 0.0839, 0.3382, 0.1353, 0.2906,
0.1022, -0.1326, -0.0457, 0.3148, 0.2374, 0.0719, 0.0469,
-0.2298, 0.1334, 0.1581, -0.1396, -0.2614, -2e-04, 0.0297,
-0.0349, -0.1307, -0.1516, -0.0526, 0.0468, 0.0534, -0.0268,
0.2099, -0.0794, 0.2252, 0.095, -0.1607, 0.1593, 0.0157,
0.0283, 0.1304, -8e-04), ciclopirox = c(0.0958, 0.2534, -0.1897,
-0.0457, 0.1704, -0.2632, -0.2596, -0.1076, 0.3008, 0.1348,
-0.0817, 0.2929, -0.2249, -0.1357, -0.0498, 0.3122, 0.1269,
0.0809, 0.05, -0.2514, -0.3665, -0.1104, 0.0793, 0.8682,
0.3056, 0.2149, -0.3415, 0.1955, 0.2518, -0.1525, 0.0429,
0.4573, -0.556, -0.2508, 0.0437, 0.0459, 0.13, 0.104, 8e-04,
-0.5053, -0.4061, -0.2273, 0.1871, 0.1646, -0.0224, 0.1323,
-0.1473, -0.0917, -0.214, 0.0844, -0.4498, 0.0313, 0.0385,
-0.1485, -0.0553)), .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("200665_s_at",
"201125_s_at", "201150_s_at", "201162_at", "201560_at", "202133_at",
"202435_s_at", "202436_s_at", "202766_s_at", "203083_at", "203238_s_at",
"203640_at", "204223_at", "204468_s_at", "204589_at", "204619_s_at",
"205381_at", "205422_s_at", "205713_s_at", "205941_s_at", "208626_s_at",
"209581_at", "209747_at", "209875_s_at", "209894_at", "210004_at",
"210078_s_at", "210495_x_at", "210511_s_at", "210517_s_at", "210809_s_at",
"211071_s_at", "211597_s_at", "211709_s_at", "211719_x_at", "212464_s_at",
"212488_at", "212667_at", "213125_at", "213351_s_at", "213352_at",
"213413_at", "213656_s_at", "214770_at", "214927_at", "216442_x_at",
"217428_s_at", "217949_s_at", "218718_at", "219054_at", "219087_at",
"219179_at", "219922_s_at", "222379_at", "37022_at"), class = "data.frame")
I want to heatmap all the columns based on only those rows that have mean of higher than 0.02.
what I do is as follows:
tab <- melt(transform(df, rowname = row.names(df)))
t<- data.frame(sort(rowMeans(df)))
t[t[,1] > 0.02,, drop=FALSE]
library(ggplot2)
ggplot(subset(tab, rowname %in% rownames(t)[t > 0.02]),
aes(x = rowname, y = variable, fill = value)) +
geom_tile() +
scale_fill_gradient2(high="red",mid="white",low="blue") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
but it does not do the job, it does put the x axis as it wants not based on the order, for example I want to be from 0.02 to 0.05
so the other looks like
204619_s_at 0.02055667
211709_s_at 0.02357333
216442_x_at 0.02364000
201162_at 0.02389667
217949_s_at 0.03153333
201150_s_at 0.03251000
204468_s_at 0.03279000
211719_x_at 0.03447667
219922_s_at 0.03968667
212667_at 0.04798667
212488_at 0.04833333
209875_s_at 0.05173333
203083_at 0.05493667
210495_x_at 0.05858333
219179_at 0.05932667
in axis and not what it shows
You can try
rn <- rownames(t[t[,1] > 0.02,, drop=FALSE])
tab1 <- subset(tab, rowname %in% rownames(t)[t > 0.02])
tab1$rowname <- factor(tab1$rowname, levels=rn)
library(ggplot2)
ggplot(tab1,aes(x = rowname, y = variable, fill = value)) +
geom_tile() +
scale_fill_gradient2(high="red",mid="white",low="blue") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
Or keeping most of the steps within the %>%
library(dplyr)
library(tidyr)
library(ggplot2)
bind_cols(data.frame(rowname=row.names(df)), df) %>%
filter(rowMeans(.[-1]) >0.02) %>%
gather(variable, value,-rowname) %>%
mutate(rowname=factor(rowname, levels=rn)) %>%
ggplot(., aes(x=rowname, y=variable, fill=value))+
geom_tile()+
scale_fill_gradient2(high='red', mid='white', low='blue')+
theme(axis.text.x = element_text(angle = 90, vjust=0.5)) +
xlab('x axis') +
ylab('y axis')

how to select a specific numbers of rows with multiple condition?

I have a data frame like below
df<- structure(list(phenelzine = c(-0.0269, 0.0217, 0.4609, -0.0011,
0.0016, -0.0334, 0.5159, 0.4687, -0.5715, -0.1466, 0.1096, 0.1006,
-0.1021, 0.0093, -0.2616), denatonium.benzoate = c(-0.1734, -0.2142,
0.2142, -0.1376, -0.0129, 0.0254, 0.06, 0.1768, 0.2295, 0.1772,
0.1978, -0.0556, 0.0971, 0.0979, 0.2073), triamterene = c(-0.0253,
0.2896, 0.1606, -0.086, -0.1886, 0.0355, 0.0552, 0.0733, 0.0131,
0.6887, -0.0161, -0.3962, 0.1294, 0.0316, -0.1851), talampicillin = c(-0.1761,
-0.0355, 0.0295, 0.1979, 0.0545, 0.0673, -0.0797, -0.1566, 0.0496,
0.2623, -0.197, 0.0192, -0.2673, 0.0687, -0.0058), triamcinolone = c(0.0115,
0.3329, -0.0752, 0.2784, -0.5543, 0.0139, -0.6692, -0.4599, -4e-04,
0.0115, 0.0624, -0.2127, 0.1339, 0.1186, -0.0732), sulfaphenazole = c(-0.0886,
0.1081, 0.2002, 0.0625, 0.0403, 0.0256, -0.0074, -0.0678, -0.0393,
0.3201, 0.4213, -0.3058, -0.1228, 0.0797, 0.0591), procyclidine = c(0.03,
0.0372, 0.335, 0.1666, -0.0048, -0.1153, 0.1449, -0.0488, 0.1038,
-0.0245, 0.1008, 0.0194, -0.0315, -0.0325, 0.0161), pentoxifylline = c(0,
0.3439, -0.0614, 0.0181, -0.0149, -0.0216, -0.1211, -0.1816,
-0.0204, 0.1023, -0.0059, -0.008, -0.1121, 0.2029, 0.052), suloctidil = c(-0.0772,
-0.1651, -0.0543, -0.037, 0.2182, -0.1884, 0.1866, 0.2013, -0.3388,
0.0493, 0.0223, 0.3441, 0.0887, -0.1477, -0.0719), etacrynic.acid = c(0.1017,
-0.4238, -0.0089, -0.1116, 0.1265, -0.0529, -0.121, -0.0243,
-0.0033, -1e-04, 0.0952, 0.4075, -0.3078, 0.1265, -0.0612), diphenylpyraline = c(0.0495,
-0.1318, -0.0723, -0.0485, 0.0175, -0.1585, 0.0054, 0.0565, -0.0637,
0.0366, 0.0155, 0.1003, -0.11, -0.129, 0.0673), carbenoxolone = c(0.0769,
0.0987, 0.1992, -0.016, -0.0276, 0.2596, -0.0086, -0.0038, -0.2432,
-0.0237, 0.0696, -0.1436, 0.1942, -0.0411, -0.0948), arecoline = c(0.0185,
0.1999, -0.0313, -0.1868, -0.0626, 0.0298, 0.03, 0, -0.2209,
0.0101, -0.0693, -0.1656, -0.1048, 0.2098, 0.0393), chenodeoxycholic.acid = c(-0.0825,
0.2098, -0.0911, -0.0287, 0.0473, 0.1348, -0.06, -0.0285, -0.1473,
-0.0162, -0.0863, 0.0652, -0.0256, 0.1345, -0.1175), torasemide = c(0.0987,
0.1829, 0.1693, -0.0482, 0.0534, 0.1624, 0.0047, -0.1721, 0.248,
0.1715, -0.2109, -0.0909, 0.0513, -0.1358, 0.6297), troglitazone = c(-0.1513,
0.2568, 0.1377, 0.2474, -0.0359, -0.0859, 0.2014, 0.096, 0.0751,
0.1304, -0.1376, -0.1718, 0.0063, 0.2732, -0.2237), mepenzolate.bromide = c(0.0392,
-0.0041, -0.0531, -0.1213, -0.0919, 0.3096, 0.0545, 0.0922, 0.112,
0.0606, -0.25, -0.0086, 0.2729, 0.1307, 0.2124), megestrol = c(-0.1106,
0.2547, -0.0488, 0, -0.0406, -0.0826, -0.1795, -0.3099, -0.0316,
0.1563, 0, -0.1189, -0.0343, -0.0482, 0.8041), dexpropranolol = c(-0.1942,
-0.0051, 0.0561, 0.0166, 0.0029, -0.1707, 0.2173, 0.1178, -0.0683,
-0.2903, -0.2874, 0.1764, -0.1135, 0.2574, 0), nipecotic.acid = c(0.2653,
-0.3115, -0.0069, 0.118, -0.0998, -0.0289, 0.1051, 0.0132, -0.0028,
-0.1072, -0.1888, -0.0312, 0.3868, -0.0019, -0.1878), omeprazole = c(0.0272,
-0.3796, -0.0125, 0.0585, -0.1073, -0.0761, 0.3838, 0.5699, 0.0027,
-0.3822, 0.1132, -0.1588, 0.2396, -0.217, 0.0434), etanidazole = c(-0.1036,
0.0281, 0.0039, 0.0123, -0.2305, -0.0542, 0.0485, -0.0686, -0.1829,
0.2637, -0.1158, -0.2029, 0.104, 0.3169, 0.1387), acenocoumarol = c(-0.0087,
0.2005, -0.1051, -0.067, -0.0655, -0.0467, -0.4081, -0.1085,
-0.0556, 0.0145, -0.0865, 0.1961, -0.1842, -0.0398, -0.0944),
dacarbazine = c(-0.0065, -0.7082, 0.0979, 0.0048, -0.1409,
0.0541, -0.0793, -0.1102, -0.1513, -0.002, -0.0898, 0.0443,
0.0686, -0.1122, -0.0239), diphemanil.metilsulfate = c(-0.0312,
-0.2611, -0.0098, -0.216, 0.24, 0.0909, 0.1171, -0.156, 0.0986,
-0.0946, 0.1027, -0.1465, 0.0611, 0.0637, 0.0677), meprylcaine = c(-0.0382,
-0.1127, 0, 0, 0.0964, 0.0097, -0.0231, -0.0747, 0.2147,
0.0618, -0.2313, 0.0878, -0.3681, 0.0497, -0.0105), mevalolactone = c(-0.0393,
-0.174, -0.1632, 0.4181, -0.038, 0.1698, -0.1771, -0.168,
-0.3952, 0.1739, 0.1304, 0, -0.0317, -0.1822, -0.2021), buspirone = c(-0.1276,
-0.0097, -0.1365, 0.0801, -0.0499, 0.0499, 0.2245, 0.0739,
-0.0485, -0.0042, -0.2356, -0.1325, 0, 0, -0.0756), sulfafurazole = c(0.0336,
0.0083, 0.0309, 0.1891, -0.0991, 0.0259, 0.084, 0.0888, 0.0672,
-0.0859, -0.0196, -0.4085, 0.0676, 0.0357, -0.0486), ciclopirox = c(0.0958,
0.2534, -0.1897, -0.0457, 0.1704, -0.2632, -0.2596, -0.1076,
0.3008, 0.1348, -0.0817, 0.2929, -0.2249, -0.1357, -0.0498
)), .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("200665_s_at",
"201125_s_at", "201150_s_at", "201162_at", "201560_at", "202133_at",
"202435_s_at", "202436_s_at", "202766_s_at", "203083_at", "203238_s_at",
"203640_at", "204223_at", "204468_s_at", "204589_at"), class = "data.frame")
what I want to do, is after I melt the data, I only keep those that t>0.02 but not all, I want to be able to say, the last 10, or the last 5 or a specific number I like , This should apply on all variables and not only one variable
what i do is as follows:
# sort the mean of the df
t<- data.frame(sort(rowMeans(df)))
# select only those that are higher than 0.02
rn <- rownames(t[t[,1] > 0.02,, drop=FALSE])
# melt the data frame
tab <- melt(transform(df, rowname = row.names(df)))
# select only those that are higher than 0.02
tab1 <- subset(tab, rowname %in% rownames(t)[t > 0.02])
You can use tailto get the last rows of a vector or a data.frame:
tail(tab1 ,5) ## the last five rows
tail(t,5) ## the last five conditions rows

Resources