Related
I am trying to create the following plot with the black background.
I have used this commands
x <- c(134.21, 139.12, 145.7, 148.81, 157.27, 128.4, 147.44, 133.72, 147.26, 137.26, 136.93, 137.37, 145.4, 141.74, 145.35, 130.01, 140.1, 136.3, 141.02, 137.5, 139.62, 140.05, 139.55, 135.55, 138.93, 140.07, 154.76, 138, 146.95, 139.62, 137.38, 146.27, 148.23, 152.65, 137.82, 145.28, 135.52, 152.97, 138.03, 138.82, 149.63, 131.83, 132.68, 151.5, 148.5, 142.79, 131.76, 132.83)
y <- c(0.0212, 0.1136, 0.0842, -0.0285, -0.1362, 0.1928, 0.0491, 0.0653, 0.0358, 0.082, -3e-04, 0.0659, -0.1173, -0.0707, -0.1298, 0.2789, 0.0092, 0.1105, -0.0603, 0.0409, 0.021, 0.0479, 0.0495, 0.1562, -0.1276, 0.0486, -0.519, 0.0548, 0.0072, 0.05, -0.0595, -0.0469, -0.1501, -0.2125, -0.0205, 0.0293, 0.0025, 3e-04, 0.0013, 0.0286, -0.1069, 0.1171, 0.1073, -0.0799, 0.0197, -0.0664, -0.0976, 0.0696 )
par(bg="black")
plot(x,y,pch=21,bg="white",col="red",cex=2,lwd=1,col.lab="white",col.axis="white",cex.lab=1,main="Scatterplot",font.main=1)
axis(1,col="white",col.axis="yellow")
axis(2,col="white",col.axis="yellow")
abline(lm(y~x),col="red",lwd=3)
I have two problems that I can not solve:
I can not see the title 'Scatterplot' on the image
It seems that the lines are cropped, when plotting with a white background the x,y axis seem ok. However I need this graph with a dark background.
Does anyone have an idea how to resolve this?
Not sure what you mean about lines being cropped, but maybe this is what you want:
plot(x, y, pch=21, bg="white", col="red", cex=2, lwd=1, col.lab="white",
col.axis="white", cex.lab=1, col.main="white", main="Scatterplot", font.main=1)
axis(1, col="white", col.axis="yellow")
axis(2, col="white", col.axis="yellow")
abline(lm(y ~ x), col="red", lwd=3)
box(col="white", lwd=2)
)
This should do it:
x <- c(134.21, 139.12, 145.7, 148.81, 157.27, 128.4, 147.44, 133.72, 147.26, 137.26, 136.93, 137.37, 145.4, 141.74, 145.35, 130.01, 140.1, 136.3, 141.02, 137.5, 139.62, 140.05, 139.55, 135.55, 138.93, 140.07, 154.76, 138, 146.95, 139.62, 137.38, 146.27, 148.23, 152.65, 137.82, 145.28, 135.52, 152.97, 138.03, 138.82, 149.63, 131.83, 132.68, 151.5, 148.5, 142.79, 131.76, 132.83)
y <- c(0.0212, 0.1136, 0.0842, -0.0285, -0.1362, 0.1928, 0.0491, 0.0653, 0.0358, 0.082, -3e-04, 0.0659, -0.1173, -0.0707, -0.1298, 0.2789, 0.0092, 0.1105, -0.0603, 0.0409, 0.021, 0.0479, 0.0495, 0.1562, -0.1276, 0.0486, -0.519, 0.0548, 0.0072, 0.05, -0.0595, -0.0469, -0.1501, -0.2125, -0.0205, 0.0293, 0.0025, 3e-04, 0.0013, 0.0286, -0.1069, 0.1171, 0.1073, -0.0799, 0.0197, -0.0664, -0.0976, 0.0696 )
par(bg="black")
plot(x,y, xlim = c(125, 160), ylim = c(-0.6, 0.4), pch=21,bg="white",col="red",cex=2,lwd=1,col.lab="white",col.axis="white",cex.lab=1, col.main ="white", main="Scatterplot",font.main=1)
axis(1,col="white",col.axis="yellow")
axis(2,col="white",col.axis="yellow")
abline(lm(y~x),col="red",lwd=3)
I computed historic VaR on a simple equity portfolio (for simplicity's sake, let's say there are only two stocks with one year of daily history). Then I did it again using the PerformanceAnalytics R package. My two results don't match.
Am I missing something on how to do basic VaR, or is PerformanceAnalytics doing something strange?
My sample code is below:
library(xts)
# (I'll define "sr" at the end of the question. It's a big xts structure)
weights = c(0.7929511, 0.2070489)
p = 0.95
# How I do it:
m = data.matrix(as.data.frame(sr))
daily_whatif_scenario = tcrossprod(weights, m)[1,]
my_historic_VaR = as.numeric(-quantile(daily_whatif_scenario, 1 - p)[1])
# How PerformanceAnalytics does it:
their_historic_VaR = PerformanceAnalytics::VaR(R = sr, p = p,
method = "historical", portfolio_method = "component", weights = weights)
My calculation comes up with 1.325%, but the package gives me 1.395%. Why such a big difference? These differences only get bigger as I add more stocks and history to my example. Am I doing something wrong?
Here is the result of dput(sr):
sr = structure(c(-0.0162, 0.0049, 0.0029, -1e-04, -0.0079, -0.0101,
0.0053, 0.007, -0.0107, 0.001, -0.003, -0.0033, 0.0013, 0.0163,
0.0102, 2e-04, -0.0027, 0.0303, -0.0149, 0.0034, 0.0073, 0.0111,
-0.0057, 0.0022, 0.0266, 0.0038, 0.0062, -0.0063, 0.0078, 0.0022,
0.0091, -0.0074, 0.008, 0.0068, 0.0048, 0.0031, 0.0142, -0.0034,
-0.0089, 0.001, 0.001, -0.0141, 0.0042, 0.0026, -0.017, 0.001,
-0.006, 0.0048, 0.0064, -0.0063, -0.0122, 0.0122, -0.0126, 0.0239,
-0.0236, -9e-04, 0.004, -0.0049, 0.0028, -0.0109, 0.0106, 0.0066,
0.0096, 0.0075, 0.0082, -0.008, 0.047, -6e-04, 5e-04, -0.005,
-0.0084, -0.0074, 0.0078, -0.0029, 0.0071, 5e-04, 0.0118, 0.0068,
-0.0032, 0.0068, -0.0011, -0.0095, 0.0029, -0.0019, -0.0084,
0.0065, -0.014, 0.0049, -0.0042, 0.0071, -0.0097, -0.0038, -0.0049,
0.0045, 0.001, -7e-04, -0.0056, 0.0098, -0.0112, 0.0093, 5e-04,
-6e-04, 0, -0.0013, 0.0026, 0.0199, -8e-04, 0.0023, -0.0038,
-0.0035, -0.0045, -0.0323, -0.0021, -0.0054, -0.0102, 0.0222,
-0.0139, -0.0027, 6e-04, -5e-04, -0.0069, -8e-04, 0.0074, -0.0071,
0.0017, -0.0142, 0.0111, 0.0022, -0.0085, 0.0183, -0.0061, -0.0031,
0.0083, 0.0111, -0.0028, -0.0051, -0.0149, 0.0026, -0.0046, -0.0105,
-0.003, 0.0019, 0.0055, 0.0471, 0.0042, 9e-04, -0.0226, -0.0175,
0.0169, 3e-04, 8e-04, -0.0258, -0.0047, -0.0044, 0.0071, 0.0129,
0.0063, 0.0248, 0.0107, 0.0133, -0.0214, 0.0045, 0.0175, -0.0034,
0.0048, -1e-04, -0.0118, 0.0082, -0.0024, 0.0019, -0.0139, 0.0037,
-0.0018, -0.0166, -0.002, -0.0058, 0.0053, -0.0086, 0.0159, 0.0027,
-0.0097, 0.0186, -0.0099, 0.0362, 0.0034, -0.0044, 0.005, -0.0074,
0.0031, 0.0151, 0, -0.0036, -0.0054, -0.004, -0.0088, 0, 0.015,
-0.0019, 3e-04, 0.0087, -0.0017, 0.0015, 0.0046, -0.0177, 0.0018,
0.0021, -0.0142, 4e-04, -0.005, -0.001, -0.0108, -5e-04, 0.0167,
0, -7e-04, -0.0079, 0.0397, -6e-04, -0.0132, -0.0013, -0.0022,
0.001, -0.0021, 0.0119, -0.0031, 0.0065, 0.001, -0.0014, -0.0054,
8e-04, -0.0021, 0.0103, -0.0016, -0.0014, 0.0169, -4e-04, -0.0067,
0.0249, 0.0018, 0.0047, -0.0117, -0.0015, 0.0133, 0.0144, -3e-04,
-0.0084, -0.0067, 0.0048, -0.0226, 0.001, -0.0079, -1e-04, 0.0062,
0.0037, 0, 7e-04, -0.0046, -0.0045, -7e-04, 0.0097, -0.0066,
-0.0054, -0.0029, -0.0041, -0.0113, 0.0059, -0.0134, -0.0177,
0.0057, -0.0043, -0.0106, -0.0115, -0.0198, 0.0131, 0.0086, -0.008,
0.0286, -0.0042, 6e-04, 0.0151, 0.0071, 0.0058, 0.0056, 0.0165,
0.0143, 0.0092, 0.0023, 0.0168, 0.0068, -0.0033, -0.0296, -5e-04,
0.0191, -0.0106, 0.0062, -0.002, -0.001, 0.0038, 0.0039, 0.001,
-0.01, 0.0059, 0.0036, -0.0063, -0.0082, -0.0034, 0.0039, 0.0184,
0.0055, -0.0084, 0.0093, -0.0104, 0.0289, -0.01, -0.0098, 0.0108,
-0.0068, -0.0056, 0.002, 0.0206, -0.008, 0.0212, 0.0496, 0.0038,
-0.008, 0.0122, 0.0029, -0.0014, -9e-04, -0.0077, -0.016, 0.0064,
0.0012, 0.0056, 0.0039, 0.0176, -0.0125, -0.0088, 0.0075, 0.0049,
-0.0131, -0.004, 0.0052, -0.004, 0.0021, -0.0162, -0.0126, 0.0042,
0.0032, -0.0059, 0.0014, -0.0034, 7e-04, 0.0043, -6e-04, -0.0067,
0.0067, 0.0045, 0.0096, -3e-04, -0.0082, 0.0038, 0.0053, -0.013,
0.0197, -0.0057, 0.0041, -0.0142, 0.0114, -0.0045, -0.0123, -0.0433,
-0.0066, -0.0212, 0.0257, -0.0068, 8e-04, -0.0031, -0.0029, -0.0097,
-0.0074, 0.0065, -0.013, -0.0036, -0.013, -0.002, 0.0032, -0.0087,
0.0054, -0.001, -0.0118, 0.0034, 0.0051, -0.0065, 2e-04, 0.0015,
-3e-04, 0.0043, -0.0168, 8e-04, -0.0033, -0.0094, -0.0017, -4e-04,
0.0024, -3e-04, -0.0087, 0.0073, 0.0176, 0.0014, -0.0121, -0.005,
-0.006, -0.0059, 0.0159, 0.0273, -0.0071, -0.0163, 0.0041, 0.004,
-6e-04, 0.0095, 0.0012, 0.0156, 0.003, 0.0153, -0.0203, -0.0024,
0.0101, -0.0108, 0.0118, -0.0172, -0.0077, -2e-04, 0, 2e-04,
0.0287, -0.0023, 0.0096, 0.0043, -0.0016, -0.0169, 0.0221, 0.0208,
-0.0036, -0.0236, 0.014, 0.0047, 0.0023, 0, -0.0013, -0.0033,
0.0024, -0.0134, 0, 0.0081, -0.0085, -0.006, -0.0014, -0.0184,
-0.0066, 0.0103, -0.0123, -0.0022, 0.0021, 0.0059, -0.0014, -0.0046,
0.0124, -0.009, 0.0079, 0.0096, -0.0019, -0.0025, -0.0024, -0.0063,
-0.0139, -0.0094, 0.0152, -0.0145, 0.0061, 0.0055, 0.016, -0.0105,
0.0061, 0.0053, -0.0058, -0.0272, -0.0345, -0.0021, 0.0322, -0.0092,
-0.0022, 0.0154, -0.0243, 0.0072, 0.0078, 0.0152, -7e-04, -0.0081,
-0.0042, 0.0038, 0.0157, -0.0064, -0.0063, -3e-04, 0.0094, -0.0162,
-0.0045, -0.0122, -0.0062, -0.0081, 0.0112, -1e-04, -0.0059,
9e-04, 0.0043, -0.0081), .indexCLASS = "Date", tclass = c("POSIXct",
"POSIXt"), .indexTZ = "", tzone = "", class = c("xts", "zoo"), index = structure(c(1459728000,
1459814400, 1459900800, 1459987200, 1460073600, 1460332800, 1460419200,
1460505600, 1460592000, 1460678400, 1460937600, 1461024000, 1461110400,
1461196800, 1461283200, 1461542400, 1461628800, 1461715200, 1461801600,
1461888000, 1462147200, 1462233600, 1462320000, 1462406400, 1462492800,
1462752000, 1462838400, 1462924800, 1463011200, 1463097600, 1463356800,
1463443200, 1463529600, 1463616000, 1463702400, 1463961600, 1464048000,
1464134400, 1464220800, 1464307200, 1464566400, 1464652800, 1464739200,
1464825600, 1464912000, 1465171200, 1465257600, 1465344000, 1465430400,
1465516800, 1465776000, 1465862400, 1465948800, 1466035200, 1466121600,
1466380800, 1466467200, 1466553600, 1466640000, 1466726400, 1466985600,
1467072000, 1467158400, 1467244800, 1467331200, 1467590400, 1467676800,
1467763200, 1467849600, 1467936000, 1468195200, 1468281600, 1468368000,
1468454400, 1468540800, 1468800000, 1468886400, 1468972800, 1469059200,
1469145600, 1469404800, 1469491200, 1469577600, 1469664000, 1469750400,
1470009600, 1470096000, 1470182400, 1470268800, 1470355200, 1470614400,
1470700800, 1470787200, 1470873600, 1470960000, 1471219200, 1471305600,
1471392000, 1471478400, 1471564800, 1471824000, 1471910400, 1471996800,
1472083200, 1472169600, 1472428800, 1472515200, 1472601600, 1472688000,
1472774400, 1473033600, 1473120000, 1473206400, 1473292800, 1473379200,
1473638400, 1473724800, 1473811200, 1473897600, 1473984000, 1474243200,
1474329600, 1474416000, 1474502400, 1474588800, 1474848000, 1474934400,
1475020800, 1475107200, 1475193600, 1475452800, 1475539200, 1475625600,
1475712000, 1475798400, 1476057600, 1476144000, 1476230400, 1476316800,
1476403200, 1476662400, 1476748800, 1476835200, 1476921600, 1477008000,
1477267200, 1477353600, 1477440000, 1477526400, 1477612800, 1477872000,
1477958400, 1478044800, 1478131200, 1478217600, 1478476800, 1478563200,
1478649600, 1478736000, 1478822400, 1479081600, 1479168000, 1479254400,
1479340800, 1479427200, 1479686400, 1479772800, 1479859200, 1479945600,
1480032000, 1480291200, 1480377600, 1480464000, 1480550400, 1480636800,
1480896000, 1480982400, 1481068800, 1481155200, 1481241600, 1481500800,
1481587200, 1481673600, 1481760000, 1481846400, 1482105600, 1482192000,
1482278400, 1482364800, 1482451200, 1482710400, 1482796800, 1482883200,
1482969600, 1483056000, 1483315200, 1483401600, 1483488000, 1483574400,
1483660800, 1483920000, 1484006400, 1484092800, 1484179200, 1484265600,
1484524800, 1484611200, 1484697600, 1484784000, 1484870400, 1485129600,
1485216000, 1485302400, 1485388800, 1485475200, 1485734400, 1485820800,
1485907200, 1485993600, 1486080000, 1486339200, 1486425600, 1486512000,
1486598400, 1486684800, 1486944000, 1487030400, 1487116800, 1487203200,
1487289600, 1487548800, 1487635200, 1487721600, 1487808000, 1487894400,
1488153600, 1488240000, 1488326400, 1488412800, 1488499200, 1488758400,
1488844800, 1488931200, 1489017600, 1489104000, 1489363200, 1489449600,
1489536000, 1489622400, 1489708800, 1489968000, 1490054400, 1490140800,
1490227200, 1490313600, 1490572800, 1490659200, 1490745600, 1490832000
), tclass = "Date"), .Dim = c(259L, 2L), .Dimnames = list(NULL,
c("DHR.US", "GIS.US")))
So I dug up the code that is run when VaR is called with the parameters you specified. Please see below.
As you read my answer please note that this domain is out side of my skill set and the math is a bit over my head, however it appears that the major difference between your code and theirs is they sort the data and select the transposed cross product identified by alpha. Where as you use quantile. For some reason this means you select adjacent numbers - it appears to be basically a rounding error. Please see the following excel screen shot of your daily_whatif_scenario to illustrate this point:
Code for VaR:
VaR.historical.portfolio = function(R,p,w)
{
alpha = .setalphaprob(p)
portret = c();
T = dim(R)[1]
N = dim(R)[2]
for( t in c(1:T) ){
portret = c(portret,sum(w*as.numeric(R[t,])))
}
hVaR = -1* sort(portret)[floor(alpha*T)]
return(hVaR)
}
Where:
.setalphaprob = function (p)
{
if ( p >= 0.51 ) {
# looks like p was a percent like .99
alpha <- 1-p
} else {
alpha = p
}
return (alpha)
}
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')
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
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))