How to map a two-dimensional density plot onto a photograph [duplicate] - r

This question already has an answer here:
R: add alpha-value to png-image
(1 answer)
Closed 2 years ago.
I have a two-dimensional density plot which I need to overlay onto a photograph. I can read-in the picture with the package rtiff:
install.packages("rtiff")
library(rtiff)
x <- readTiff('F01_screenshot.tiff')
plot(x)
The data for the density plot is a very large dataframe called eet2 (see reproducible data below); the code for the density plot is this:
library(ggplot2)
ggplot(eet2, aes(x=X, y= Y)) +
geom_point() + stat_density2d()
I can easily produce both plots separately but don't know how to overlay the density plot onto the photograph in such a way that the density plot is scaled down proportionately to fit the picture. Any help is much appreciated!
Reproducible data:
eet2 <- dput(eet1[1:1000, 2:3])
structure(list(X = c(0, 177.378, 27.289, 0, 852.719, 0, 852.813,
71.068, 0, 144.875, 0, 140.385, 21.598, 0, 170.325, 0, 136.746,
21.038, 0, 146.279, 0, 141.86, 11.822, 0, 146.078, 0, 137.681,
21.182, 0, 148.867, 0, 132.886, 20.444, 0, 146.129, 0, 80.251,
6.688, 0, 141.08, 0, 149.789, 23.044, 0, 74.097, 0, 141.182,
21.72, 0, 83.075, 0, 81.192, 6.766, 0, 849.784, 0, 153.96, 23.686,
0, 78.374, 0, 142.782, 21.967, 0, 72.929, 0, 142.922, 11.91,
0, 83.639, 0, 143.912, 22.14, 0, 134.809, 0, 142.826, 21.973,
0, 132.7, 0, 85.876, 7.156, 0, 138.935, 0, 80.951, 12.454, 0,
144.385, 0, 141.716, 21.802, 0, 76.768, 0, 74.406, 6.2, 0, 134.444,
0, 155.341, 23.899, 0, 189.336, 0, 224.517, 34.541, 0, 207.46,
0, 216.122, 18.01, 0, 204.552, 0, 208.524, 32.081, 0, 207.513,
0, 203.162, 31.256, 0, 197.578, 0, 204.362, 17.03, 0, 195.223,
0, 956.396, 147.138, 0, 201.969, 0, 224.989, 34.614, 0, 214.064,
0, 140.374, 11.698, 0, 140.235, 0, 958.501, 147.462, 0, 141.898,
0, 143.337, 22.052, 0, 955.901, 0, 954.965, 79.58, 0, 131.323,
0, 223.214, 34.341, 0, 952.203, 0, 143.102, 22.016, 0, 935.525,
0, 918.238, 76.52, 0, 123.337, 0, 127.93, 19.682, 0, 915.755,
0, 128.336, 19.744, 0, 913.158, 0, 120.076, 10.006, 0, 911.196,
0, 124.387, 19.136, 0, 911.631, 0, 911.389, 140.214, 0, 910.433,
0, 119.375, 9.948, 0, 115.898, 0, 910.073, 140.011, 0, 915.461,
0, 965.025, 148.465, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1125.565,
0, 1074.389, 165.291, 0, 1061.611, 0, 1051.23, 161.728, 0, 1044.3,
0, 269.733, 22.478, 0, 245.869, 0, 206.934, 234.525, 245.609,
250.874, 251.576, 260.164, 262.974, 263.161, 269.25, 262.693,
261.818, 265.634, 266.479, 266.592, 266.39, 262.751, 262.508,
265.093, 262.666, 262.343, 1059.856, 1075.848, 1077.98, 1068.255,
1065.442, 1065.255, 1071.821, 1072.292, 1072.355, 1073.788, 1072.572,
1072.41, 1074.295, 1074.487, 1074.499, 1078.434, 1080.074, 1080.293,
1081.104, 1080.014, 1079.869, 1080.261, 1080.181, 1080.175, 1078.568,
1079.422, 1079.536, 1078.84, 1077.459, 1077.275, 1077.805, 1076.787,
1076.719, 1077.199, 1076.045, 1075.891, 1077.362, 1076.663, 1076.569,
1077.942, 1078.842, 1078.902, 1078.713, 1080.023, 1080.198, 1080.764,
1081.553, 1081.659, 1081.946, 1082.445, 1082.479, 1083.987, 1084.113,
1084.13, 1084.741, 1085.809, 1085.952, 1085.031, 1084.708, 1084.686,
1085.901, 1085.266, 1085.181, 1085.513, 1085.159, 1085.112, 1088.024,
1088.29, 1088.308, 1088.889, 1088.327, 1088.252, 1069.543, 1065.069,
1064.472, 1067.409, 1068.075, 1068.119, 1068.377, 1070.019, 1070.238,
1072.384, 1071.454, 1071.33, 1072.31, 1069.704, 1069.53, 1068.856,
1073.986, 1074.67, 1075.824, 1077.42, 1077.633, 1080.095, 1082.62,
1082.788, 1087.112, 1090.026, 1090.414, 1094.654, 1098.678, 1099.215,
1103.801, 1097.674, 1097.266, 1110.129, 1105.4, 1104.769, 1115.638,
1115.922, 1115.96, 1115.612, 1115.278, 1115.256, 1114.682, 1112.96,
1112.73, 1063.573, 948.249, 932.873, 844.512, 834.23, 833.545,
865.203, 867.402, 867.695, 838.499, 864.3, 867.74, 837.215, 864.187,
865.985, 0, 760.653, 862.073, 861.199, 860.757, 860.699, 0, 861.681,
71.807, 0, 851.287, 855.415, 855.965, 859.554, 864.523, 865.185,
868.109, 873.22, 873.561, 845.881, 882.877, 887.81, 893.391,
899.782, 900.634, 908.492, 56.781, 0, 0, 0, 0, 0, 0, 0, 319.09,
0, 0, 0, 0, 0, 0, 0, 0, 0, 696.905, 0, 704.008, 58.667, 0, 0,
0, 0, 677.276, 673.676, 673.195, 673.45, 671.209, 671.06, 672.474,
674.07, 674.283, 674.896, 656.33, 653.855, 678.654, 681.884,
682.099, 684.933, 715.507, 719.584, 735.659, 1290.244, 1364.188,
1362.632, 1366.322, 1366.568, 1371.973, 1364.1, 1363.051, 1361.383,
812.325, 739.117, 734.716, 1322.199, 1361.365, 730.422, 731.132,
731.227, 729.084, 724.164, 723.507, 726.199, 725.939, 725.922,
725.437, 1278.917, 1352.714, 726.791, 1281.431, 1355.383, 1366.558,
764.435, 724.294, 725.46, 724.544, 724.422, 724.178, 1288.932,
1364.232, 723.902, 680.495, 677.601, 724.172, 722.856, 722.681,
735.236, 743.706, 744.835, 554.779, 735.387, 747.427, 724.914,
720.25, 719.628, 721.122, 742.971, 745.884, 742.171, 737.947,
737.665, 729.207, 808.032, 818.542, 905.216, 106.496, 0, 1169.534,
0, 1195.852, 99.654, 0, 1236.396, 0, 0, 0, 0, 1190.473, 183.15,
0, 1143.686, 1132.754, 1132.025, 1123.856, 1103.6, 1100.9, 1083.716,
1079.191, 1078.588, 1083.785, 1070.395, 1069.503, 1066.885, 1065.121,
1064.885, 1064.401, 1065.285, 1065.403, 1075.009, 1076.036, 1076.105,
1086.308, 1075.821, 1074.423, 1073.282, 126.268, 0, 1092.281,
0, 1019.684, 1087.663, 286.389, 33.693, 0, 1117.064, 0, 1114.254,
171.424, 0, 1111.518, 0, 1110.347, 92.529, 0, 1077.293, 0, 1107.754,
170.424, 0, 1107.136, 0, 1076.571, 165.626, 0, 1067.475, 0, 1110.71,
92.559, 0, 274.852, 0, 280.654, 43.177, 0, 280.278, 271.773,
270.639, 0, 1060.434, 88.369, 0, 1054.964, 1047.876, 1046.931,
1052.977, 1064.93, 1066.524, 1061.181, 1069.339, 1069.883, 275.888,
970.662, 1063.299, 268.308, 974.232, 1068.355, 1044.757, 1065.286,
1066.655, 1076.68, 1085.54, 1086.721, 1088.216, 1083.409, 1082.768,
1087.272, 1087.283, 1087.283, 1086.689, 1087.19, 1087.257, 1089.988,
1087.837, 1087.55, 272.953, 1040.007, 1091.144, 1076.712, 1089.387,
1091.077, 1103.104, -360.56, -555.715, 1072.081, 1073.131, 1073.201,
1073.04, 1089.257, 1091.419, 1072.888, 1073.111, 1073.141, 1104.182,
1075.399, 1073.48, 1103.711, 1076.133, 1072.456, 1104.247, 1105.181,
1105.305, 1072.744, 1104.02, 1106.105, 1074.673, 1102.759, 1106.503,
1076.082, 1104.401, 1108.177, 1078.025, 1109.772, 1111.888, 288.949,
292.278, 292.722, 1112.981, 1123.139, 1124.493, 1127.996, 1134.943,
1135.406, 1136.071, 1141.749, 1142.506, 1148.754, 1150.764, 1151.032,
1156.093, 1154.597, 1154.497, 1150.481, 1124.218, 1120.716, 1108.646,
1095.181, 1093.385, 1113.175, 1089.783, 1088.224, 1719.795, -286.164,
-553.625, 1112.49, 1099.064, 1097.274, 1106.601, 1105.705, 1105.645,
1100.75, 1110.143, 1111.395, 1114.044, 1108.662, 1107.944, 1115.615,
1114.492, 1114.417, 296.606, 1011.559, 1106.886, 213.15, 735.037,
804.622, 643.816, 590.868, 587.338, 588.443, 591.022, 591.366,
595.437, 598.575, 598.994, -70.23, 562.977, 605.191, 608.988,
14.755, -64.476, -86.326, 490.189, 567.057, 573.015, 572.841,
572.83, 576.607, 578.243, 578.461, 579.714, 1343.595, 1445.446,
585.277, 588.153, 588.345, 588.809, 588.843, 588.848, 592.898,
591.447, 591.254, 592.538, 589.623, 589.429, 590.233, 587.901,
587.59, 589.276, 603.526, 605.426, 691.889, 810.649, 818.566,
889.948, 866.466, 863.335, 164.367, 164.895, 164.966, 158.056,
851.73, 897.975, 164.51, 806.786, 892.423, 894.069, 892.465,
892.251, 153.408, 842.909, 888.876, 151.935, 802.889, 889.683,
154.817, 146.446, 145.329, 888.428, 193.529, 147.202, 885.088,
224.817, 136.78, 147.255, 802.581, 889.958, 888.64, 889.975,
890.064, 1575.971, 968.922, 887.982, 889.232, 860.991, 857.226,
872.007, 893.987, 895.452, 859.373, 892.292, 896.681, 859.84,
859.942, 859.955, 898.274, 860.41, 857.885, 879.17, 862.76, 860.572,
856.434, 854.683, 854.45, 851.949, 174.027, 128.832, 848.35,
847.791, 847.716, 847.847, 849.008, 849.163, 133.198, 806.744,
851.647, 146.291, 773.622, 857.267, 860.622, 834.937, 831.512,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 623.517, 0, 631.712, 97.187,
0, 631.526, 0, 641.912, 98.756, 0, 648.161, 0, 654.673, 54.556,
0, 659.733, 0, 603.775, 684.278, 696.548, 689.181, 688.198, 687.25,
682.216, 681.881, 711.089, 713.482, 713.802, 713.969, 715.124,
715.278, 711.369, 709.299, 709.162, 679.428, 675.016, 674.428,
675.948, 673.724, 673.427, 675.195, 678.8, 679.041, 681.486,
686.341, 686.988, 696.445, 694.867, 694.657, 703.31, 705.003,
705.116, 0, 711.882, 109.52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1269.549, 1438.822, 0, 720.784, 60.065, 0, 721.678, 0, 713.535,
109.775, 0, 709.244, 0, 681.534, 104.851, 0, 693.092, 0, 681.394,
56.783, 0, 679.538, 0, 680.866, 104.749, 0, 675.661, 0, 668.345,
102.822, 0, 0, 0, 0, 0, -69.364, -10.671, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 677.983, 0), Y = c(0, -560.914,
-86.294, 0, 12.57, 0, 10.336, 0.861, 0, -569.851, 0, -582.641,
-89.637, 0, -513.461, 0, -582.556, -89.624, 0, -567.846, 0, -574.869,
-47.906, 0, -567.25, 0, -571.902, -87.985, 0, -554.095, 0, -574.651,
-88.408, 0, -557.127, 0, -365.662, -30.472, 0, -565.123, 0, -564.378,
-86.827, 0, -380.282, 0, -587.338, -90.36, 0, -373.494, 0, -394.186,
-32.849, 0, -15.305, 0, -586.833, -90.282, 0, -391.571, 0, -603.611,
-92.863, 0, -392.659, 0, -595.738, -49.645, 0, -369.805, 0, -585.56,
-90.086, 0, -607.653, 0, -598.77, -92.118, 0, -603.821, 0, -362.726,
-30.227, 0, -590.569, 0, -365.806, -56.278, 0, -584.274, 0, -594.203,
-91.416, 0, -378.246, 0, -385.426, -32.119, 0, -585.678, 0, -569.332,
-87.589, 0, -572.327, 0, -566.971, -87.226, 0, -586.06, 0, -584.482,
-48.707, 0, -593.944, 0, -583.298, -89.738, 0, -580.776, 0, -590.48,
-90.843, 0, -600.616, 0, -598.535, -49.878, 0, -602.826, 0, -29.44,
-4.529, 0, -618.062, 0, -592.995, -91.23, 0, -609.041, 0, -411.878,
-34.323, 0, -413.262, 0, -52.608, -8.094, 0, -411.569, 0, -415.928,
-63.989, 0, -48.63, 0, -47.12, -3.927, 0, -415.299, 0, -599.916,
-92.295, 0, -44.314, 0, -411.139, -63.252, 0, -46.124, 0, -48.509,
-4.042, 0, -407.485, 0, -413.499, -63.615, 0, -55.391, 0, -415.922,
-63.988, 0, -57.45, 0, -422.827, -35.236, 0, -54.607, 0, -414.86,
-63.825, 0, -51.45, 0, -48.986, -7.536, 0, -45.942, 0, -413.839,
-34.487, 0, -411.075, 0, -38.956, -5.993, 0, -14.525, 0, 35.454,
5.455, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 279.172, 0, 315.145,
48.484, 0, 313.523, 0, 314.854, 48.439, 0, 314.166, 0, -89.369,
-7.447, 0, -125.57, 0, -138.883, -157.401, -162.487, -158.14,
-157.561, -171.662, -160.906, -160.189, -166.883, -171.154, -171.724,
-187.379, -175.901, -174.37, -188.734, -185.215, -184.98, -189.818,
-189.088, -188.991, 283.22, 302.83, 305.444, 270.066, 289.663,
290.969, 275.119, 276.953, 277.198, 260.347, 261.677, 261.855,
246.632, 250.916, 251.202, 238.808, 246.008, 246.968, 240.285,
253.356, 255.099, 251.122, 262.086, 262.817, 258.677, 268.433,
269.734, 264.812, 273.771, 274.966, 268.696, 276.112, 276.606,
268.26, 275.135, 276.051, 265.545, 272.265, 273.161, 264.077,
268.116, 268.385, 261.015, 266.217, 266.911, 259.724, 265.892,
266.714, 259.602, 265.93, 266.352, 258.209, 265.537, 266.514,
259.411, 265.687, 266.523, 259.761, 266.978, 267.459, 260.97,
268.01, 268.949, 262.4, 271.867, 273.13, 264.803, 273.569, 274.153,
266.979, 275.25, 276.353, 268.073, 274.926, 275.84, 269.625,
279.424, 280.077, 271.785, 281.741, 283.068, 276.083, 284.162,
285.239, 279.865, 291.741, 292.533, 291.52, 302.124, 303.538,
300.685, 313.141, 314.802, 313.594, 327.048, 327.945, 325.593,
339.796, 341.69, 341.58, 355.702, 357.585, 356.602, 381.738,
383.413, 370.869, 394.752, 397.936, 391.659, 409.376, 411.739,
415.584, 433.281, 434.461, 437.478, 454.368, 456.62, 445.173,
427.865, 425.558, 383.004, 370.373, 369.531, 382.343, 399.016,
401.239, 413.831, 422.157, 423.267, 432.015, 448.845, 449.967,
0, 425.956, 482.75, 487.1, 501.178, 503.055, 0, 508.065, 42.339,
0, 492.242, 494.125, 494.376, 484.667, 484.368, 484.328, 467.742,
464.267, 464.035, 468.697, 433.854, 429.208, 400.85, 393.727,
392.777, 376.735, 23.546, 0, 0, 0, 0, 0, 0, 0, 885.955, 0, 0,
0, 0, 0, 0, 0, 0, 0, 500.323, 0, 481.941, 40.162, 0, 0, 0, 0,
530.014, 516.432, 514.621, 494.618, 478.341, 477.256, 456.114,
458.922, 459.297, 456.514, 425.5, 421.365, 462.707, 472.408,
473.055, 467.768, 454.51, 452.742, 441.741, 201.307, 169.249,
167.631, 163.795, 163.539, 160.509, 161.775, 161.943, 161.298,
397.997, 429.557, 419.546, 177.979, 161.875, 418.022, 423.34,
424.049, 413.55, 419.645, 420.457, 407.472, 415.496, 416.031,
407.006, 148.509, 114.042, 409.442, 164.128, 131.419, 109.849,
407.293, 427.122, 418.844, 426.465, 427.481, 417.779, 161.317,
127.123, 409.12, 367.918, 365.171, 395.673, 396.298, 396.381,
388.306, 392.859, 393.466, 473.545, 400.213, 395.325, 366.771,
370.934, 371.489, 339.469, 387.501, 393.905, 386.022, 390.883,
391.207, 378.721, 394.352, 396.436, 384.581, 45.245, 0, 380.042,
0, 375.485, 31.29, 0, 335.187, 0, 0, 0, 0, 323.048, 49.7, 0,
359.169, 361.068, 361.195, 348.111, 362.176, 364.051, 383.932,
392.518, 393.663, 349.699, 392.668, 395.532, 391.36, 396.741,
397.458, 387.906, 391.383, 391.846, 371.849, 378.048, 378.461,
318.908, 357.694, 362.865, 342.973, 40.35, 0, 288.149, 0, 259.291,
276.577, -102.466, -12.055, 0, 241.977, 0, 234.288, 36.044, 0,
247.849, 0, 244.967, 20.414, 0, 290.17, 0, 258.996, 39.846, 0,
269.854, 0, 259.002, 39.847, 0, 297.18, 0, 266.569, 22.214, 0,
-87.029, 0, -89.617, -13.787, 0, -95.348, -79.043, -76.869, 0,
246.968, 20.581, 0, 272.257, 289.812, 292.152, 271.933, 287.652,
289.748, 276.443, 284.481, 285.017, -116.323, 230.711, 276.983,
-141.084, 229.411, 278.81, 314.961, 280.304, 277.994, 277.686,
282.62, 283.278, 287.306, 282.107, 281.414, 290.288, 291.936,
292.045, 288.296, 302.772, 304.702, 292.15, 302.242, 303.588,
-136.669, 270.271, 297.4, 274.154, 301.517, 305.165, 290.369,
-426.184, -521.724, 321.366, 329.046, 329.558, 321.7, 306.849,
304.869, 321.009, 327.981, 328.91, 290.339, 328.378, 330.914,
289.022, 325.172, 329.992, 288.447, 295.19, 296.089, 322.57,
298.091, 296.459, 321.394, 300.861, 298.123, 320.881, 300.679,
297.985, 319.966, 296.464, 294.897, -164.893, -160.079, -159.437,
278.924, 281.357, 281.682, 269.259, 274.752, 275.118, 267.165,
265.143, 264.873, 258.509, 264.614, 265.429, 256.744, 266.259,
266.893, 263.361, 301.473, 306.555, 262.416, 313.245, 320.022,
281.712, 320.535, 323.123, -194.846, -521.034, -564.526, 274.367,
278.267, 278.787, 264.133, 268.555, 268.85, 256.818, 266.194,
267.444, 260.084, 265.714, 266.464, 262.669, 272.063, 272.689,
-175.62, 225.568, 279.06, -159.431, 216.327, 266.428, 245.597,
254.043, 254.606, 244.851, 255.798, 257.258, 248.152, 256.508,
257.622, -319.948, 219.787, 255.769, 243.947, -235.689, -299.64,
-334.288, 164.071, 230.518, 224.188, 233.409, 234.024, 228.544,
236.363, 237.405, 235.462, -116.121, -162.999, 240.889, 251.796,
252.524, 246.8, 258.72, 260.309, 257.726, 267.828, 269.175, 265.087,
277.995, 278.856, 273.876, 287.09, 288.852, 280.858, 292.608,
294.174, 246.387, 213.675, 211.494, 201.469, 209.375, 210.429,
-217.38, -218.456, -218.6, -232.09, 196.612, 225.193, -248.699,
166.3, 221.633, 213.54, 219.095, 219.836, -261.438, 192.469,
222.729, -265.48, 164.234, 221.529, -254.99, -254.199, -254.094,
209.197, -218.172, -246.663, 207.52, -196.823, -250.735, -261.477,
163.4, 220.05, 210.838, 222.305, 223.069, -196.463, 176.753,
226.515, 217.024, 249.012, 253.277, 203.798, 226.753, 228.283,
241.225, 228.683, 227.011, 235.665, 243.606, 244.664, 212.621,
240.812, 242.692, 207.285, 207.473, 207.498, 194.745, 202.346,
203.359, 191.408, -221.759, -249.304, 186.022, 192.304, 193.141,
182.101, 191.751, 193.037, -260.225, 162.901, 191.109, -252.884,
141.317, 193.877, 186.754, 220.72, 225.249, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 304.742, 0, 316.744, 48.73, 0, 334.833, 0,
352.419, 54.218, 0, 368.749, 0, 378.178, 31.515, 0, 378.548,
0, 339.912, 385.233, 406.951, 373.432, 368.963, 411.64, 362.786,
359.529, 358.897, 353.738, 353.051, 336.054, 340.344, 340.915,
335.944, 355.461, 356.762, 345.061, 372.321, 375.955, 376.513,
404.13, 407.812, 416.338, 426.28, 426.943, 432.67, 429.483, 429.058,
426.368, 425.226, 425.074, 416.298, 411.677, 411.369, 0, 345.327,
53.127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 183.69, 208.182, 0,
261.561, 21.797, 0, 298.1, 0, 281.941, 43.375, 0, 314.226, 0,
304.413, 46.833, 0, 374.171, 0, 368.525, 30.71, 0, 359.118, 0,
367.905, 56.601, 0, 287.494, 0, 285.213, 43.879, 0, 0, 0, 0,
0, 735.244, 113.115, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 304.619, 0)), row.names = 31537:32536, class = "data.frame")

This should give you a way to get the image you want. However, I don't have your original TIFF, and therefore the alignment won't be correct here since I had to use a cut-and-pasted version of the png in your question.
Anyway, the method I would use is:
Convert the image to a raster
Convert the raster to a grid::rasterGrob
Plot the rasterGrob as your first layer in ggplot using annotation_custom
Plot your other layers as normal.
Here's an example:
library(ggplot2)
library(rtiff)
library(grid)
x <- readTiff('F01_screenshot.tiff')
pic <- as.raster(array(c(x#red, x#green, x#blue), c(x#size, 3)))
picgrob <- rasterGrob(pic)
ggplot(eet2, aes(x=X, y= Y)) +
annotation_custom(picgrob) +
geom_point() +
stat_density2d() +
coord_equal()
You may need to scale your Y axis data to make it match the aspect ratio of the picture.
As an example, if we assume max(eet2$Y) is the top edge of the image, and min(eet2$Y) the bottom edge, and also assume that min(eet2$X) is the left edge and max(eet2$X) the right edge (as you have suggested is the case in your comments), we can marry the picture to the data like this:
pic_ratio <- dim(pic)[2]/dim(pic)[1]
data_ratio <- diff(range(eet2$X)) / diff(range(eet2$Y))
eet2$Y <- eet2$Y * data_ratio / pic_ratio
ggplot(eet2, aes(x=X, y= Y)) +
annotation_custom(picgrob) +
geom_point() +
stat_density2d() +
coord_equal(xlim = range(eet2$X), ylim = range(eet2$Y))
If this alignment is not correct, then we need extra calibration information not present in the data (i.e. what value of eet2$Y should represent the top and bottom of the image, and what value of eet2$X represents the left and right edges.

Related

RC Bray function not accepting phyloseq otu_table as argument

I am using James Stegen et al's code here to calculate an abundance-weighted raup-crick value for my 16S dataset.
I load in my phyloseq object then extract the otu_table. I then use the otu_table as the spXsite argument in the function raup_crick_abundance(). My otu_table is available as a dput() below the text.
physeq<-qza_to_phyloseq(
features="~/Documents/qiime2-analyses/CRD/fresh_run/table.qza",
tree="~/Documents/qiime2-analyses/CRD/fresh_run/rooted-tree.qza",
"~/Documents/qiime2-analyses/CRD/fresh_run/taxonomy.qza",
metadata = "crd-metadata.txt")
otu_table <- (otu_table(physeq))
raup_crick_abundance = function(spXsite=otu_table, plot_names_in_col1=TRUE,
reps=9999, set_all_species_equal=FALSE,
as.distance.matrix=TRUE, report_similarity=FALSE){
Where the remaining code is verbatim from the github link above.
I am having a hard time understanding the error I have been receiving:
Error in sample.int(x, size, replace, prob) :
incorrect number of probabilities
Called from: sample.int(x, size, replace, prob)
Browse[1]> traceback()
1: raup_crick_abundance()
I had thought perhaps the function was looking for an equivalent number of columns and rows, but that was not the case. I searched the function sample() and think it's hoping to select values from my argument otu_table but can't find the expected range?
The sample() causing problems is on line 48 of the github where I believe the function is weighing the otu occurrences and entering the number of occurrences into a column.
##two empty null communities of size gamma:
com1<-rep(0,gamma)
com2<-rep(0,gamma)
##add observed number of species to com1, weighting by species occurrence frequencies:
com1[sample(1:gamma, sum(spXsite.inc[null.one,]), replace=FALSE, prob=occur)]<-1
com1.samp.sp = sample(which(com1>0),(sum(spXsite[null.one,])-sum(com1)),replace=TRUE,prob=abundance[which(com1>0)]);
com1.samp.sp = cbind(com1.samp.sp,1); # head(com1.samp.sp);
com1.sp.counts = as.data.frame(tapply(com1.samp.sp[,2],com1.samp.sp[,1],FUN=sum)); colnames(com1.sp.counts) = 'counts'; # head(com1.sp.counts);
com1.sp.counts$sp = as.numeric(rownames(com1.sp.counts)); # head(com1.sp.counts);
com1[com1.sp.counts$sp] = com1[com1.sp.counts$sp] + com1.sp.counts$counts; # com1;
#sum(com1) - sum(spXsite[null.one,]); ## this should be zero if everything work properly
rm('com1.samp.sp','com1.sp.counts');
Any thoughts are greatly appreciated. Thank you in advance!
> dput(otu_table)
new("otu_table", .Data = structure(c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0,
0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 4), .Dim = c(6L, 48L), .Dimnames = list(
c("e54924083c02bd088c69537d02406eb8", "3112899fc7a2adb7f74a081a82c7cde4",
"db5d745b02355b6fed513c4953b62326", "2faf2046aa9ab2f6598df79cd52e9c7b",
"bec8db81cc8ec453136c82ede8327a9f", "601e47b8adcbd21d159f74421710e1b5"
), c("sample-10", "sample-11", "sample-12", "sample-14",
"sample-16", "sample-18", "sample-19", "sample-20", "sample-21",
"sample-22", "sample-23", "sample-24", "sample-25", "sample-26",
"sample-27", "sample-28", "sample-29", "sample-30", "sample-31",
"sample-32", "sample-33", "sample-34", "sample-35", "sample-36",
"sample-37", "sample-40", "sample-41", "sample-43", "sample-44",
"sample-45", "sample-46", "sample-50", "sample-55", "sample-56",
"sample-57", "sample-58", "sample-59", "sample-61", "sample-62",
"sample-63", "sample-64", "sample-65", "sample-67", "sample-68",
"sample-69", "sample-70", "sample-71", "sample-8"))), taxa_are_rows = TRUE)
>

ggplot with two manual color scales

I am trying to reuse some old code which I have used to make two separate plots in past, and would like to pout it together into one now.
However I have few problems
color_var <- vector(mode = "double",length = length(OP_2016$risk))
color_var[color_var== '0']<- NA
color_var[OP_2016$risk>=1 & OP_2016$risk<12] <- "yellow"
color_var[OP_2016$risk>=12] <- "red"
ggplot(OP_2016)+
geom_col(aes(x = short_date, y = risk, color = color_var , group = 1), size= 0.9) +
scale_y_continuous(limits = c(0, 100),name = "Accumulated EBHours")+
scale_color_identity("Risk Level", breaks= levels(as.factor(color_var))[c(1,2)],
labels = c("High >12 EBH","Medium 0-12EBH"),
guide = "legend"
)+
geom_line(aes(x = short_date, y= 12), linetype= "dotted", size = 0.8, colour = "red")+
# scale_color_manual("Varieties", values =c( "British Queen"= "orchid1"))+
geom_line(data = dis_fun_df, aes(x= date, y = rating, colour = "green"))
Problems:
Bars and and boxes in the legend are not filled,
I can not add manual color for geom_line and add it to the legend, that I have added from other plot.
Apologies, data set to reproduce the plot is a bit big.
dis_fun_df <- structure(list(date = structure(c(15534, 15540, 15548, 15555,
15562, 15573, 15580), class = "Date"), rating = c(10.2, 30, 61.6666666666667,
81.6666666666667, 95.8333333333333, 99.1666666666667, 100)), row.names = c(NA,
-7L), class = c("tbl_df", "tbl", "data.frame"))
OP_2016 <- structure(list(date = structure(c(1342224000, 1342227600, 1342231200,
1342234800, 1342238400, 1342242000, 1342245600, 1342249200, 1342252800,
1342256400, 1342260000, 1342263600, 1342267200, 1342270800, 1342274400,
1342278000, 1342281600, 1342285200, 1342288800, 1342292400, 1342296000,
1342299600, 1342303200, 1342306800, 1342310400, 1342314000, 1342317600,
1342321200, 1342324800, 1342328400, 1342332000, 1342335600, 1342339200,
1342342800, 1342346400, 1342350000, 1342353600, 1342357200, 1342360800,
1342364400, 1342368000, 1342371600, 1342375200, 1342378800, 1342382400,
1342386000, 1342389600, 1342393200, 1342396800, 1342400400, 1342404000,
1342407600, 1342411200, 1342414800, 1342418400, 1342422000, 1342425600,
1342429200, 1342432800, 1342436400, 1342440000, 1342443600, 1342447200,
1342450800, 1342454400, 1342458000, 1342461600, 1342465200, 1342468800,
1342472400, 1342476000, 1342479600, 1342483200, 1342486800, 1342490400,
1342494000, 1342497600, 1342501200, 1342504800, 1342508400, 1342512000,
1342515600, 1342519200, 1342522800, 1342526400, 1342530000, 1342533600,
1342537200, 1342540800, 1342544400, 1342548000, 1342551600, 1342555200,
1342558800, 1342562400, 1342566000, 1342569600, 1342573200, 1342576800,
1342580400, 1342584000, 1342587600, 1342591200, 1342594800, 1342598400,
1342602000, 1342605600, 1342609200, 1342612800, 1342616400, 1342620000,
1342623600, 1342627200, 1342630800, 1342634400, 1342638000, 1342641600,
1342645200, 1342648800, 1342652400, 1342656000, 1342659600, 1342663200,
1342666800, 1342670400, 1342674000, 1342677600, 1342681200, 1342684800,
1342688400, 1342692000, 1342695600, 1342699200, 1342702800, 1342706400,
1342710000, 1342713600, 1342717200, 1342720800, 1342724400, 1342728000,
1342731600, 1342735200, 1342738800, 1342742400, 1342746000, 1342749600,
1342753200, 1342756800, 1342760400, 1342764000, 1342767600, 1342771200,
1342774800, 1342778400, 1342782000, 1342785600, 1342789200, 1342792800,
1342796400, 1342800000, 1342803600, 1342807200, 1342810800, 1342814400,
1342818000, 1342821600, 1342825200, 1342828800, 1342832400, 1342836000,
1342839600, 1342843200, 1342846800, 1342850400, 1342854000, 1342857600,
1342861200, 1342864800, 1342868400, 1342872000, 1342875600, 1342879200,
1342882800, 1342886400, 1342890000, 1342893600, 1342897200, 1342900800,
1342904400, 1342908000, 1342911600, 1342915200, 1342918800, 1342922400,
1342926000, 1342929600, 1342933200, 1342936800, 1342940400, 1342944000,
1342947600, 1342951200, 1342954800, 1342958400, 1342962000, 1342965600,
1342969200, 1342972800, 1342976400, 1342980000, 1342983600, 1342987200,
1342990800, 1342994400, 1342998000, 1343001600, 1343005200, 1343008800,
1343012400, 1343016000, 1343019600, 1343023200, 1343026800, 1343030400,
1343034000, 1343037600, 1343041200, 1343044800, 1343048400, 1343052000,
1343055600, 1343059200, 1343062800, 1343066400, 1343070000, 1343073600,
1343077200, 1343080800, 1343084400, 1343088000, 1343091600, 1343095200,
1343098800, 1343102400, 1343106000, 1343109600, 1343113200, 1343116800,
1343120400, 1343124000, 1343127600, 1343131200, 1343134800, 1343138400,
1343142000, 1343145600, 1343149200, 1343152800, 1343156400, 1343160000,
1343163600, 1343167200, 1343170800, 1343174400, 1343178000, 1343181600,
1343185200, 1343188800, 1343192400, 1343196000, 1343199600, 1343203200,
1343206800, 1343210400, 1343214000, 1343217600, 1343221200, 1343224800,
1343228400, 1343232000, 1343235600, 1343239200, 1343242800, 1343246400,
1343250000, 1343253600, 1343257200, 1343260800, 1343264400, 1343268000,
1343271600, 1343275200, 1343278800, 1343282400, 1343286000, 1343289600,
1343293200, 1343296800, 1343300400, 1343304000, 1343307600, 1343311200,
1343314800, 1343318400, 1343322000, 1343325600, 1343329200, 1343332800,
1343336400, 1343340000, 1343343600, 1343347200, 1343350800, 1343354400,
1343358000, 1343361600, 1343365200, 1343368800, 1343372400, 1343376000,
1343379600, 1343383200, 1343386800, 1343390400, 1343394000, 1343397600,
1343401200, 1343404800, 1343408400, 1343412000, 1343415600, 1343419200,
1343422800, 1343426400, 1343430000, 1343433600, 1343437200, 1343440800,
1343444400, 1343448000, 1343451600, 1343455200, 1343458800, 1343462400,
1343466000, 1343469600, 1343473200, 1343476800, 1343480400, 1343484000,
1343487600, 1343491200, 1343494800, 1343498400, 1343502000, 1343505600,
1343509200, 1343512800, 1343516400, 1343520000, 1343523600, 1343527200,
1343530800, 1343534400, 1343538000, 1343541600, 1343545200, 1343548800,
1343552400, 1343556000, 1343559600, 1343563200, 1343566800, 1343570400,
1343574000, 1343577600, 1343581200, 1343584800, 1343588400, 1343592000,
1343595600, 1343599200, 1343602800, 1343606400, 1343610000, 1343613600,
1343617200, 1343620800, 1343624400, 1343628000, 1343631600, 1343635200,
1343638800, 1343642400, 1343646000, 1343649600, 1343653200, 1343656800,
1343660400, 1343664000, 1343667600, 1343671200, 1343674800, 1343678400,
1343682000, 1343685600, 1343689200, 1343692800, 1343696400, 1343700000,
1343703600, 1343707200, 1343710800, 1343714400, 1343718000, 1343721600,
1343725200, 1343728800, 1343732400, 1343736000, 1343739600, 1343743200,
1343746800, 1343750400, 1343754000, 1343757600, 1343761200, 1343764800,
1343768400, 1343772000, 1343775600, 1343779200, 1343782800, 1343786400,
1343790000, 1343793600, 1343797200, 1343800800, 1343804400, 1343808000,
1343811600, 1343815200, 1343818800, 1343822400, 1343826000, 1343829600,
1343833200, 1343836800, 1343840400, 1343844000, 1343847600, 1343851200,
1343854800, 1343858400, 1343862000, 1343865600, 1343869200, 1343872800,
1343876400, 1343880000, 1343883600, 1343887200, 1343890800, 1343894400,
1343898000, 1343901600, 1343905200, 1343908800, 1343912400, 1343916000,
1343919600, 1343923200, 1343926800, 1343930400, 1343934000, 1343937600,
1343941200, 1343944800, 1343948400, 1343952000, 1343955600, 1343959200,
1343962800, 1343966400, 1343970000, 1343973600, 1343977200, 1343980800,
1343984400, 1343988000, 1343991600, 1343995200, 1343998800, 1344002400,
1344006000, 1344009600, 1344013200, 1344016800, 1344020400, 1344024000,
1344027600, 1344031200, 1344034800, 1344038400, 1344042000, 1344045600,
1344049200, 1344052800, 1344056400, 1344060000, 1344063600, 1344067200,
1344070800, 1344074400, 1344078000, 1344081600, 1344085200, 1344088800,
1344092400, 1344096000, 1344099600, 1344103200, 1344106800, 1344110400,
1344114000, 1344117600, 1344121200, 1344124800, 1344128400, 1344132000,
1344135600, 1344139200, 1344142800, 1344146400, 1344150000, 1344153600,
1344157200, 1344160800, 1344164400, 1344168000, 1344171600, 1344175200,
1344178800, 1344182400, 1344186000, 1344189600, 1344193200, 1344196800,
1344200400, 1344204000, 1344207600, 1344211200, 1344214800, 1344218400,
1344222000, 1344225600, 1344229200, 1344232800, 1344236400, 1344240000,
1344243600, 1344247200, 1344250800, 1344254400, 1344258000, 1344261600,
1344265200, 1344268800, 1344272400, 1344276000, 1344279600, 1344283200,
1344286800, 1344290400, 1344294000, 1344297600, 1344301200, 1344304800,
1344308400, 1344312000, 1344315600, 1344319200, 1344322800, 1344326400,
1344330000, 1344333600, 1344337200, 1344340800, 1344344400, 1344348000,
1344351600, 1344355200, 1344358800, 1344362400, 1344366000, 1344369600,
1344373200, 1344376800, 1344380400, 1344384000, 1344387600, 1344391200,
1344394800, 1344398400, 1344402000, 1344405600, 1344409200, 1344412800,
1344416400, 1344420000, 1344423600, 1344427200, 1344430800, 1344434400,
1344438000, 1344441600, 1344445200, 1344448800, 1344452400, 1344456000,
1344459600, 1344463200, 1344466800, 1344470400, 1344474000, 1344477600,
1344481200, 1344484800, 1344488400, 1344492000, 1344495600, 1344499200,
1344502800, 1344506400, 1344510000, 1344513600, 1344517200, 1344520800,
1344524400, 1344528000, 1344531600, 1344535200, 1344538800, 1344542400,
1344546000, 1344549600, 1344553200, 1344556800, 1344560400, 1344564000,
1344567600, 1344571200, 1344574800, 1344578400, 1344582000, 1344585600,
1344589200, 1344592800, 1344596400, 1344600000, 1344603600, 1344607200,
1344610800, 1344614400, 1344618000, 1344621600, 1344625200, 1344628800,
1344632400, 1344636000, 1344639600, 1344643200, 1344646800, 1344650400,
1344654000, 1344657600, 1344661200, 1344664800, 1344668400, 1344672000,
1344675600, 1344679200, 1344682800, 1344686400, 1344690000, 1344693600,
1344697200, 1344700800, 1344704400, 1344708000, 1344711600, 1344715200,
1344718800, 1344722400, 1344726000, 1344729600, 1344733200, 1344736800,
1344740400, 1344744000, 1344747600, 1344751200, 1344754800, 1344758400,
1344762000, 1344765600, 1344769200, 1344772800, 1344776400, 1344780000,
1344783600, 1344787200, 1344790800, 1344794400, 1344798000, 1344801600,
1344805200, 1344808800, 1344812400, 1344816000, 1344819600, 1344823200,
1344826800, 1344830400, 1344834000, 1344837600, 1344841200, 1344844800,
1344848400, 1344852000, 1344855600, 1344859200, 1344862800, 1344866400,
1344870000, 1344873600, 1344877200, 1344880800, 1344884400, 1344888000,
1344891600, 1344895200, 1344898800, 1344902400, 1344906000, 1344909600,
1344913200, 1344916800, 1344920400, 1344924000, 1344927600, 1344931200,
1344934800, 1344938400, 1344942000, 1344945600, 1344949200, 1344952800,
1344956400, 1344960000, 1344963600, 1344967200, 1344970800, 1344974400,
1344978000, 1344981600, 1344985200, 1344988800, 1344992400, 1344996000,
1344999600, 1345003200, 1345006800, 1345010400, 1345014000, 1345017600,
1345021200, 1345024800, 1345028400, 1345032000, 1345035600, 1345039200,
1345042800, 1345046400, 1345050000, 1345053600, 1345057200, 1345060800,
1345064400, 1345068000, 1345071600, 1345075200, 1345078800, 1345082400,
1345086000, 1345089600, 1345093200, 1345096800, 1345100400, 1345104000,
1345107600, 1345111200, 1345114800, 1345118400, 1345122000, 1345125600,
1345129200, 1345132800, 1345136400, 1345140000, 1345143600, 1345147200,
1345150800, 1345154400, 1345158000, 1345161600, 1345165200, 1345168800,
1345172400, 1345176000, 1345179600, 1345183200, 1345186800, 1345190400,
1345194000, 1345197600, 1345201200, 1345204800, 1345208400, 1345212000,
1345215600, 1345219200, 1345222800, 1345226400, 1345230000, 1345233600,
1345237200, 1345240800, 1345244400, 1345248000, 1345251600, 1345255200,
1345258800, 1345262400, 1345266000, 1345269600, 1345273200, 1345276800,
1345280400, 1345284000, 1345287600, 1345291200, 1345294800, 1345298400,
1345302000, 1345305600, 1345309200, 1345312800, 1345316400, 1345320000,
1345323600, 1345327200, 1345330800, 1345334400, 1345338000, 1345341600,
1345345200, 1345348800, 1345352400, 1345356000, 1345359600, 1345363200,
1345366800, 1345370400, 1345374000, 1345377600, 1345381200, 1345384800,
1345388400, 1345392000, 1345395600, 1345399200, 1345402800, 1345406400,
1345410000, 1345413600, 1345417200, 1345420800, 1345424400, 1345428000,
1345431600, 1345435200, 1345438800, 1345442400, 1345446000, 1345449600,
1345453200, 1345456800, 1345460400, 1345464000, 1345467600, 1345471200,
1345474800, 1345478400, 1345482000, 1345485600, 1345489200, 1345492800,
1345496400, 1345500000, 1345503600, 1345507200, 1345510800, 1345514400,
1345518000, 1345521600, 1345525200, 1345528800, 1345532400, 1345536000,
1345539600, 1345543200, 1345546800, 1345550400, 1345554000, 1345557600,
1345561200, 1345564800, 1345568400, 1345572000, 1345575600, 1345579200,
1345582800, 1345586400, 1345590000, 1345593600, 1345597200, 1345600800,
1345604400, 1345608000, 1345611600, 1345615200, 1345618800, 1345622400,
1345626000, 1345629600, 1345633200, 1345636800, 1345640400, 1345644000,
1345647600, 1345651200, 1345654800, 1345658400, 1345662000, 1345665600,
1345669200, 1345672800, 1345676400, 1345680000, 1345683600, 1345687200,
1345690800, 1345694400, 1345698000, 1345701600, 1345705200, 1345708800,
1345712400, 1345716000, 1345719600, 1345723200, 1345726800, 1345730400,
1345734000, 1345737600, 1345741200, 1345744800, 1345748400, 1345752000,
1345755600, 1345759200, 1345762800, 1345766400, 1345770000, 1345773600,
1345777200, 1345780800, 1345784400, 1345788000, 1345791600, 1345795200,
1345798800, 1345802400, 1345806000, 1345809600, 1345813200, 1345816800,
1345820400, 1345824000, 1345827600, 1345831200, 1345834800, 1345838400,
1345842000, 1345845600, 1345849200, 1345852800, 1345856400, 1345860000,
1345863600, 1345867200, 1345870800, 1345874400, 1345878000, 1345881600,
1345885200, 1345888800, 1345892400, 1345896000, 1345899600, 1345903200,
1345906800, 1345910400, 1345914000, 1345917600, 1345921200, 1345924800,
1345928400, 1345932000, 1345935600, 1345939200, 1345942800, 1345946400,
1345950000, 1345953600, 1345957200, 1345960800, 1345964400, 1345968000,
1345971600, 1345975200, 1345978800, 1345982400, 1345986000, 1345989600,
1345993200, 1345996800, 1346000400, 1346004000, 1346007600, 1346011200,
1346014800, 1346018400, 1346022000, 1346025600, 1346029200, 1346032800,
1346036400, 1346040000, 1346043600, 1346047200, 1346050800, 1346054400,
1346058000, 1346061600, 1346065200, 1346068800, 1346072400, 1346076000,
1346079600, 1346083200, 1346086800, 1346090400, 1346094000, 1346097600,
1346101200, 1346104800, 1346108400), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), risk = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 7,
8, 9, 10, 11, 0, 0, 0, 12, 13, 14, 15, 16, 17, 18, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3,
4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0)), row.names = c(NA, -1080L), class = c("tbl_df",
"tbl", "data.frame"))
I think this might do the trick, using fill instead of colour
OP_2016$date <- as.Date(OP_2016$date)
color_var <- vector(mode = "double",length = length(OP_2016$risk))
color_var[color_var== '0']<- NA
color_var[OP_2016$risk>=1 & OP_2016$risk<12] <- "yellow"
color_var[OP_2016$risk>=12] <- "red"
ggplot(OP_2016)+
geom_col(aes(x = date, y = risk, group = 1,fill=color_var), size= 0.9) +
scale_y_continuous(limits = c(0, 100),name = "Accumulated EBHours")+
scale_fill_identity("Risk Level", breaks= levels(as.factor(color_var))[c(1,2)],
labels = c("High >12 EBH","Medium 0-12EBH"),
guide = "legend"
)+
geom_line(aes(x = date, y= 12), linetype= "dotted", size = 0.8, colour = "red")+
geom_line(data = dis_fun_df, aes(x= date, y = rating),colour = "green")
To my knowledge ggplot does not support multiple scales of the same type, but others would know better than I.
UPDATE:
For anyone looking to actually use multiple scales for the same type of geom the {ggnewscale} package should provide the functionality that you are looking for:
https://github.com/eliocamp/ggnewscale

Why are the number of row results always different to the number of data frame rows and how can I make them match?

I use this loop to connect to Elasticsearch, retrieve some data and add it into my data frame based on a common identifier:
library(elastic); library(jsonlite)
for (i in first_chats_eu$associate) {
firstchats_eu_body <- paste0(
'{"size":1,"query":{"bool":{"must":[{"term":{"associate":"',
i
,'"}},{"term":{"type":"conversation-archived"}}]}},"sort":{"time":"asc"}}'
)
firstchats_eu_connection <- Search(
index = "my_index",
type = "my_type",
body = firstchats_eu_body
)
first_chats_eu$firstChat[first_chats_eu$associate == i] <- fromJSON(toJSON(firstchats_eu_connection$hits$hits), flatten = TRUE)[1]$`_source.time`
}
The problem is that it always produces the following message:
Error in `$<-.data.frame`(`*tmp*`, "firstChat", value = list("2017-06-08T00:17:01.118Z", :
replacement has 445 rows, data has 446
I have looked at other answers on SO but the solutions appear to be specific to each case, and thus not applicable to mine.
What could be causing this?
If it is of any use, this loop sometimes partially succeeds, in that it returns the first 34 results and then fails for all other rows after that point.
Data frame:
structure(list(associate = c("Pm0jYumSjx", "PbjmBqnIdY", "Vq3VblKpZn",
"JUCx0MOygt", "9IxVPXPGvQ", "FUUobAsS2Z", "uflgsKMNze", "VzeedmQuiv",
"BWYbDAIPkr", "PNCzrO3TRA", "zwemlUrxQE", "y19AiLUxLN", "4hhURCoC8H",
"Ak13YJF63y", "aoQI4Ncrt4", "syaulepdzW", "ISdTwUISIG", "wncQ5pXq88",
"MYbsg1E2EQ", "OhkAz5aren", "A2wqC9F1WR", "ZDVmRyHkUY", "n5kKRmTMzj",
"mfek2ukQXk", "9Tz6HWSBTd", "b7JdFFaL7M", "TetbzmItfe", "NfCPXPfA8G",
"fc3QfKEgoI", "lWOiSbqDkN", "PXU7CqGO1S", "kEgnBQeUVH", "Zk182RMS4W",
"vzF3lzDKlx", "92PThwjFeO", "hunSRzc1p9", "n2xvlXoTS3", "AzJIMGGn4I",
"8DAm5dFPtN", "Qk6Dl1wgAG", "b6Z1C2RgYk", "FsHvmjOWu8", "SHqTI9YFFx",
"xhKjx5JJin", "RAPQXUNJbF", "vuq1KKylZY", "sBcaWcjEsk", "B4Z2TYT02E",
"u6iEcDrjRL", "Tv8FVDmFlN", "lTS0ZT24vu", "i2I0kykYo0", "N9Nqwu7XiM",
"0wFw3bY5De", "KEcKE4DRdT", "JcZWhBasDE", "FfLrHsnrT5", "ibOCROEWcG",
"b9K3V27GH7", "1TMLQZS5eR", "Yfo5sO7Hyj", "Q09sg2jrsI", "byzMzoS8QV",
"NKLbLwDA4b", "iKkQUDBXln", "Rutbihe39R", "T00E44PAAf", "PSKIFW2Bi3",
"ewJjvrT8H6", "rudiPWdyHj", "gShZwgHn0m", "dQmluy3ilM", "kO2hP2SNzJ",
"hG8iLjN6BD", "GuDBMuoht6", "AOzBVHetmK", "inSAsVD12e", "tYSXKOEhQB",
"ie5m9sDcd5", "31XpEIYYEd", "qrh9kSiCTh", "6cGqFJB1vh", "VPqvH3dHnz",
"tmg2iLu1f6", "a7nBJlzYRt", "D8jHgvvZfY", "pMwjxAV1iP", "0hD2vZYxf7",
"JFuSGQZmYq", "dCu6ebaRzu", "8kFAGY52xO", "8dq2kAKbnP", "FL3RRY9dbb",
"kJhRRpLQDu", "bRkAyVrvBf", "mMHRUeQEjd", "3ATe60itju", "t1IdGh570n",
"LT7PLxWjKh", "zanyzD8KHy", "zo2fOcIflj", "pLhZhgBSd6", "iTzqXtTlVc",
"6ZGJSIjfUK", "bp0JOkgL2n", "4fHmIsuzoR", "wr8pS9BIGn", "XjBFbgCbA6",
"aBDfBMlyhl", "bjpWS5Zi1d", "1m8RnFavmk", "BCbEV3YcWs", "WO3z2kXsHv",
"W4ziF0GFYp", "Dvdpoq3gZJ", "TeO490YGfr", "sMuDgfHnOY", "IBH2U18pLr",
"iWfUpGFrvN", "JiqDLRGEFX", "kyNo0jk8Z1", "93HElGBOHz", "5ZJCyJrVoO",
"2XS3g6lGJN", "BcLA5ChZHs", "kEVIPyQENZ", "xEHCoCz4kk", "2VP9YQHg0z",
"NE1PnnqgGk", "Ay1r8aB1p3", "KX6pPK8IYt", "oaPI42kqwl", "G8DQ2GKget",
"rrUNVhvaL6", "e1V2GXIIQi", "S1ABORMzvC", "P5SHJFevse", "G5oyD7KyNk",
"96uS2gpKKW", "oVokuIkcBH", "uvZwOIZkhC", "9g3a3lkaqK", "sL3CM1Ydmp",
"CeRfpJxt5l", "gywxLgJfwk", "ykryX7GvTZ", "6G3LCq9dTM", "zcbQHDmYlm",
"vb9zQCBcsC", "CHQR1sudJt", "aSPIsQtBW9", "vUF6TTncxg", "mfZwbqTdqs",
"6KFAVH1JfU", "5VC1MoGAPL", "dBT7WbCPbg", "hsiLXdTOlX", "RYCLZhFY90",
"q9SsmoL8Ig", "eHawNS1jr0", "GVgIzKB9UB", "v1RSMGUZAb", "2arkjePApM",
"FVNiv2T5i2", "2YI036CYSi", "bEe9hMOuPw", "SYHAbSA4Bo", "kIxHklYUvm",
"ea0HZio45e", "bJLyBCIDLq", "UOX1xE4I4m", "DUlIH2h7wi", "MYF1ueeXfe",
"1tOV5TEEK5", "oPt6FcBGj5", "jvyfvuEgaZ", "lNVTpT4hA9", "HqDMYSu1lC",
"al11gInzgN", "hIC4Q5JlBH", "ujvEUtatGU", "wU4B2gFWsH", "UbC2Fnef7t",
"4PMafQHS86", "pzgyPSDDR2", "HajTJzwD6s", "Vmb16EuXQd", "MLImxndCP8",
"JioVLn85XG", "ezeN1Jrfz7", "X3dVvzFpO7", "jXWNcEJF40", "1Dlxb9sFY1",
"I2rDM0GCQY", "ItEc62tlMD", "BQSEE0aw5j", "PcKSaFFCjc", "UZOQv40QpR",
"TpTUfAkH2j", "102Qub695d", "iYMYFVeO3a", "9q6smEyLHE", "Oft5GPL1Kz",
"RNgREArWYp", "6JpBEX0t5E", "FQGSnKhEHZ", "qhH4yU4GF4", "smal1c8nZI",
"XM22Vo5TMb", "wVNGHFxsrw", "bOhSejjY4F", "XstvxG2evH", "YSeaBFnCxC",
"aHvosVBvWF", "ECa32iNWUP", "ntNOwcH86q", "6sBSxrENuN", "TKrEDKGBP4",
"7goCPMibBX", "owne4NwFBU", "DzbUeksk3x", "GFmnRMNXLn", "z03bGKnR4K",
"yrickhqzXO", "orNL3qgRs5", "aZDhxK3MnT", "GfjLnYebLi", "ABzJScG4aF",
"MzDl5sIcEv", "nDPQ5ryX60", "wzYLWcEGt7", "fg4yEOCrMg", "cOxDNFSVJ1",
"N3xJGamqYg", "YCWeSx7FdA", "nS7j6dlHW1", "v8xHPHvF9I", "WTgftXNmQv",
"JhhRGHAiAv", "kA95elFmbl", "6SBhLJ10MA", "zB8PDYd3yi", "Id0K9bUgkh",
"wBkgvnElHb", "kQnwwfc7kJ", "6hNV4Y1C4A", "AmA4Y13eQd", "flON1gpb6N",
"f5DopCEFwK", "boFD2o0FU2", "yd2OuCsCSL", "nDzEFEpz2D", "fweWiQ6x6l",
"G1mT217Z6l", "rFt9fevsgu", "MGtbd36gdp", "K48dwo2BDj", "fzDa2HHq8k",
"q3MmPsZKB4", "O9yTAKQlCN", "tDpjeKpWIY", "wOgm4P7503", "4kXj6O1TWk",
"GTgOP6Ik3U", "AcCdmoQqtn", "F5fwdhj5l3", "AJnCAwFOdi", "ssvby6VgQp",
"qzh7xCHMLl", "T2VySRQ0M0", "0yfnFPWynB", "L7PqHs1f0H", "83GzTa9bxh",
"DOGXIreUyo", "PDsqEWLsu5", "sN3JstovsG", "knZx7l2hsk", "asCBJPBgTg",
"ytAmW9sTAP", "S9vuzwn13O", "nrXFr6Ww5O", "sFUB1beOAx", "CyCvAIDs5q",
"z2jmTzZVAo", "Zdd2fiDPVe", "MOQP5vAj8W", "rjx4tHVh6E", "2G3dquHI2q",
"BruK8mqzVI", "GAB1feRTKE", "TPwJ8jp6j2", "mprSpIdRuJ", "qBaU7KERwq",
"QNfCFKWXv5", "6IdSIlfhrz", "19SMR3CQID", "LxnNlx8DsD", "TEJeH1bZXz",
"bTmafqEayN", "9X18BUOA36", "NoYo4tT3oG", "yj5TyZZyNT", "kPVRWBJRXM",
"H3kVwfzGTe", "vq7qUUqGUr", "GLDCR4av3A", "xiYU2MDc6c", "qOGqanHpjw",
"5BaW31f0Nr", "4dJwcfCqkA", "UJzHhwFZRe", "dec0Qlz3eK", "0l7WDqbZb0",
"MMwd72FDCL", "EDMkdKIWMy", "2zLQmEuKpY", "1SUMAUhkZc", "BxXiWBTb8w",
"zFdVjcL7qz", "pfPOEmMTei", "HzjyCETszF", "b26xszWUlP", "AseQsOAqqY",
"lvDbmLqnyg", "L5WxvWWAyt", "X5WUvRm2BY", "t6XCwcOinz", "FG7pLFG8Uw",
"yLpMyvmHb2", "7Ac9pVAF0B", "sDN9UHNGm4", "9Z1L7RtlA3", "bVb8hjn1ju",
"F4nLAs89VO", "kB4aIQEpkf", "lov4Q42w2B", "jnFjq8covm", "tIuHe5uCaC",
"qsAssEo3Qr", "mW3HVtDHQj", "pFoLBb4EfI", "5UA1lbRTrF", "s1qrkLbTIU",
"RQrW6CtY7C", "xVIb7ymcmW", "Iw3AvXAlZK", "yW0P3oHRKp", "Ivf463pZ3k",
"07OAzJ3LMW", "5nB6O3rLm3", "QsI7HSwDZ6", "YN6V6flLDk", "B5RXoyGmeY",
"gt1VgDERw1", "FS2yKGf1vg", "BpUytkzSeS", "fKFSxBU4QG", "ueBhiB7lig",
"c901c019c5", "MKADBViZNz", "Vcb76RpM0z", "PnIACGOY1I", "EIYwDIB3od",
"B30kGNViyD", "YuAuj81ULa", "HBRNcgXjly", "CNFYrTCnkB", "0NG6oAIhrv",
"39OL0da3JB", "pEYsCPrejG", "imUsAjHivl", "D6QCEqCeEN", "pbARKO6WHG",
"mtx6Brb5rP", "oaLUE44A5W", "AgrZaWAFsg", "yvOMkkIZ1m", "Z6fXosz4Fa",
"2fthCJ5DpQ", "yzDHP6VC3C", "oACH3KkPl4", "OUpJGlVe7Q", "rTUsgI4HB1",
"BG0bfaNntS", "T6DgmUfrMS", "tjMwvgas9N", "gSYzWEbwH6", "6P9pRD9jft",
"h7jgprw5ZI", "9RYqSSNP4s", "pv51ewuo8J", "kPHNzPAsy7", "qDg9oiE5vO",
"8PxDnGjkT9", "czyeqBbnOJ", "uHSx9FbBZy", "Z2mdkJk7Tk", "A9lBq2pPB7",
"1MjXRGUpCL", "oS6oAKFqvi", "Ro2Cpuw7nB", "4LjJWDFiJU", "ASrmSlOd4j",
"f9BBXMzBju", "VHdzp3okgg", "zgCdOvoFoJ", "vm7dLCBAXu", "aohXymMv9M",
"rsATydK2xE", "l6erf0NNN8", "wtcNWItEK5", "XO9BfYHSuJ", "FIsDrDMEM6",
"wThiyvldqW", "n49e2LohLA", "ISPVKeaL8B", "QavdvrKCrJ", "Oc5GaqMvsD",
"dBgH5abgm4", "jbxXIMi5T3", "wOEbpBhvJO", "EcSEO5x8Aa", "IPYmcletaC",
"RccyJrXYSQ", "F044DDAL9U", "xCUcsCDnQd", "wkF3tx7M5N", "RNMO5oH2ce",
"KEUArjG58B", "TfBcBOhVZQ", "BpjQjjlTDg", "wSsnje6q39", "ITAESdcmae",
"XTzpZzZFxy", "UutDWic510", "TMgewBSCgi", "mhEM43wLRC", "FwXswHZJzn",
"jqvBb6v6CY", "1YJDIwy9q2", "ea7Hx9RXNl", "cAYtXFKDvK", "R8DhTwSDmg",
"a54nasvwuX", "bnNkM8nimD", "FSSGwmo5Qh"), accountCreation = structure(c(1521647203.675,
1519339418.982, 1520978846.791, 1521462847.464, 1521219528.448,
1525602918.744, 1521039530.235, 1521026513.49, 1521802344.631,
1523790193.342, 1523699716.79, 1522159355.724, 1520935894.587,
1525529531.887, 1524575967.739, 1521127935.332, 1521047835.094,
1521101424.346, 1520997141.963, 1521025271.346, 1521106469.437,
1520952168.279, 1522939980.072, 1521644559.484, 1520962152.598,
1520946377.448, 1521114036.503, 1523303212.556, 1520948768.796,
1521281304.856, 1521484713.426, 1521314445.804, 1521632072.396,
1524399853.238, 1524063742.962, 1524048668.223, 1521231047.543,
1524157639.65, 1521209831.34, 1521475186.089, 1521724609.138,
1523533039.228, 1523702955.539, 1522488605.67, 1521890183.374,
1521642579.3, 1522228581.673, 1523525372.293, 1521195229.708,
1520940268.528, 1521044364.138, 1527678100.482, 1524600058.621,
1522777239.275, 1523870518.667, 1521369133.225, 1522232575.542,
1521198325.682, 1523360584.136, 1521813258.386, 1521627226.595,
1521033049.721, 1523452387.623, 1522236488.509, 1521198246.297,
1524671994.275, 1521720640.095, 1522242402.263, 1521637447.34,
1521718413.354, 1521290525.148, 1521530545.831, 1521022229.054,
1521054255.295, 1522147616.899, 1521115359.397, 1525349801.15,
1520943213.673, 1523610221.63, 1521478193.229, 1521889281.441,
1523701184.22, 1521810788.09, 1520946831.645, 1521471304.719,
1521561858.612, 1521893506.908, 1521048628.327, 1521116277.724,
1522765167.032, 1521286311.531, 1521125237.2, 1521887929.78,
1525159037.328, 1524135123.072, 1519230466.413, 1521030848.111,
1520956865.733, 1521592739.521, 1521457270.104, 1521736081.412,
1521736093.529, 1522159760.701, 1524053386.664, 1524496291.977,
1524216088.817, 1520936977.191, 1520953090.937, 1521212807.586,
1521803151.728, 1521801343.725, 1524055557.569, 1522767163.557,
1521729513.656, 1524316353.148, 1520935603.264, 1523960278.276,
1521499028.577, 1521643199.88, 1524483302.128, 1525335512.042,
1521373117.292, 1520879030.594, 1521023257.287, 1521022157.451,
1523800300.841, 1520935205.484, 1521738187.203, 1521280970.99,
1521213783.986, 1523970332.086, 1522053669.333, 1521738166.805,
1522945933.897, 1520952792.799, 1524757978.638, 1521111905.664,
1524295064.907, 1521025774.074, 1522320956.043, 1521655757.989,
1521195220.823, 1521016193.598, 1521730985.238, 1521739086.354,
1520936329.814, 1520941866.452, 1521308647.796, 1520942541.479,
1520932451.164, 1520932450.409, 1524233637.038, 1523542112.437,
1521891391.694, 1520941112.597, 1521981704.61, 1524126540.184,
1521452436.923, 1521681150.112, 1521021271.162, 1523113077.121,
1524135121.181, 1521225075.835, 1521200485.608, 1521463364.621,
1522340866.586, 1522675782.65, 1522061257.085, 1520935213.165,
1521111379.722, 1522405319.784, 1521021777.312, 1521062469.007,
1524653489.931, 1521216029.087, 1520932450.074, 1522483910.384,
1521019100.815, 1524483561.903, 1521196200.601, 1521141925.275,
1524608679.675, 1520935615.677, 1524766524.32, 1521019881.426,
1520936989.662, 1523036547.916, 1521196426.438, 1521210908.376,
1521628649.889, 1521280983.286, 1521300046.167, 1524738218.644,
1523543199.814, 1521200519.952, 1524914417.224, 1524652840.984,
1525862276.545, 1521027321.217, 1521104801.584, 1521223553.524,
1521211503.42, 1521921773.167, 1520946989.043, 1523445841.807,
1521725990.924, 1521977244.334, 1521627508.652, 1525969006.645,
1525969022.093, 1521812644.345, 1520932449.817, 1521458377.453,
1521020768.777, 1521304861.478, 1520947823.441, 1521205017.765,
1520953961.96, 1520940974.834, 1523182530.64, 1521886711.101,
1521393592.616, 1521377252.798, 1521455570.819, 1521477410.136,
1524502991.311, 1521568209.362, 1520970930.145, 1523821413.628,
1521541778.729, 1519236437.627, 1523702049.192, 1522173164.254,
1524657516.266, 1521735865.479, 1521466638.454, 1521813538.683,
1521476092.325, 1521150151.59, 1521565928.114, 1522758779.981,
1521479231.715, 1520935850.699, 1521025111.789, 1522867701.541,
1521471989.26, 1524744993.667, 1520950428.393, 1523536553.54,
1522421382.988, 1521546156.87, 1521028394.686, 1521050335.668,
1521020456.866, 1522229659.937, 1521025286.049, 1521122727.912,
1521026467.854, 1521450006.649, 1521219087.52, 1521469866.623,
1521197195.734, 1521105260.349, 1521011471.989, 1521190524.789,
1521452303.022, 1520933421.224, 1521115127.677, 1523884939.943,
1520961138.002, 1521016753.264, 1521016893.143, 1521017010.196,
1521017173.526, 1521017281.13, 1521017606.628, 1521117180.29,
1521117300.792, 1521125450.611, 1521132242.791, 1521205674.621,
1521205950.641, 1521215731.346, 1521459082.156, 1521460316.99,
1521482702.085, 1521482815.695, 1521482903.143, 1521483099.712,
1521483278.868, 1521483371.324, 1521540692.887, 1521543890.489,
1521544034.707, 1521544160.551, 1521544235.273, 1521544330.989,
1521544485.786, 1521544609.88, 1521544764.696, 1521553643.038,
1521553739.931, 1521590910.434, 1521731535.229, 1521731535.928,
1521731546.759, 1521802978.117, 1521886145.182, 1521886194.283,
1521886413.213, 1521889580.687, 1522052985.205, 1522069337.077,
1522063483.939, 1522166738.438, 1522250898.347, 1522340911.64,
1522942459.588, 1523359215.503, 1523397047.86, 1523437988.256,
1523438083.426, 1523445420.319, 1523447633.945, 1523455566.243,
1523458605.986, 1523541116.105, 1523615795.787, 1523879249.334,
1523890699.033, 1523979871.23, 1523980654.125, 1524045405.981,
1524056257.89, 1524154119.955, 1524159979.146, 1524393026.557,
1524393845.931, 1524478733.191, 1524494492.895, 1524495293.313,
1524500425.993, 1524500660.969, 1524585635.173, 1524641288.518,
1524659670.369, 1524667611.839, 1524668269.638, 1524672949.857,
1524673079.627, 1524673178.774, 1524912901.694, 1525006502.237,
1525034518.791, 1525076940.386, 1525079238.038, 1525079979.214,
1525081785.524, 1525083082.759, 1525085456.7, 1525176437.685,
1525253930.6, 1525255054.744, 1525259664.818, 1525261014.423,
1525261500.246, 1525287238.277, 1525346162.325, 1525350313.866,
1525451045.815, 1525686874.633, 1525800629.92, 1525865551.171,
1525877902.633, 1525942349.151, 1525945093.268, 1526141141.217,
1526142261, 1526301362.64, 1526301431.797, 1526301861.592, 1526302079.545,
1526324313.439, 1526327287.226, 1526383076.912, 1526394120.405,
1526394871.906, 1526395540.302, 1526396440.737, 1526467461.013,
1526474515.013, 1526474851.867, 1526474950.656, 1526475027.807,
1526572192.208, 1526899111.332, 1526903749.417, 1526911124.083,
1526978640.406, 1526992033.825, 1527005249.155, 1527062775.068,
1527065597.18, 1527070549.853, 1527072673.61, 1527086220.387,
1527087986.751, 1527149254.527, 1527149382.032, 1527149716.254,
1527149851.777, 1527150283.992, 1527150450.301, 1527150553.517,
1527150666.548, 1527165902.1, 1527165998.747, 1527166128.254,
1527166243.866, 1527170994.7, 1527171081.964, 1527171158.104,
1527171239.335, 1527174965.995, 1527175065.505, 1527236752.65,
1527237081.146, 1527237162.074, 1527237333.16, 1527237518.202,
1527237606.934, 1527237676.797, 1527237786.63, 1527237891.106,
1527237979.302, 1527238310.444, 1527238397.71, 1527238467.749,
1527247882.085, 1527247921.493, 1527253757.25, 1527261991.607,
1527262528.413, 1527590487.952, 1527590606.241, 1527590748.119
), class = c("POSIXct", "POSIXt")), firstChat = list("2017-12-23T19:02:03.112Z",
"2017-06-08T00:17:01.118Z", "2016-12-11T15:20:54.019Z", "2017-03-10T11:41:44.036Z",
"2017-02-07T17:00:13.338Z", "2017-02-09T17:56:32.961Z", "2017-02-06T16:34:19.095Z",
"2017-06-07T12:11:15.257Z", "2017-02-21T14:58:31.850Z", "2017-02-09T15:17:40.920Z",
"2017-02-09T14:43:02.026Z", "2017-02-20T15:37:56.756Z", "2017-03-08T15:04:17.341Z",
"2017-03-03T19:43:19.287Z", "2017-07-16T09:47:28.713Z", "2017-03-30T17:57:47.205Z",
"2017-03-20T20:09:41.879Z", "2017-03-22T15:09:21.433Z", "2017-03-22T19:02:20.977Z",
"2017-04-06T10:46:50.105Z", "2017-04-19T17:12:18.318Z", "2017-04-21T15:21:48.603Z",
"2017-04-20T14:12:07.052Z", "2017-04-21T11:33:10.867Z", "2017-04-20T08:58:52.326Z",
"2017-04-21T13:34:01.145Z", "2017-04-24T17:50:07.694Z", "2017-04-21T11:02:15.512Z",
"2017-05-24T10:56:42.089Z", "2017-05-06T13:26:43.845Z", "2017-05-10T17:44:43.533Z",
"2017-05-04T15:14:47.320Z", "2017-05-16T15:28:05.820Z", "2017-05-17T19:29:16.809Z",
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA,
446L), class = "data.frame")

plot a very large data with many zeros

This is a small portion of a vey big data
df<- structure(list(A = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0.68906, 0, 0, 0, 0, 0, 0, 0, 0, 0.13597, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0), B = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0.40001, 0, 0, 0, 0, 0.69718, 0, 0, 0, 0, 0, 0, 0,
0, 0.090752, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), C = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0.84068, 0, 0, 0, 0.34713, 0, 0, 0, 0, 0.65201,
0, 0, 0.25725, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), D = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.86419, 0, 0, 0, 0.3845,
0, 0, 0, 0, 0.67091, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0), E = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1.1083, 0.8324,
0, 0, 0, 0.38499, 0, 0, 0, 0, 0.69064, 0, 0, 0.14596, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), F = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 1.0954, 0.74426, 0, 0, 0, 0.37715, 0, 0, 0, 0, 0.68884,
0, 0, 0.20826, 0, 0.38782, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0), G = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1.0985, 0.66651, 0, 0,
0, 0, 0, 0, 0, 0, 0.68861, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.1812,
0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("A", "B", "C", "D", "E",
"F", "G"), class = "data.frame", row.names = c(NA, -39L))
What I want is to show the values in a more stressed way when there are a lot of zeros in a data
How I plot it is like this
eucl_dist=dist(df,method = 'euclidean')
hie_clust=hclust(eucl_dist,method = 'complete')
my_palette <- colorRampPalette(c( "green", "yellow", "red"))(n = 1000)
heatmap.2(mydata, scale = c("none"), Colv=F, Rowv=as.dendrogram(hie_clust),
xlab = "X", ylab = "Y", key=TRUE, keysize=1.1, trace="none",
density.info=c("none"), margins=c(4, 4), col=my_palette, dendrogram="row")
But as you see, in this small example, the zero dominate my plot and when it is very large then it is impossible to see anything. also I cannot change the position of the values
You are asking a lot of questions here, I'll try to answer those I see.
Zero dominates plot
Zeros dominate you data but, what do the zeros mean? Without some insight into what the zeros actually mean its hard to prescribe one best way to deal with it.
Colormap
The colorful colormap that you chose is not the best way to describe quantitative data. I would suggest a simple white to blue (or color of your choice) so that your zeros are shown as white and get hidden with the nonzero data emphasized. Example (only changing my_palette <- colorRampPalette(c("white", "cornflowerblue"))(n = 1000)):
Changing the position of the values
I'm not certain what you mean here but the layout is fixed by the dendrogram you defined.

Each ggplot on separate pdf page - plot row by row [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to put one line plot on each page of pdf file. All the data for plotting is stored in single data frame. Each row should be plotted.
That's how the data looks like:
structure(list(`10` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `34` = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 370500, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1091361.9, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1512409.6,
0, 0, 0, 0, 0, 0), `59` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 4231358.2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 5995680.4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 2266775, 0, 0, 0, 0, 0, 0, 6864490.1, 0, 0,
0, 0, 0, 0), `84` = c(0, 0, 0, 0, 1783350, 0, 0, 0, 1177650,
0, 0, 0, 0, 0, 0, 0, 0, 4316664.7, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 9262556.7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 2831286.1, 0, 0, 0, 0, 0, 0, 10643218.2,
0, 0, 0, 0, 0, 0), `110` = c(0, 0, 0, 0, 1778743.3, 0, 0, 0,
1465966.7, 0, 0, 0, 0, 0, 0, 0, 0, 3111700, 0, 0, 1955337.5,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5584784.4, 5584784.4,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3092525, 0,
0, 0, 0, 0, 0, 7847143.8, 0, 0, 0, 0, 0, 0), `134` = c(0, 0,
0, 0, 1121869.4, 0, 0, 0, 1439430.6, 0, 0, 0, 0, 0, 0, 0, 0,
2854250, 0, 0, 0, 0, 0, 0, 914890, 0, 0, 847880, 0, 0, 0, 0,
0, 0, 0, 8191800, 0, 0, 0, 0, 0, 0, 1830904.5, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1650150, 0, 0, 837130, 0, 0, 0, 4925095.1, 0,
0, 0, 0, 0, 0), `165` = c(0, 0, 0, 0, 1432775, 0, 0, 0, 1394186.1,
0, 1120183.3, 0, 0, 0, 0, 0, 0, 2262421.7, 0, 0, 0, 615660, 0,
0, 1292795.8, 0, 0, 712622.5, 0, 0, 0, 0, 0, 0, 0, 2683469.4,
0, 0, 0, 0, 0, 0, 2318485.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1561800,
0, 0, 0, 0, 0, 0, 4382993.7, 0, 0, 763460, 0, 0, 0), `199` = c(0,
0, 0, 0, 1314220, 0, 0, 0, 1439718.8, 0, 1929266.7, 0, 0, 0,
1101800, 0, 0, 2759366.7, 0, 0, 0, 1291728.6, 0, 0, 2489775.6,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2858345.8, 0, 0, 0, 1819542.1,
0, 0, 1497640.3, 0, 0, 0, 1300250, 0, 0, 0, 0, 0, 0, 1566875,
0, 0, 0, 0, 0, 0, 4625895.6, 0, 0, 1308158.3, 0, 0, 0), `234` = c(1257250,
0, 0, 0, 0, 0, 0, 0, 1276080, 0, 1848500, 0, 0, 0, 1529350, 0,
0, 2155275, 0, 0, 0, 2023041.9, 0, 0, 1966447.7, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1184200, 1184200, 0, 0, 1652350, 0, 0, 2018581.7,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1835225, 0, 0, 0, 0, 0, 0, 4639414.7,
0, 0, 720715, 0, 0, 0), `257` = c(0, 0, 0, 0, 0, 669442.5, 0,
0, 1253026.7, 0, 960410, 960410, 0, 0, 1258267.5, 0, 0, 1707392.5,
0, 0, 0, 563280, 0, 0, 2403237.9, 0, 0, 0, 1044100, 0, 2075700,
0, 0, 0, 0, 0, 5718450, 0, 0, 1704550, 0, 0, 1350286.9, 0, 0,
0, 0, 2011700, 0, 0, 0, 0, 0, 1739500, 0, 0, 0, 0, 0, 0, 4612520.8,
4612520.8, 0, 0, 0, 0, 0), `362` = c(0, 1593500, 0, 0, 0, 1610625.3,
0, 0, 1234902.5, 0, 0, 1481036.8, 0, 0, 1583647.5, 0, 0, 1752089.2,
0, 0, 0, 0, 0, 0, 2410809.2, 0, 0, 0, 654940, 0, 0, 0, 0, 0,
0, 0, 7014905.6, 0, 0, 0, 0, 0, 1165672.1, 0, 0, 0, 0, 0, 0,
0, 1029910, 0, 0, 2153087.5, 0, 0, 0, 422920, 0, 0, 0, 7495855.9,
0, 0, 0, 0, 0), `433` = c(0, 0, 0, 0, 0, 1340283.9, 0, 0, 1268996.9,
0, 0, 1416683.3, 0, 0, 1047862.5, 0, 0, 1819653.8, 0, 0, 0, 0,
0, 0, 2227565.7, 0, 0, 0, 763765, 0, 0, 1595430, 0, 0, 0, 0,
4894549, 0, 0, 0, 0, 0, 1061375.4, 0, 0, 0, 0, 0, 2251950, 0,
1042130, 0, 0, 2055300, 0, 0, 0, 696278.3, 0, 0, 0, 5353797.8,
0, 0, 0, 0, 0), `506` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2020300,
2020300, 0, 0, 0, 0, 0, 0, 7681526, 0, 0, 0, 0, 0), `581` = c(0,
0, 1749237.5, 0, 0, 0, 2421665.8, 0, 0, 1773262.5, 0, 0, 2251004.3,
0, 0, 2570175, 0, 0, 3379756.9, 0, 0, 0, 2054455.6, 0, 0, 2518270.8,
0, 0, 0, 0, 0, 0, 2917968.2, 0, 0, 0, 0, 7004350, 0, 0, 1451600,
0, 0, 1394411, 0, 0, 0, 0, 0, 2507858.3, 0, 2377012.5, 0, 0,
3719165.4, 0, 0, 0, 1472870.3, 0, 0, 9666916.1, 0, 0, 1730300,
0, 0), `652` = c(0, 0, 476910, 476910, 0, 0, 1149078.8, 1149078.8,
0, 1082468.7, 0, 0, 882769.7, 0, 0, 1370449.4, 1370449.4, 0,
1529049, 1529049, 0, 0, 943632.2, 0, 0, 916587.8, 0, 0, 0, 988261.1,
0, 0, 1778007.1, 1778007.1, 0, 0, 0, 3087304.8, 3087304.8, 0,
782860, 782860, 0, 510158.5, 510158.5, 0, 0, 0, 0, 1503750, 0,
1100677.5, 1100677.5, 0, 1669260, 1669260, 0, 0, 770733.2, 0,
0, 4939242.8, 4939242.8, 0, 643564.4, 643564.4, 0), `733` = c(0,
0, 0, 1095060, 0, 0, 0, 1674089.3, 0, 1252101.3, 0, 0, 1259111,
0, 0, 0, 2429293.3, 0, 0, 2326928.3, 0, 0, 1259216.5, 0, 0, 1238837.5,
0, 0, 0, 1224858.3, 0, 0, 0, 2952529.9, 0, 0, 0, 0, 4626414.7,
0, 0, 1121440, 0, 0, 1025386.2, 0, 0, 0, 0, 1917900, 0, 0, 2197533.3,
0, 0, 2840155.5, 0, 0, 1054285.7, 0, 0, 0, 7516814.2, 0, 0, 1329434.4,
0), `818` = c(0, 0, 0, 720551.1, 0, 0, 0, 714662.7, 0, 617012.9,
0, 0, 549850.8, 0, 0, 0, 1197460, 0, 0, 771979.2, 0, 0, 585847.5,
585847.5, 0, 875475.4, 0, 0, 0, 576774, 0, 0, 0, 1147389.8, 0,
0, 0, 0, 2292421.7, 0, 0, 755258.3, 0, 0, 0, 0, 0, 0, 0, 858930,
0, 0, 1242668.3, 0, 0, 1580088.3, 0, 0, 641938.6, 641938.6, 0,
0, 3838660.4, 0, 0, 733140.8, 733140.8), `896` = c(0, 0, 0, 590480,
0, 0, 0, 817087.6, 0, 569869.5, 0, 0, 650822.5, 650822.5, 0,
0, 1624052.5, 0, 0, 682570.8, 0, 0, 0, 1538800, 0, 690488.6,
690488.6, 0, 0, 797923.9, 0, 0, 0, 1204889.3, 0, 0, 0, 0, 2184432.2,
0, 0, 676654.7, 0, 0, 0, 210680, 0, 0, 0, 791152.5, 0, 0, 1599855.8,
0, 0, 1358543.8, 0, 0, 0, 931288, 0, 0, 4683895.2, 0, 0, 0, 1202806
), `972` = c(0, 0, 0, 799116.4, 0, 0, 0, 759169.9, 0, 408845,
0, 0, 0, 948980, 0, 0, 968766.7, 0, 0, 675349.7, 0, 0, 0, 0,
0, 0, 1811117.6, 0, 0, 609098.5, 0, 0, 0, 1073749.1, 0, 0, 0,
0, 2392258.9, 0, 0, 743580, 0, 0, 0, 1020485, 0, 0, 0, 446596.7,
0, 0, 1178583, 0, 0, 1438261.7, 0, 0, 0, 1133057.9, 0, 0, 4445814.7,
0, 0, 0, 1057776.9), `1039` = c(0, 0, 0, 447255.3, 0, 0, 0, 609409.1,
0, 304340, 0, 0, 0, 0, 0, 0, 694232.8, 0, 0, 473015.3, 0, 0,
0, 0, 0, 0, 419524.9, 0, 0, 447760.6, 0, 0, 0, 932513.5, 0, 0,
0, 0, 1251960.5, 0, 0, 276560, 0, 0, 0, 259640, 0, 0, 0, 354995,
0, 0, 1570222.5, 0, 0, 1021822, 0, 0, 0, 811614, 0, 0, 2941698.2,
0, 0, 0, 1199942.5), Gene = 1:67), .Names = c("10", "34", "59",
"84", "110", "134", "165", "199", "234", "257", "362", "433",
"506", "581", "652", "733", "818", "896", "972", "1039", "Gene"
), row.names = c(NA, 67L), class = "data.frame")
I have tried something like that so far...:
for(i in 1:nrow(Tra_decon)){
Tra_decon_melt <- melt(Tra_decon[i,], id = "Gene")
pdf("Test_plot.pdf", onefile = TRUE)
ggplot(Tra_decon_melt, aes(variable, log10(value), group=factor(Gene))) +
theme(legend.title=element_blank()) +
ylab("XXX") +
xlab("XXX") +
geom_line(aes(color=factor(Gene)), size = 1.2) +
ggtitle("XXXX") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
dev.off()
}
A solution without a for loop (and so faster) is this one:
plots <- lapply(1:nrow(Tra_decon), function(i){
Tra_decon_melt <- melt(Tra_decon[i,], id = "Gene")
ggplot(Tra_decon_melt, aes(variable, log10(value), group=factor(Gene))) +
theme(legend.title=element_blank()) +
ylab("XXX") +
xlab("XXX") +
geom_line(aes(color=factor(Gene)), size = 1.2) +
ggtitle("XXXX") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
})
pdf("Test_plot.pdf", onefile = TRUE)
plots
dev.off()
This works for me:
pdf("Test_plot.pdf", onefile = TRUE)
for(i in 1:nrow(Tra_decon)){
Tra_decon_melt <- melt(Tra_decon[i,], id.vars = "Gene")
plot<-list()
plot[[i]]<-ggplot(Tra_decon_melt, aes(variable, log10(value), group=factor(Gene))) +
theme(legend.title=element_blank()) +
ylab("XXX") +
xlab("XXX") +
geom_line(aes(color=factor(Gene)), size = 1.2) +
ggtitle("XXXX") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
print(plot[[i]])
}
dev.off()

Resources