Save multiple plots at once - r

I have a large database to test normality, and I am making plots through a loop
The example piece of df
structure(list(paciente = c(6278, 6447, 6462, 6213, 6358, 6295,
6523, 6174, 6343, 6270, 6483, 6307, 6467, 6219, 6446, 6274, 6001,
6002, 6003, 6004, 6005, 6006, 6014, 6016, 6019, 6024, 6026, 6028,
6030, 6031, 6043, 6044, 6048, 6051, 6053, 6054, 6059, 6066, 6074,
6080, 6085, 6088, 6092, 6094, 6096, 6098, 6100, 6102, 6103, 6104,
6105, 6109, 6110, 6118, 6124, 6126, 6127, 6129, 6132, 6133, 6136,
6137, 6139, 6140, 6142, 6143, 6144, 6148, 6190, 6287, 6285, 6194,
6167, 6206, 6271, 6555, 6416, 6513, 6374, 6400, 6325, 6371, 6440,
6185, 6451, 6487, 6419, 6529, 6311, 6551, 6378, 6181, 6210, 6557,
6203, 6175, 6150, 6470, 6204, 6166, 6355, 6477, 6534, 6286, 6561,
6336, 6232, 6354, 6531, 6297, 6296, 6156, 6516, 6324, 6290, 6256,
6176, 6410, 6277, 6254, 6173, 6151, 6499, 6248, 6546, 6280, 6152,
6461, 6283, 6362, 6255, 6428, 6272, 6266, 6550, 6344, 6247, 6372,
6454, 6237, 6539, 6387, 6215, 6251, 6330, 6180, 6515, 6238, 6201,
6549, 6409, 6313, 6212, 6221, 6480, 6522, 6223, 6249, 6331, 6305,
6352, 6484, 6357, 6281, 6257, 6432, 6545, 6345, 6379, 6236, 6498,
6479, 6363, 6346, 6284, 6300, 6288, 6507, 6200, 6413, 6423, 6350,
6326, 6229, 6368, 6386, 6434, 6509, 6244, 6293, 6530, 6427, 6453,
6508, 6337, 6250, 6327, 6466, 6339, 6341, 6373, 6430, 6494, 6165,
6188, 6207, 6463, 6431, 6268, 6230, 6351, 6485, 6383, 6364, 6239,
6160, 6235, 6162, 6528, 6007, 6009, 6011, 6012, 6015, 6017, 6020,
6022, 6023, 6029, 6032, 6036, 6038, 6039, 6040, 6041, 6045, 6049,
6050, 6052, 6056, 6060, 6061, 6062, 6063, 6064, 6065, 6069, 6072,
6081, 6082, 6083, 6086, 6087, 6089, 6090, 6091, 6093, 6097, 6099,
6101, 6108, 6112, 6116, 6119, 6120, 6122, 6123, 6125, 6134, 6135,
6138, 6141, 6145, 6147, 6198, 6233, 6217, 6258, 6161, 6242, 6218,
6323, 6178, 6279, 6476, 6514, 6262, 6321, 6460, 6308, 6224, 6205,
6208, 6302, 6195, 6153, 6532, 6500, 6492, 6240, 6457, 6273, 6422,
6367, 6318, 6490, 6228, 6496, 6227, 6243, 6414, 6415, 6197, 6267,
6306, 6177, 6155, 6260, 6459, 6264, 6154, 6170, 6263, 6304, 6259,
6171, 6202, 6292, 6269, 6481, 6322, 6493, 6526, 6519, 6486, 6226,
6169, 6450, 6445, 6261, 6438, 6252, 6468, 6220, 6505, 6299, 6193,
6149, 6317, 6222, 6189, 6394, 6231, 6553, 6241, 6164, 6234, 6158,
6216, 6375, 6168, 6452, 6265, 6537, 6369, 6294, 6488, 6214, 6291,
6191, 6183, 6211, 6276, 6437, 6199, 6472, 6335, 6245, 6489, 6448,
6504, 6506, 6347, 6253, 6163, 6316, 6502, 6246, 6179, 6473, 6282,
6510, 6482, 6192, 6196, 6275, 6527, 6186, 6439, 6475, 6533, 6289,
6503, 6298, 6436, 6225, 6376), sexo_s1 = structure(c(1, 0, 0,
1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0,
1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1,
1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0,
0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0,
1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0,
1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0,
1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1,
1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1,
0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0,
0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1,
0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0,
1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1,
0, 1, 0, 1, 1), label = "Sexo", format.spss = "F2.0", labels = c(Hombre = 0,
Mujer = 1), class = c("haven_labelled", "vctrs_vctr", "double"
)), edad_s1 = c(66, 67, 62, 66, 73, 61, 68, 62, 65, 65, 60, 60,
62, 65, 71, 70, 72, 70, 69, 73, 70, 61, 64, 56, 68, 74, 73, 67,
68, 65, 67, 67, 70, 63, 61, 66, 72, 61, 61, 63, 65, 68, 67, 66,
68, 63, 74, 71, 57, 62, 70, 74, 61, 61, 59, 62, 65, 67, 59, 65,
68, 65, 65, 60, 69, 71, 65, 55, 63, 63, 61, 64, 71, 63, 60, 72,
62, 64, 70, 71, 71, 64, 64, 64, 71, 69, 55, 65, 64, 72, 64, 71,
69, 68, 75, 70, 72, 67, 60, 71, 60, 57, 65, 64, 66, 56, 57, 72,
64, 74, 67, 67, 60, 61, 66, 69, 72, 75, 60, 68, 66, 64, 62, 68,
68, 66, 67, 63, 69, 66, 65, 61, 56, 67, 63, 62, 68, 63, 64, 58,
72, 70, 70, 63, 63, 75, 65, 66, 64, 65, 71, 64, 67, 70, 61, 72,
71, 74, 60, 65, 75, 60, 58, 65, 58, 71, 69, 64, 75, 60, 64, 64,
66, 67, 66, 60, 64, 60, 57, 61, 63, 63, 70, 73, 71, 63, 66, 67,
61, 73, 68, 66, 63, 60, 64, 69, 66, 69, 60, 62, 62, 63, 63, 73,
58, 69, 55, 63, 60, 64, 66, 60, 63, 63, 61, 66, 60, 65, 59, 62,
72, 67, 68, 68, 61, 69, 70, 72, 60, 70, 61, 70, 65, 69, 72, 74,
58, 63, 63, 74, 66, 65, 66, 61, 66, 60, 63, 66, 72, 69, 70, 63,
66, 60, 67, 62, 57, 71, 63, 57, 58, 66, 67, 65, 60, 62, 61, 68,
70, 57, 62, 68, 67, 65, 63, 67, 65, 63, 62, 70, 61, 59, 64, 72,
67, 59, 67, 66, 64, 69, 70, 68, 74, 60, 63, 69, 68, 61, 61, 61,
61, 63, 66, 68, 68, 72, 71, 62, 69, 68, 70, 69, 62, 63, 62, 69,
73, 58, 71, 71, 69, 62, 66, 69, 65, 69, 59, 58, 72, 62, 67, 62,
66, 72, 56, 70, 70, 71, 59, 70, 61, 62, 69, 63, 68, 60, 55, 60,
61, 63, 67, 68, 66, 67, 64, 71, 65, 69, 61, 63, 73, 62, 72, 70,
63, 56, 62, 59, 68, 67, 68, 72, 58, 63, 62, 65, 70, 71, 67, 59,
61, 69, 66, 71, 65, 68, 60, 68, 69, 63, 66, 72, 74, 62, 63, 74,
63, 66, 60, 65, 58, 73, 73, 71, 58, 68, 61), peso1_v00 = c(71.3,
106, 76.8, 69.9, 85.1, 83, 102.7, 110.3, 80, 74.6, 95.9, 99,
103.2, 65, 81.2, 90.3, 82.4, 78, 66.7, 97.3, 85.3, 79.9, 73.2,
109.6, 71.1, 91.5, 60, 68.2, 70.8, 76.8, 96, 75.4, 63.9, 118.7,
76.3, 120.1, 80.6, 92.4, 82.6, 80.1, 79.6, 82, 86.2, 90.4, 57.8,
66.3, 63.7, 109.7, 103.4, 85, 77.6, 96.8, 92.5, 76.9, 99.5, 78.6,
95.5, 89.3, 90.4, 70.1, 96.2, 75, 72.5, 67.1, 82.7, 82.8, 104.4,
113.2, 78, 65.5, 123.5, 98.9, 78.7, 90.7, 106.6, 72, 84.2, 91,
71.6, 93.4, 105.4, 122, 87.6, 86.4, 68, 85.5, 91.3, 66, 85.8,
103.4, 98.7, 94.8, 83, 83.8, 92.8, 77.7, 66, 92.6, 98.3, 85.4,
81.2, 107, 132.5, 108.2, 112.4, 102, 88.9, 93.1, 74.8, 90.6,
74.4, 90, 101.8, 94.5, 86.5, 95.8, 84, 95.5, 76.7, 78.4, 72.6,
76.2, 97.9, 85.5, 129, 88.9, 92.5, 85.4, 94.2, 102.1, 80.5, 80.5,
105.7, 75.6, 102.5, 70.5, 105.5, 76.3, 101, 105.2, 83.5, 93.7,
90.9, 107.5, 98, 77.5, 103.1, 91.3, 72.1, 103.7, 91.6, 81.6,
92.4, 86.5, 75, 77.9, 102.5, 98, 82.1, 86.5, 77.5, 81.9, 112,
91.2, 97.4, 86.5, 70, 82, 76.7, 87.8, 104.1, 71.1, 84.4, 93.5,
76.8, 91.5, 65.3, 94.5, 88.3, 86.8, 65.2, 88.2, 66.5, 74, 84.1,
85, 101.2, 85.3, 68.5, 91.5, 89, 81.8, 85, 86, 111.4, 98.8, 90,
84.2, 62.7, 95, 85.8, 115.4, 76.2, 93.8, 107, 89.8, 101.5, 88,
88.2, 103.8, 78.2, 101, 80.5, 69.4, 85.6, 79.5, 110.2, 84.8,
99.8, 110.9, 69.7, 93.3, 66.7, 97.5, 82.4, 73.7, 80.2, 81.1,
73, 101.6, 93.6, 76, 103.2, 77.5, 88.8, 65.7, 117, 82.5, 76.2,
74.2, 91.9, 98.5, 106.5, 78.2, 85.1, 100.7, 79.2, 67.1, 70, 94.4,
91.5, 91.6, 87.1, 91.8, 95.5, 125.9, 110.8, 95.3, 102.9, 116.4,
91.1, 87.9, 78.6, 72, 108.2, 82.7, 117.2, 88.7, 110.1, 78.2,
83.1, 83, 88.2, 107.5, 90.7, 73.1, 98.8, 100.1, 88.9, 107, 100.7,
90, 82.2, 87.3, 80.2, 82.5, 107.9, 92.5, 89, 79.1, 99.5, 110.8,
72.7, 107.7, 127.6, 92.9, 66.8, 91.6, 97, 94.5, 99, 94.9, 77,
97.2, 90.7, 75, 114.6, 78.4, 85.6, 76.8, 88, 80.8, 98, 78.9,
82.5, 73.7, 79, 119.8, 86.6, 78.7, 65.4, 74.5, 82.1, 90.9, 81.5,
89.9, 90, 95.9, 77.3, 109, 71.9, 93, 57.9, 88.3, 93.4, 99.8,
95, 75.8, 88.2, 78.4, 83.1, 85, 87.7, 88.8, 92.2, 78.8, 93.2,
80.2, 76.8, 115.5, 93.4, 86.5, 70.3, 98.2, 91, 80.2, 88.4, 83,
68, 72.8, 80.9, 102.5, 90.3, 83.7, 70.1, 115.6, 62, 86.7, 92.6,
97.8, 75.1, 88.2, 110.9, 83, 101, 92.5, 94.2, 72.5, 70.5, 94.6,
87.5, 75.2, 85.3, 69.8, 80.4, 70, 92.5, 88.5, 81.7, 105, 106.2,
91.3, 110.2, 84.7, 95.8, 96.6, 90.5, 69.5, 98, 71, 137.5, 101.1,
73.5, 64.8, 101.5, 85.1, 80.2), cintura1_v00 = c(105, 124, 104,
100.5, 114, 100.5, 120.5, 132, 109, 101.5, 113, 123, 114, 95,
102, 115, 117, 105.5, 89, 109, 104, 108.5, 102.5, 122.5, 95.4,
119, 94, 96, 98.3, 100, 119.2, 102.8, 96, 136.2, 109.5, 132,
103, 113, 98.5, 106.8, 117.5, 102.3, 102, 116, 94, 94.3, 93.2,
127, 119, 96.5, 105.5, 113.5, 105.2, 112.8, 116.5, 102, 126,
114.5, 114, 103, 108, 107, 104, 101, 110, 110, 125, 128, 106,
97, 127.5, 118, 112, 104, 126.5, 107, 107, 109.5, 104, 107, 123,
128.5, 123, 109.5, 99, 102.5, 109, 97, 108, 120, 113.5, 123,
114.5, 105, 107, 106, 97, 115, 110.5, 109, 104.5, 129, 139, 120,
121, 119, 108, 116.5, 104, 111.5, 100, 102, 117, 111, 116.5,
124, 117, 112, 96, 104, 92, 110, 112.5, 110, 140, 119.5, 116,
104, 114, 114, 116, 106.5, 116, 108, 114.5, 101.5, 116, 100,
116, 116.5, 108.5, 113, 119, 116, 118, 105, 122, 121.5, 92.5,
124.5, 115.5, 110, 116, 106, 100, 106.5, 126.5, 123, 104, 98,
94, 115, 121.5, 107.5, 111.5, 102.5, 101, 106.5, 103, 101, 120.5,
117, 104.5, 116, 103, 112, 96, 118.5, 108.5, 107, 102.5, 106,
103, 105.5, 97.5, 103, 116.5, 114.5, 94, 112, 113.5, 110, 104,
113, 124, 112, 114.5, 113.4, 90, 114, 106, 123, 106.5, 117, 116.5,
107.5, 115, 103, 103, 118, 98, 103.5, 112.5, 95, 106, 103, 114,
113, 122, 126, 102.5, 117, 85, 115, 100.2, 106, 107.5, 110, 102.7,
126.3, 111.6, 103, 125, 107.7, 112, 91.5, 127.5, 117.7, 101,
104.5, 117, 121.5, 130.7, 105.2, 117.2, 116.2, 109, 97, 98, 115,
116, 107.5, 122, 116.7, 116.8, 131.8, 118.5, 115.5, 123.2, 122.6,
115, 115, 113.2, 101.5, 119, 112, 134, 102, 131, 98, 110, 110,
119, 126, 113.5, 102.5, 118.5, 119, 120, 121, 116.5, 114.5, 100,
106.5, 104.5, 113, 128, 118, 107.5, 103, 125.5, 133, 103.5, 121,
128, 115, 93.5, 107, 118.5, 110, 107, 110, 98, 112.5, 114.5,
96, 130, 104, 106, 104, 103, 119, 132, 105.5, 106.5, 98, 109,
135, 119.5, 104, 92, 106, 105.5, 107, 107, 113, 106, 104.5, 104,
115.5, 104.5, 112, 94, 107.5, 110, 108, 113, 112.5, 120, 105,
110, 112, 124.5, 116, 107, 105, 111.5, 108, 101.5, 129.5, 111,
106, 102, 119.5, 113, 111, 109, 109, 100, 101, 110, 113.5, 114.5,
107, 94, 126, 90, 109, 111, 120, 109, 116, 125, 120, 112, 119,
114.5, 104.5, 90, 114, 117, 110.2, 104.5, 100, 106, 109, 108,
108, 106.5, 111, 110, 129.5, 127, 108, 121, 117.5, 114, 101.5,
109, 102.5, 131, 125, 109, 94, 121, 113, 103), tasis2_e_v00 = c(138,
151, 134, 164, 138, 156, 144, 137, 130, 146, 131, 139, 122, 156,
141, 161, 137, 130, 152, 120, 154, 165, 138, 169, 147, 155, 152,
138, 136, 144, 123, 154, 138, 137, 123, 139, 145, 146, 145, 115,
123, 137, 146, 121, 145, 109, 130, 138, 137, 123, 146, 131, 131,
123, 123, 131, 145, 137, 146, 139, 160, 138, 109, 138, 178, 146,
130, 130, 138, 147, 145, 138, 154, 144, 139, 136, 138, 149, 123,
138, 130, 147, 154, 154, 122, 135, 139, 140, 146, 129, 145, 156,
137, 116, 161, 146, 154, 167, 138, 146, 138, 137, 126, 129, 128,
123, 138, 136, 121, 154, 146, 139, 138, 137, 148, 131, 146, 137,
149, 138, 173, 147, 130, 156, 153, 148, 152, 114, 124, 149, 146,
138, 131, 161, 143, 130, 155, 131, 131, 131, 144, 138, 146, 132,
139, 146, 122, 130, 124, 154, 162, 114, 167, 138, 132, 130, 147,
158, 153, 139, 145, 156, 140, 124, 131, 143, 146, 145, 123, 138,
138, 144, 155, 124, 145, 149, 139, 138, 131, 145, 130, 144, 160,
160, 139, 140, 123, 113, 123, 167, 157, 138, 113, 132, 146, 130,
137, 168, 143, 115, 138, 139, 129, 136, 146, 145, 140, 115, 123,
147, 145, 138, 138, 131, 135, 146, 131, 171, 147, 151, 176, 147,
146, 124, 167, 123, 129, 149, 144, 146, 125, 123, 130, 145, 122,
130, 146, 160, 131, 130, 139, 144, 126, 131, 106, 117, 153, 138,
100, 146, 115, 137, 131, 161, 145, 161, 114, 123, 123, 127, 154,
138, 146, 115, 140, 139, 122, 146, 162, 130, 118, 145, 158, 123,
147, 161, 139, 147, 106, 138, 131, 138, 143, 138, 133, 129, 131,
143, 136, 140, 147, 155, 146, 138, 146, 147, 134, 129, 145, 145,
136, 147, 146, 139, 145, 175, 153, 152, 138, 139, 131, 123, 152,
147, 124, 145, 154, 138, 148, 146, 146, 129, 147, 130, 164, 132,
145, 132, 146, 133, 144, 136, 145, 148, 138, 146, 138, 113, 131,
145, 146, 130, 146, 164, 138, 137, 139, 172, 130, 142, 165, 138,
145, 131, 131, 139, 130, 122, 130, 147, 154, 122, 130, 121, 137,
154, 132, 124, 153, 139, 146, 154, 138, 125, 124, 137, 147, 122,
145, 131, 183, 146, 115, 138, 143, 145, 149, 132, 152, 130, 163,
130, 146, 144, 117, 131, 121, 146, 130, 147, 169, 138, 133, 149,
123, 161, 130), tadias2_e_v00 = c(76, 71, 59, 63, 84, 67, 79,
76, 72, 88, 50, 65, 71, 60, 59, 78, 79, 64, 67, 53, 81, 93, 80,
103, 76, 77, 71, 79, 63, 80, 49, 87, 68, 69, 69, 72, 75, 88,
79, 69, 68, 67, 76, 75, 71, 67, 63, 76, 77, 60, 68, 83, 85, 71,
84, 65, 79, 59, 81, 73, 66, 72, 74, 68, 65, 80, 65, 96, 77, 82,
88, 60, 79, 78, 85, 73, 76, 63, 91, 72, 69, 84, 76, 80, 56, 84,
96, 83, 80, 84, 71, 89, 72, 77, 97, 81, 60, 76, 87, 80, 88, 87,
73, 71, 80, 78, 76, 75, 67, 73, 88, 69, 88, 83, 58, 65, 57, 71,
81, 75, 70, 87, 84, 82, 72, 86, 73, 71, 65, 82, 80, 68, 92, 72,
79, 79, 94, 87, 77, 64, 78, 85, 65, 78, 77, 81, 76, 68, 62, 77,
95, 78, 89, 78, 87, 68, 82, 65, 75, 81, 64, 57, 93, 66, 81, 80,
88, 84, 54, 72, 81, 69, 98, 76, 79, 79, 77, 84, 81, 65, 69, 76,
74, 75, 78, 74, 68, 68, 72, 85, 93, 72, 68, 68, 92, 71, 75, 74,
91, 81, 80, 78, 63, 71, 80, 75, 82, 61, 65, 81, 79, 63, 72, 59,
89, 72, 58, 89, 82, 88, 110, 81, 72, 80, 89, 72, 76, 82, 78,
79, 81, 76, 82, 56, 75, 55, 89, 75, 76, 63, 68, 79, 67, 61, 60,
61, 76, 71, 50, 68, 68, 63, 64, 62, 79, 81, 68, 62, 65, 78, 93,
82, 80, 72, 85, 69, 84, 68, 74, 80, 71, 84, 91, 57, 90, 71, 78,
61, 46, 82, 72, 81, 78, 60, 79, 71, 76, 60, 79, 66, 73, 66, 84,
85, 73, 86, 64, 67, 85, 79, 84, 88, 72, 66, 92, 78, 88, 69, 72,
73, 65, 57, 90, 65, 49, 83, 80, 77, 90, 73, 80, 74, 78, 76, 81,
81, 94, 74, 80, 73, 79, 82, 84, 83, 92, 65, 73, 58, 58, 83, 79,
64, 80, 98, 79, 79, 78, 89, 76, 86, 95, 76, 79, 84, 73, 69, 69,
68, 64, 101, 74, 68, 61, 75, 75, 73, 58, 77, 73, 74, 49, 91,
72, 56, 66, 71, 77, 72, 80, 96, 94, 60, 65, 57, 55, 79, 65, 73,
77, 87, 90, 64, 80, 72, 70, 84, 56, 60, 82, 72, 80, 65, 84, 83,
75, 86, 60), p17_total_v00 = c(7, 2, 9, 7, 12, 5, 8, 8, 14, 4,
8, 4, 14, 7, 9, 7, 12, 11, 10, 14, 10, 9, 14, 10, 9, 8, 8, 12,
9, 10, 7, 9, 7, 4, 6, 5, 5, 6, 3, 5, 6, 4, 10, 7, 8, 6, 6, 8,
4, 8, 6, 9, 6, 8, 8, 9, 4, 5, 5, 7, 5, 8, 6, 7, 6, 6, 6, 6, 4,
8, 2, 5, 7, 8, 7, 10, 12, 8, 9, 4, 6, 10, 5, 5, 11, 6, 4, 8,
8, 12, 11, 4, 9, 10, 13, 3, 5, 9, 5, 8, 4, 4, 12, 7, 5, 8, 8,
5, 7, 6, 5, 8, 4, 8, 11, 10, 8, 8, 9, 9, 6, 6, 7, 11, 6, 5, 7,
10, 8, 10, 6, 7, 7, 5, 11, 11, 6, 6, 6, 13, 4, 9, 4, 6, 5, 7,
7, 10, 5, 5, 8, 6, 5, 6, 7, 8, 8, 6, 6, 7, 2, 8, 8, 9, 5, 9,
5, 12, 8, 6, 7, 6, 10, 12, 7, 10, 7, 9, 4, 6, 12, 10, 8, 6, 9,
9, 9, 8, 4, 14, 7, 11, 11, 2, 12, 7, 9, 11, 11, 9, 6, 7, 14,
5, 4, 8, 5, 7, 7, 7, 12, 6, 8, 7, 2, 6, 6, 9, 7, 10, 8, 11, 9,
8, 6, 11, 9, 9, 7, 7, 4, 7, 5, 9, 9, 6, 6, 8, 8, 8, 3, 6, 6,
6, 11, 11, 6, 6, 7, 11, 11, 6, 8, 10, 7, 8, 8, 13, 5, 5, 4, 7,
7, 10, 9, 3, 5, 9, 9, 6, 3, 5, 4, 5, 5, 9, 5, 8, 5, 8, 10, 7,
12, 9, 9, 4, 7, 9, 3, 8, 3, 2, 6, 7, 4, 5, 10, 9, 3, 5, 5, 5,
9, 5, 9, 8, 12, 9, 8, 7, 5, 9, 6, 11, 10, 7, 9, 9, 6, 8, 3, 4,
10, 6, 8, 7, 9, 4, 8, 11, 6, 9, 10, 10, 6, 10, 6, 9, 5, 7, 6,
9, 7, 7, 9, 7, 6, 7, 2, 5, 4, 4, 5, 5, 7, 10, 5, 10, 8, 6, 10,
4, 7, 10, 6, 10, 9, 7, 8, 7, 7, 10, 5, 6, 5, 9, 8, 8, 6, 5, 3,
9, 5, 7, 5, 10, 9, 4, 7, 6, 13, 7, 3, 6, 6, 7, 4, 9, 9, 6, 5,
10, 8, 10, 6, 11, 10), geaf_tot_v00 = c(2517.48, 2769.23, 97.9,
5146.85, 664.34, 1557.11, 1678.32, 1300.7, 7574.83, 1678.32,
3543.12, 3188.81, 2293.71, 111.89, 4755.24, 5687.65, 3153.85,
2958.04, 2042.89, 1535.66, 1678.32, 1931.93, 432.63, 292.77,
2331, 6834.5, 2530.54, 1272.73, 909.09, 1986.01, 559.44, 2321.68,
3356.64, 559.44, 559.44, 86.71, 2097.9, 3384.62, 2349.65, 2713.29,
839.16, 4755.24, 1622.38, 615.38, 1678.32, 395.34, 2937.06, 4382.28,
2797.2, 2388.81, 8937.06, 1678.32, 2237.76, 5679.25, 3804.2,
1678.32, 1818.18, 839.16, 4643.36, 2531.47, 6414.92, 5622.38,
4895.1, 2377.62, 447.55, 1371.56, 1678.32, 0, 2834.5, 4335.66,
455.01, 1398.6, 6293.71, 559.44, 2237.76, 839.16, 4965.03, 923.08,
2517.48, 3482.52, 1958.04, 3356.64, 2937.06, 1608.39, 9860.14,
2797.2, 2293.71, 6853.15, 5034.97, 5559.44, 1678.32, 2937.06,
466.2, 1332.4, 1440.56, 83.92, 2517.48, 839.16, 839.16, 2013.99,
3034.97, 251.75, 6433.57, 1930.07, 643.36, 839.16, 5902.1, 2834.5,
3076.92, 3251.75, 4587.41, 93.24, 1076.92, 4895.1, 10349.65,
3356.64, 3356.64, 839.16, 699.3, 5524.48, 13986.01, 0, 3524.48,
1706.29, 2237.76, 3916.08, 279.72, 5995.34, 2517.48, 2536.13,
2517.48, 895.1, 279.72, 2237.76, 2314.69, 581.82, 27.97, 1230.77,
5118.88, 1818.18, 3076.92, 7160.84, 4191.14, 3636.36, 1461.54,
839.16, 1118.88, 1678.32, 447.55, 2382.28, 3356.64, 895.1, 3468.53,
4055.94, 1398.6, 3121.68, 186.48, 7370.63, 587.41, 671.33, 1980.42,
4475.52, 1818.18, 3356.64, 419.58, 1601.4, 1202.8, 1258.74, 1300.7,
1433.57, 55.94, 335.66, 1048.95, 2657.34, 350.58, 559.44, 6013.99,
1048.95, 951.05, 1678.32, 372.96, 5734.27, 671.33, 3650.35, 223.78,
7160.84, 1706.29, 559.44, 2853.15, 7780.89, 6951.05, 5601.4,
4335.66, 1678.32, 1006.99, 3216.78, 3475.52, 3104.9, 3118.88,
2769.23, 6242.42, 2256.41, 2517.48, 0, 1501.17, 6937.06, 4944.06,
5524.48, 923.08, 1678.32, 3776.22, 7496.5, 1370.63, 2797.2, 615.38,
4755.24, 279.72, 3356.64, 139.86, 671.33, 895.1, 9097.44, 8053.15,
139.86, 3174.83, 1444.29, 1594.41, 863.4, 3358.51, 1670.86, 1090.91,
1454.55, 2452.21, 3084.38, 1678.32, 231, 2517.48, 198.14, 4195.8,
2517.48, 1678.32, 1678.32, 5927.27, 3776.22, 2265.73, 4128.21,
2517.48, 2101.63, 7272.73, 1678.32, 1734.27, 3776.22, 167.83,
2386.01, 1678.32, 4219.11, 1659.67, 2800.93, 1286.71, 4055.94,
559.44, 1398.6, 2489.51, 3184.15, 1188.81, 1286.71, 671.33, 2713.29,
5874.13, 3412.59, 1468.53, 1525.41, 0, 339.39, 4820.51, 559.44,
157.34, 1118.88, 111.89, 839.16, 783.22, 2144.52, 1491.84, 8400,
2601.4, 923.08, 2279.72, 2055.94, 6853.15, 1822.84, 1678.32,
10069.93, 2601.4, 1384.62, 3356.64, 4475.52, 993.94, 391.61,
1454.55, 1146.85, 5734.27, 895.1, 2937.06, 839.16, 1832.17, 839.16,
1174.83, 993.01, 1118.88, 4475.52, 1538.46, 6293.71, 0, 2741.26,
1976.69, 1734.27, 10209.79, 1324.01, 111.89, 1720.28, 3398.6,
0, 6881.12, 335.66, 4461.54, 3412.59, 1070.4, 5594.41, 3356.64,
6489.51, 1846.15, 0, 2377.62, 4615.38, 3916.08, 3356.64, 419.58,
839.16, 3020.98, 3832.17, 839.16, 6153.85, 419.58, 302.1, 1685.78,
1398.6, 83.92, 2592.07, 839.16, 0, 3356.64, 0, 69.93, 1657.34,
1995.34, 419.58, 1557.11, 3860.14, 3566.43, 4405.59, 5930.07,
547.79, 629.37, 1441.49, 1607.46, 1135.66, 3524.48, 4377.62,
279.72, 0, 5594.41, 2573.43, 9006.99, 335.66, 1762.24, 2601.4,
8146.85, 3524.48, 2237.76, 2685.31, 1335.66, 1153.85, 1118.88,
1258.74, 419.58, 3636.36, 4160.84, 1678.32, 2212.12, 1398.6,
2517.48, 5594.41, 2517.48, 4979.02, 783.22, 4657.34, 559.44,
559.44, 1958.04, 2797.2, 773.89, 839.16, 6228.44, 2256.41, 419.58,
1678.32, 3286.71)), row.names = c(NA, 407L), class = "data.frame")
The columns to loop through
vars<-c( "peso1_v00", "cintura1_v00", "tasis2_e_v00", "tadias2_e_v00", "p17_total_v00", "geaf_tot_v00" )
The loop
for (v in vars) {
print(
boxplot(df[, v], main = v)
)
}
Then, there are generated several boxplots in the viewer. The problem is with my real database I have multiple plots, and I am looking for something to save it all at once. The solutions proposed in previous threads are not working
Thanks for the help in advance

To save each boxplot you could modify your loop like this:
for (v in vars) {
png(file=paste("plot",v,".png",sep=""))
boxplot(df[, v], main = v)
dev.off()
}

Related

Is there any way to speed up this loop?

I am pretty new to programming and am looking for help to speed up this process. At the current rate the loop will take approximately 24-30 hours.
library(readxl)
## Load data##
A02_pre <- read_excel("C:/Users/Gebruiker/Desktop/Nieuwe map/Mappen participanten/Pre/A02/A02_vivo13_wk2_4mei_90uur.xlsx",
col_types = c("text", "text", "text",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"text", "text", "text", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "text", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "text", "numeric",
"numeric", "numeric", "numeric",
"text", "text"))
A02_pre_hr <- read_excel("C:/Users/Gebruiker/Desktop/Nieuwe map/Mappen participanten/Pre/A02/A02 - 17817.xlsx", )
View(A02_pre_hr)
### Convert string to date time ##
A02_pre$`ns1:Time` = as.POSIXct(A02_pre$`ns1:Time`,
format="%Y-%m-%dT%H:%M:%OSZ",tz ="GMT")
A02_pre_hr$start = as.POSIXct(substr(A02_pre_hr$start,1,nchar(A02_pre_hr$start)-4),
format="%Y-%m-%d %H:%M:%S",tz ="GMT")
## Test dates ##
A02_pre$`ns1:Time`[1] < A02_pre_hr$start[1]
measurementTime <- A02_pre$`ns1:Time`
heartRate <- A02_pre$`ns1:Value4`
**### Run for loop starting here
toCheck = 1:length(measurementTime)
maxj = 0
for(i in 1:(nrow(A02_pre_hr)-1)){
activityStartTime = A02_pre_hr$start[i]
activityEndTime= A02_pre_hr$start[i+1]
heartRateSum = 0
heartRateCount = 0
## Loop over heart rate measurements & assign average to activity
for(j in toCheck[toCheck>maxj]){
if(measurementTime[j]>=activityStartTime & measurementTime[j]<= activityEndTime){
heartRateCount = heartRateCount+1
heartRateSum = heartRateSum + heartRate[j]
}else if(heartRateCount>0){
break;
}
}
averageHeartRate = NA
if(heartRateCount>0){
averageHeartRate = heartRateSum / heartRateCount
}
A02_pre_hr$averageHeartRate[i] = averageHeartRate
print(i)
}**
### Summary ####
by(A02_pre_hr$averageHeartRate, A02_pre_hr$class, summary)
dput(A02_pre)
structure(list(`ns1:Value4` = c(78, 77, 82, 87, 92, 97, 99, 100,
101, 102, 103, 104, 105, 106, 105, 104, 103, 102, 101, 100, 99,
98, 97, 96, 97, 98, 99, 101, 102, 104, 105, 106, 105, 104, 103,
102, 101, 100, 99, 98, 97, 96, 97, 98, 99, 100, 101, 99, 100,
101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 94, 98,
100, 101, 99, 97, 96, 95, 94, 93, 94, 93, 92, 91, 90, 89, 90,
89, 88, 85, 83, 82, 81, 80, 79, 78, 77, 75, 74, 72, 71, 70, 69,
68, 67, 66, 65, 64, 63, 62, 63, 62, 65, 68, 71, 73, 75, 77, 79,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 90, 89, 88, 87, 86,
85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 75, 74, 73, 72,
71, 70, 69, 68, 67, 66, 65, 67, 68, 69, 68, 67, 71, 74, 77, 79,
81, 83, 85, 87, 88, 90, 89, 88, 89, 88, 87, 86, 85, 84, 83, 85,
87, 89, 90, 92, 91, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101, 102, 103, 107, 111, 114, 117, 117, 116, 115, 116, 117, 118,
119, 120, 119, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108,
107, 106, 105, 104, 103, 102, 100, 99, 98, 97, 96, 95, 94, 93,
92, 90, 88, 89, 90, 91, 93, 94, 95, 96, 95, 94, 93, 92, 91, 90,
89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 98, 97, 96, 97, 98,
99, 100, 99, 98, 97, 98, 97, 96, 95, 94, 93, 92, 91, 90, 91,
90, 89, 87, 85, 83, 81, 80, 79, 80, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 95, 98, 102, 105, 107, 110, 112, 114, 116, 115, 114,
113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101,
100, 99, 98, 97, 96, 95, 94, 95, 94, 95, 96, 95, 94, 93, 94,
93, 92, 91, 90, 91, 92, 93, 94, 95, 96, 97, 99, 100, 103, 105,
106, 108, 107, 106, 105, 104, 105, 106, 105, 104, 105, 106, 107,
106, 105, 104, 103, 104, 103, 104, 100, 99, 98, 97, 96, 95, 94,
93, 92, 93, 94, 95, 97, 98, 99, 100, 99, 100, 101, 100, 99, 100,
99, 98, 99, 100, 99, 100, 99, 97, 94, 91, 89, 90, 89, 88, 87,
85, 84, 85, 86, 87, 86, 85, 83, 81, 80, 79, 78, 77, 76, 75, 74,
72, 71, 70, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 80, 81, 83,
84, 85, 86, 85, 84, 85, 86, 87, 88, 87, 88, 89, 90, 91, 92, 91,
89, 88, 87, 88, 89, 90, 95, 96, 95, 94, 93, 92, 91, 90, 89, 88,
87, 86, 85, 84, 85, 86, 87, 86, 84, 85, 86, 87, 86, 87, 86, 85,
84, 83, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75), `ns1:Time` = structure(c(1525439315,
1525439321, 1525439325, 1525439326, 1525439327, 1525439328, 1525439329,
1525439330, 1525439332, 1525439333, 1525439335, 1525439337, 1525439339,
1525439346, 1525439355, 1525439400, 1525439401, 1525439402, 1525439403,
1525439405, 1525439406, 1525439407, 1525439409, 1525439410, 1525439413,
1525439414, 1525439415, 1525439417, 1525439418, 1525439419, 1525439421,
1525439425, 1525439429, 1525439430, 1525439432, 1525439433, 1525439434,
1525439436, 1525439437, 1525439438, 1525439440, 1525439441, 1525439443,
1525439444, 1525439445, 1525439447, 1525439448, 1525439451, 1525439452,
1525439455, 1525439457, 1525439458, 1525439460, 1525439461, 1525439462,
1525439463, 1525439464, 1525439466, 1525439467, 1525439469, 1525439471,
1525439473, 1525439475, 1525439476, 1525439477, 1525439478, 1525439479,
1525439480, 1525439481, 1525439483, 1525439484, 1525439486, 1525439488,
1525439489, 1525439490, 1525439491, 1525439493, 1525439494, 1525439495,
1525439496, 1525439497, 1525439499, 1525439500, 1525439502, 1525439504,
1525439506, 1525439508, 1525439510, 1525439511, 1525439514, 1525439515,
1525439516, 1525439517, 1525439519, 1525439526, 1525439531, 1525439536,
1525439537, 1525439539, 1525439540, 1525439541, 1525439544, 1525439563,
1525439577, 1525439587, 1525439588, 1525439589, 1525439590, 1525439591,
1525439592, 1525439593, 1525439594, 1525439595, 1525439596, 1525439597,
1525439598, 1525439599, 1525439600, 1525439601, 1525439602, 1525439604,
1525439606, 1525439607, 1525439608, 1525439610, 1525439611, 1525439613,
1525439614, 1525439616, 1525439618, 1525439620, 1525439621, 1525439623,
1525439626, 1525439628, 1525439630, 1525439633, 1525439636, 1525439638,
1525439640, 1525439641, 1525439644, 1525439647, 1525439651, 1525439656,
1525439660, 1525439666, 1525439673, 1525439680, 1525439690, 1525439703,
1525439704, 1525439705, 1525439717, 1525439728, 1525439732, 1525439733,
1525439734, 1525439735, 1525439736, 1525439737, 1525439738, 1525439739,
1525439740, 1525439741, 1525439742, 1525439743, 1525439749, 1525439751,
1525439754, 1525439756, 1525439758, 1525439760, 1525439762, 1525439765,
1525439766, 1525439767, 1525439768, 1525439769, 1525439770, 1525439771,
1525439773, 1525439774, 1525439775, 1525439776, 1525439777, 1525439780,
1525439782, 1525439784, 1525439785, 1525439786, 1525439788, 1525439791,
1525439797, 1525439811, 1525439812, 1525439813, 1525439814, 1525439815,
1525439816, 1525439819, 1525439820, 1525439821, 1525439822, 1525439823,
1525439824, 1525439828, 1525439829, 1525439830, 1525439831, 1525439832,
1525439833, 1525439834, 1525439835, 1525439836, 1525439837, 1525439838,
1525439839, 1525439840, 1525439841, 1525439842, 1525439843, 1525439844,
1525439846, 1525439847, 1525439849, 1525439850, 1525439852, 1525439853,
1525439855, 1525439857, 1525439858, 1525439859, 1525439860, 1525439863,
1525439864, 1525439865, 1525439866, 1525439870, 1525439872, 1525439880,
1525439885, 1525439887, 1525439888, 1525439889, 1525439891, 1525439892,
1525439894, 1525439895, 1525439896, 1525439898, 1525439899, 1525439901,
1525439904, 1525439907, 1525439915, 1525439925, 1525439964, 1525439965,
1525439966, 1525439967, 1525439970, 1525439975, 1525439987, 1525440021,
1525440052, 1525440053, 1525440055, 1525440066, 1525440098, 1525440100,
1525440101, 1525440102, 1525440104, 1525440105, 1525440106, 1525440108,
1525440109, 1525440111, 1525440113, 1525440114, 1525440115, 1525440116,
1525440117, 1525440118, 1525440120, 1525440122, 1525440124, 1525440125,
1525440130, 1525440137, 1525440141, 1525440145, 1525440171, 1525440186,
1525440189, 1525440194, 1525440202, 1525440203, 1525440204, 1525440205,
1525440206, 1525440207, 1525440208, 1525440209, 1525440210, 1525440211,
1525440213, 1525440214, 1525440216, 1525440218, 1525440220, 1525440222,
1525440225, 1525440228, 1525440231, 1525440235, 1525440238, 1525440241,
1525440246, 1525440251, 1525440256, 1525440267, 1525440268, 1525440269,
1525440276, 1525440303, 1525440314, 1525440316, 1525440321, 1525440330,
1525440331, 1525440332, 1525440334, 1525440351, 1525440364, 1525440408,
1525440409, 1525440411, 1525440412, 1525440416, 1525440422, 1525440424,
1525440427, 1525440430, 1525440436, 1525440454, 1525440457, 1525440458,
1525440459, 1525440460, 1525440461, 1525440462, 1525440468, 1525440473,
1525440479, 1525440503, 1525440508, 1525440509, 1525440513, 1525440530,
1525440550, 1525440551, 1525440552, 1525440556, 1525440557, 1525440558,
1525440563, 1525440572, 1525440576, 1525440579, 1525440607, 1525440609,
1525440614, 1525440615, 1525440616, 1525440618, 1525440620, 1525440621,
1525440623, 1525440627, 1525440628, 1525440629, 1525440630, 1525440635,
1525440637, 1525440640, 1525440642, 1525440643, 1525440649, 1525440655,
1525440668, 1525440676, 1525440692, 1525440714, 1525440725, 1525440729,
1525440731, 1525440732, 1525440733, 1525440734, 1525440735, 1525440736,
1525440737, 1525440744, 1525440746, 1525440747, 1525440748, 1525440749,
1525440750, 1525440752, 1525440756, 1525440757, 1525440767, 1525440768,
1525440769, 1525440770, 1525440771, 1525440772, 1525440773, 1525440775,
1525440778, 1525440780, 1525440783, 1525440785, 1525440786, 1525440787,
1525440789, 1525440790, 1525440792, 1525440795, 1525440799, 1525440803,
1525440808, 1525440810, 1525440813, 1525440818, 1525440820, 1525440821,
1525440822, 1525440826, 1525440831, 1525440838, 1525440839, 1525440848,
1525440850, 1525440852, 1525440853, 1525440855, 1525440857, 1525440858,
1525440862, 1525440867, 1525440871, 1525440907, 1525440908, 1525440909,
1525440910, 1525440911, 1525440914, 1525440916, 1525440917, 1525440918,
1525440919, 1525440920, 1525440938, 1525440952, 1525440962, 1525440970,
1525440975, 1525441002, 1525441014, 1525441015, 1525441017, 1525441019,
1525441020, 1525441021, 1525441024, 1525441028, 1525441033, 1525441034,
1525441036, 1525441048, 1525441054, 1525441056, 1525441057, 1525441078,
1525441079, 1525441081, 1525441083, 1525441085, 1525441087, 1525441089,
1525441091, 1525441093, 1525441096, 1525441098, 1525441100, 1525441103,
1525441106), tzone = "GMT", class = c("POSIXct", "POSIXt"))), row.names = c(NA,
-500L), class = c("tbl_df", "tbl", "data.frame"))
dput(A02_pre_hr)
structure(list(results_id = c(17817, 17817, 17817, 17817, 17817,
17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817,
17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817,
17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817,
17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817, 17817,
17817, 17817, 17817, 17817), start = structure(c(1525439377,
1525439400, 1525439460, 1525439520, 1525439580, 1525439640, 1525439700,
1525439760, 1525439781, 1525439783, 1525439820, 1525439880, 1525439940,
1525439954, 1525439957, 1525440000, 1525440051, 1525440053, 1525440058,
1525440060, 1525440120, 1525440180, 1525440240, 1525440300, 1525440360,
1525440420, 1525440480, 1525440540, 1525440600, 1525440660, 1525440720,
1525440780, 1525440840, 1525440900, 1525440960, 1525441020, 1525441080,
1525441140, 1525441200, 1525441260, 1525441320, 1525441380, 1525441440,
1525441500, 1525441560), tzone = "GMT", class = c("POSIXct",
"POSIXt")), duration = c(22.37, 60, 60, 60, 60, 60, 60, 21.04,
2.65, 36.32, 60, 60, 14.58, 3.07, 42.35, 51.38, 2.46, 4.78, 1.38,
60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60,
60, 60, 60, 60, 60, 60, 60, 60, 60, 60), class = c("standing",
"standing", "standing", "standing", "standing", "standing", "standing",
"standing", "walking", "standing", "standing", "standing", "standing",
"sitting", "standing", "standing", "walking", "standing", "lying",
"lying", "lying", "lying", "lying", "lying", "lying", "lying",
"lying", "lying", "lying", "lying", "lying", "lying", "lying",
"lying", "lying", "lying", "lying", "lying", "lying", "lying",
"lying", "lying", "lying", "lying", "lying"), steps = c(0, 0,
0, 0, 0, 0, 0, 0, 3, 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), mi = c(2, 12, 1, 11, 2, 102, 329, 285, 351, 193, 190, 225,
148, 361, 180, 226, 230, 379, 349, 212, 30, 125, 225, 36, 1,
1, 1, 2, 1, 1, 1, 1, 1, 10, 40, 8, 8, 9, 8, 9, 9, 10, 9, 7, 2
), aee = c(16, 21, 15, 20, 16, 64, 173, 151, 313, 107, 106, 123,
86, 129, 101, 123, 246, 197, 448, 272, 38, 160, 289, 46, 1, 1,
1, 2, 1, 1, 1, 1, 1, 12, 51, 10, 10, 11, 10, 11, 11, 12, 11,
9, 2), tee = c(91, 96, 90, 96, 91, 144, 265, 242, 421, 192, 191,
210, 169, 216, 186, 210, 346, 292, 571, 375, 116, 251, 394, 124,
74, 74, 74, 76, 74, 74, 74, 74, 74, 87, 130, 84, 84, 86, 84,
86, 86, 87, 86, 83, 76), met = c(1.145, 1.208, 1.139, 1.201,
1.145, 1.767, 3.18, 2.906, 6.847, 2.334, 2.315, 2.533, 2.054,
2.424, 2.253, 2.539, 5.12, 3.491, 7.275, 4.802, 1.516, 3.231,
5.036, 1.624, 0.992, 0.992, 0.992, 1.01, 0.992, 0.992, 0.992,
0.992, 0.992, 1.155, 1.697, 1.119, 1.119, 1.137, 1.119, 1.137,
1.137, 1.155, 1.137, 1.101, 1.01), counts = c(14, 138, 0, 256,
0, 2059, 7922, 2162, 394, 2952, 2886, 3888, 782, 62, 2163, 3041,
128, 560, 169, 3506, 432, 2024, 3825, 844, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 392, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0), averageHeartRate = c(104,
100.236842105263, 88.6904761904762, 64.9, 81.1944444444444, 70,
80.4347826086957, 90.1333333333333, 97, 108.285714285714, 103.775,
93.2941176470588, NA, NA, 97.7142857142857, 100, 98.5, 97.5,
NA, 89.5, 83, 105.833333333333, 99.5, 94.5555555555556, 91.8333333333333,
100.4, 104.8, 104.7, 97, 99, 87.4642857142857, 76.4090909090909,
87.5, 91.3076923076923, 88, 85.6923076923077, 82.0714285714286,
91.2, 86.6428571428571, 90.6363636363636, 105.142857142857, 95.5,
94.1, 102.5, 93.8333333333333)), row.names = c(NA, -45L), class = c("tbl_df",
"tbl", "data.frame"))
I'm trying to get an average heart rate out of every activity. The
heart rate has been measured with a sports watch and the activities
(sitting, lying, walking etc) have been measured with an accelerometer
(which in turn estimates what kind of activity was being done).
You could use a data.table non-qui join with by = .EACHI. That should be quite fast.
library(data.table)
setDT(A02_pre_hr)
setDT(A02_pre)
A02_pre_hr[, start := as.POSIXct(start)]
A02_pre_hr[, end := start + duration]
A02_pre[, time := as.POSIXct(`ns1:Time`)]
A02_pre_hr[, averageHeartRate1 := A02_pre[A02_pre_hr, mean(`ns1:Value4`, na.rm = TRUE),
on = c("time >= start", "time <= end"), by = .EACHI]$V1]
# results_id start duration class steps mi aee tee met counts averageHeartRate end averageHeartRate1
# 1: 17817 2018-05-04 13:09:37 22.37 standing 0 2 16 91 1.145 14 104.00000 2018-05-04 13:09:59 NaN
# 2: 17817 2018-05-04 13:10:00 60.00 standing 0 12 21 96 1.208 138 100.23684 2018-05-04 13:11:00 100.23684
# 3: 17817 2018-05-04 13:11:00 60.00 standing 0 1 15 90 1.139 0 88.69048 2018-05-04 13:12:00 88.69048
# 4: 17817 2018-05-04 13:12:00 60.00 standing 0 11 20 96 1.201 256 64.90000 2018-05-04 13:13:00 64.90000
# 5: 17817 2018-05-04 13:13:00 60.00 standing 0 2 16 91 1.145 0 81.19444 2018-05-04 13:14:00 81.19444
# 6: 17817 2018-05-04 13:14:00 60.00 standing 0 102 64 144 1.767 2059 70.00000 2018-05-04 13:15:00 70.00000
# 7: 17817 2018-05-04 13:15:00 60.00 standing 0 329 173 265 3.180 7922 80.43478 2018-05-04 13:16:00 80.43478
# ....
The first value doesn't match because of the subseconds in the duration.

Batch column aggregation and reordering dataframe in R

I have census tract data divided my age variables by sex, into a value for males (varname_m) and females (varname_f):
Rows: 146,112
Columns: 13
$ tractid <chr> "01001020100", "01001020100", "01001020200", "01001020200", "01001020300", "01001020300", "01001020400", "01001020400", "01001020500", "01001020500", "0100102060…
$ ag18to19_m <dbl> 37, 57, 24, 15, 49, 27, 87, 33, 293, 159, 57, 40, 19, 41, 18, 56, 143, 86, 25, 155, 41, 7, 40, 0, 35, 0, 99, 25, 190, 420, 61, 157, 63, 110, 37, 127, 67, 45, 198…
$ ag20_m <dbl> 6, 14, 64, 0, 11, 18, 16, 8, 115, 21, 42, 15, 53, 71, 16, 0, 63, 77, 43, 96, 32, 15, 21, 0, 12, 44, 8, 0, 105, 80, 34, 20, 8, 0, 13, 46, 88, 0, 83, 241, 10, 96, …
$ ag21_m <dbl> 18, 0, 15, 7, 0, 16, 117, 18, 14, 40, 23, 26, 45, 47, 32, 0, 41, 50, 0, 76, 14, 45, 20, 1, 48, 11, 11, 30, 18, 30, 60, 55, 20, 0, 28, 43, 31, 21, 9, 0, 11, 8, 0,…
$ ag22to24_m <dbl> 48, 64, 109, 45, 25, 62, 65, 41, 224, 531, 28, 51, 31, 60, 0, 24, 132, 96, 59, 98, 27, 45, 111, 30, 113, 58, 71, 61, 46, 114, 11, 86, 116, 99, 28, 158, 72, 135, …
$ ag25to29_m <dbl> 49, 31, 83, 99, 87, 144, 153, 142, 428, 327, 69, 35, 36, 22, 61, 113, 202, 420, 184, 255, 94, 84, 118, 82, 71, 30, 47, 195, 44, 135, 118, 150, 215, 157, 118, 180…
$ ag30to34_m <dbl> 52, 72, 59, 97, 84, 157, 124, 85, 415, 227, 95, 13, 105, 202, 37, 86, 274, 334, 161, 182, 91, 173, 84, 84, 81, 106, 79, 67, 263, 77, 40, 115, 199, 411, 81, 115, …
$ ag18to19_f <dbl> 33, 8, 51, 7, 31, 19, 107, 15, 33, 25, 47, 37, 35, 81, 98, 92, 127, 147, 72, 0, 109, 57, 7, 74, 78, 0, 36, 24, 109, 268, 88, 62, 10, 0, 47, 33, 79, 191, 63, 134,…
$ ag20_f <dbl> 13, 40, 23, 18, 27, 18, 12, 11, 37, 0, 58, 83, 19, 45, 20, 77, 16, 103, 0, 36, 15, 0, 8, 37, 29, 34, 36, 0, 23, 30, 37, 0, 10, 48, 51, 67, 17, 15, 125, 55, 27, 1…
$ ag21_f <dbl> 40, 6, 13, 24, 36, 0, 16, 19, 17, 0, 11, 0, 0, 89, 28, 31, 39, 20, 15, 0, 7, 13, 0, 17, 9, 13, 17, 47, 106, 36, 42, 94, 0, 13, 19, 50, 67, 0, 122, 48, 21, 9, 145…
$ ag22to24_f <dbl> 21, 67, 71, 21, 69, 35, 28, 165, 346, 350, 15, 0, 53, 50, 25, 42, 207, 165, 158, 114, 20, 0, 73, 66, 29, 29, 59, 39, 83, 94, 22, 24, 79, 69, 37, 21, 73, 201, 282…
$ ag25to29_f <dbl> 36, 24, 86, 51, 88, 160, 130, 73, 318, 539, 157, 127, 128, 111, 86, 29, 334, 365, 87, 217, 57, 60, 177, 92, 17, 90, 86, 113, 67, 204, 136, 120, 130, 108, 211, 51…
$ ag30to34_f <dbl> 36, 73, 38, 42, 87, 154, 63, 84, 440, 414, 51, 95, 151, 73, 27, 70, 429, 458, 231, 173, 54, 82, 104, 24, 61, 159, 69, 30, 218, 82, 88, 214, 222, 158, 76, 125, 24…
I want to aggregate each of the variables divided by sex to a single combined variable. For example, I want to add ag18to19_m and ag18to19_f to create ag18to19. I can easily do this using mutate and the following code and order them to the front of the data frame:
aggregated <- merged %>%
mutate(ag18to19 = ag18to19_m + ag18to19_f) %>%
relocate(ag18to19, .before = ag18to19_m) %>%
mutate(ag20 = ag20_m + ag20_f) %>%
relocate(ag20, .before = ag20_m) %>%
mutate(ag21 = ag21_m + ag21_f) %>%
relocate(ag21, .before = ag21_m) %>%
mutate(ag22to24 = ag22to24_m + ag22to24_f) %>%
relocate(ag22to24, .before = ag22to24_m) %>%
mutate(ag25to29 = ag25to29_m + ag25to29_f) %>%
relocate(ag25to29, .before = ag25to29_m) %>%
mutate(ag30to34 = ag30to34_m + ag30to34_f) %>%
relocate(ag30to34, .before = ag30to34_m)
I know there's a more efficient way to do this using a loop or map_df function that will also give me a data frame as an output. I've been trying for the last hour to write a function and use map_df but haven't had any success. Does anyone have a suggestion?
More efficient code here is best practice and will help me apply this same data cleaning step to several other variables that are grouped in the same way (e.g., income grouped by sex or education grouped by age).
Any help would be greatly appreciated. Thanks.
Here is an option in tidyverse
library(dplyr)
library(stringr)
merged1 <- merged %>%
mutate(across(ends_with('_m'), ~
. + get(str_replace(cur_column(), '_m', '_f')),
.names = '{.col}_new')) %>%
rename_at(vars(ends_with('_new')),
~ str_remove(., '_[m]_new$')) %>%
select(tract_id, order(names(.)[-1]) + 1)

Plotly animation in R; frames are not in correct order, mix up in frames

The Data:
dput(LifeExpCH$Age)
c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
111)
> dput(LifeExpCH$Die)
c(380, 16, 11, 9, 9, 8, 7, 7, 7, 8, 7, 7, 8, 8, 8, 10, 11, 13,
16, 19, 20, 20, 21, 20, 19, 21, 20, 21, 23, 24, 25, 27, 30, 31,
35, 37, 41, 44, 48, 52, 57, 63, 70, 76, 84, 94, 104, 115, 129,
143, 159, 176, 195, 215, 237, 258, 283, 307, 334, 363, 392, 424,
458, 495, 534, 578, 624, 677, 734, 798, 869, 952, 1044, 1149,
1271, 1411, 1569, 1750, 1955, 2184, 2440, 2723, 3032, 3363, 3711,
4064, 4404, 4710, 4952, 5106, 5143, 5041, 4795, 4408, 3908, 3342,
2753, 2190, 1679, 1243, 888, 612, 406, 259, 158, 93, 51, 27,
13, 5, 2, 1)
I want to create a plotly animation in R. The animation is not working as intended. There is a mix up in the frames. Frames 100:109 are at the start. May I ask for some help, how to get the frames in the right order?
Here is the code:
library(plotly)
library(dplyr)
library(purrr)
LifeExpCH %>%
split(.$Age) %>% accumulate(~bind_rows(.x, .y)) %>%
set_names(0:111) %>%
bind_rows(.id = "frame") %>%
plot_ly(x = ~Age, y = ~Die) %>%
add_lines(frame = ~frame, showlegend = FALSE)

Assign the same colors to the nodes in different graphics

I have a network and I found the degree of the nodes.
I created two charts:
the first represents the network in such a way that the nodes of degree greater are larger and I used the RColorBrewer library for coloring the nodes
the second graph is a barplot which always represents the degree of each node.
I wish the same nodes have the same color.
For example, the Valjean node in the first graph is green/blue, while in the second is yellow.
How can I do so that it has the same color in both graphs?
This is the code:
library(igraph)
library(RColorBrewer)
library(sna)
library(scales)
library(plotrix)
net <- read.graph("./s.gml", format = c("gml"))
dput(net)
# find degree
deg <- igraph::degree(net, mode = "all")
pal <- rev(colorRampPalette(RColorBrewer::brewer.pal(9, "Set2"))(max(deg)))
##### FIRST CHART ######
labelSpecific <- ifelse(igraph::degree(net) > 35, V(net)$label, NA)
lab1 <- labelSpecific
V(net)$label.color <- "black"
x11()
V(net)$frame.color <- NA
plot(net,
vertex.color = pal[deg - min(deg) + 1],
vertex.size = plotrix::rescale(deg, c(4, 15)),
vertex.label = lab1,
main = "Degree")
###### SECOND CHART #########
mar.default <- c(5, 4, 4, 2) + 0.1
par(mar = mar.default + c(4, 0, 0, 0))
V(net)$label <- V(net)$label
x11()
barplot(height = deg,
space = 1,
names.arg = V(net)$label,
main = "Degree",
ylab = "Degree",
ylim = c(0, 40),
cex.axis = 1,
cex.names = 0.6,
col = pal,
border = NA,
las = 2)
I get this image.
My network is:
> dput(net)
structure(list(77, FALSE, c(1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 11,
11, 11, 11, 12, 13, 14, 15, 17, 18, 18, 19, 19, 19, 20, 20, 20,
20, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23,
23, 23, 23, 23, 23, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27,
27, 27, 27, 28, 28, 29, 29, 29, 30, 31, 31, 31, 31, 32, 33, 33,
34, 34, 35, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37, 37, 38, 38,
38, 38, 38, 38, 39, 40, 41, 41, 42, 42, 42, 43, 43, 43, 44, 44,
45, 47, 48, 48, 48, 48, 49, 49, 50, 50, 51, 51, 51, 52, 52, 53,
54, 54, 54, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 56, 56, 57,
57, 57, 58, 58, 58, 58, 58, 59, 59, 59, 59, 60, 60, 60, 61, 61,
61, 61, 61, 61, 62, 62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63,
63, 63, 63, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 65, 65,
65, 65, 65, 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 66, 66, 66,
66, 67, 68, 68, 68, 68, 68, 68, 69, 69, 69, 69, 69, 69, 69, 70,
70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 71, 71, 71, 72,
72, 72, 73, 74, 74, 75, 75, 75, 75, 75, 75, 75, 76, 76, 76, 76,
76, 76, 76), c(0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 10, 3, 2, 0, 11,
11, 11, 11, 16, 16, 17, 16, 17, 18, 16, 17, 18, 19, 16, 17, 18,
19, 20, 16, 17, 18, 19, 20, 21, 16, 17, 18, 19, 20, 21, 22, 12,
11, 23, 11, 24, 23, 11, 24, 11, 16, 25, 11, 23, 25, 24, 26, 11,
27, 23, 27, 11, 23, 30, 11, 23, 27, 11, 11, 27, 11, 29, 11, 34,
29, 34, 35, 11, 29, 34, 35, 36, 11, 29, 34, 35, 36, 37, 11, 29,
25, 25, 24, 25, 41, 25, 24, 11, 26, 27, 28, 11, 28, 46, 47, 25,
27, 11, 26, 11, 49, 24, 49, 26, 11, 51, 39, 51, 51, 49, 26, 51,
49, 39, 54, 26, 11, 16, 25, 41, 48, 49, 55, 55, 41, 48, 55, 48,
27, 57, 11, 58, 55, 48, 57, 48, 58, 59, 48, 58, 60, 59, 57, 55,
55, 58, 59, 48, 57, 41, 61, 60, 59, 48, 62, 57, 58, 61, 60, 55,
55, 62, 48, 63, 58, 61, 60, 59, 57, 11, 63, 64, 48, 62, 58, 61,
60, 59, 57, 55, 64, 58, 59, 62, 65, 48, 63, 61, 60, 57, 25, 11,
24, 27, 48, 41, 25, 68, 11, 24, 27, 48, 41, 25, 69, 68, 11, 24,
27, 41, 58, 27, 69, 68, 70, 11, 48, 41, 25, 26, 27, 11, 48, 48,
73, 69, 68, 25, 48, 41, 70, 71, 64, 65, 66, 63, 62, 48, 58),
c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 13, 12, 11, 10, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 47, 46, 39, 40, 41, 42, 43, 44,
45, 49, 48, 52, 51, 50, 54, 55, 53, 56, 57, 58, 60, 59, 61,
62, 63, 66, 64, 65, 67, 69, 70, 71, 68, 72, 73, 74, 75, 76,
77, 79, 78, 82, 83, 80, 81, 87, 88, 84, 85, 86, 93, 94, 89,
90, 91, 92, 95, 96, 97, 98, 101, 100, 99, 102, 103, 104,
106, 105, 107, 108, 112, 110, 111, 109, 114, 113, 116, 115,
119, 118, 117, 121, 120, 122, 125, 124, 123, 131, 132, 133,
130, 128, 134, 135, 127, 126, 129, 136, 137, 139, 140, 138,
145, 143, 142, 141, 144, 148, 147, 149, 146, 150, 151, 152,
153, 158, 157, 154, 156, 155, 164, 162, 159, 163, 160, 161,
166, 165, 168, 174, 170, 171, 167, 173, 172, 169, 184, 177,
175, 183, 179, 182, 181, 180, 176, 178, 187, 194, 193, 189,
192, 191, 190, 188, 185, 186, 200, 196, 197, 203, 202, 198,
201, 195, 199, 204, 206, 207, 205, 208, 210, 209, 213, 214,
211, 215, 217, 216, 212, 221, 222, 218, 223, 224, 225, 220,
219, 230, 233, 226, 232, 231, 228, 227, 229, 236, 234, 235,
237, 238, 239, 242, 244, 243, 241, 240, 245, 246, 252, 253,
251, 250, 247, 248, 249), c(0, 1, 2, 4, 5, 6, 7, 8, 9, 13,
3, 12, 11, 10, 14, 15, 16, 17, 47, 49, 52, 54, 57, 62, 66,
69, 72, 73, 75, 77, 82, 87, 93, 102, 106, 112, 114, 119,
131, 145, 184, 206, 213, 221, 230, 236, 46, 18, 19, 21, 24,
28, 33, 39, 55, 132, 20, 22, 25, 29, 34, 40, 23, 26, 30,
35, 41, 27, 31, 36, 42, 32, 37, 43, 38, 44, 45, 48, 51, 58,
64, 67, 70, 50, 53, 60, 97, 101, 116, 207, 214, 222, 56,
59, 95, 96, 98, 100, 110, 133, 205, 211, 218, 233, 242, 61,
103, 113, 118, 125, 130, 234, 63, 65, 71, 74, 104, 111, 143,
208, 215, 223, 226, 235, 105, 107, 76, 79, 83, 88, 94, 68,
78, 80, 84, 89, 81, 85, 90, 86, 91, 92, 121, 128, 99, 134,
139, 164, 210, 217, 224, 232, 244, 108, 109, 135, 140, 142,
148, 150, 153, 162, 168, 177, 187, 200, 209, 216, 231, 237,
238, 243, 252, 115, 117, 124, 127, 136, 120, 122, 123, 126,
129, 137, 138, 141, 147, 158, 159, 174, 175, 194, 144, 149,
157, 163, 170, 183, 193, 204, 146, 151, 154, 160, 171, 179,
189, 196, 225, 253, 152, 156, 161, 167, 182, 192, 197, 155,
166, 173, 181, 191, 203, 165, 172, 180, 190, 202, 169, 176,
188, 198, 251, 178, 185, 201, 250, 186, 195, 247, 199, 248,
249, 212, 220, 228, 241, 219, 227, 240, 229, 245, 246, 239
), c(0, 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 10, 14, 15, 16, 17,
18, 18, 19, 21, 24, 28, 33, 39, 48, 50, 53, 57, 62, 64, 67,
68, 72, 73, 75, 77, 80, 84, 89, 95, 96, 97, 99, 102, 105,
107, 108, 108, 109, 113, 115, 117, 120, 122, 123, 126, 136,
138, 141, 146, 150, 153, 159, 167, 175, 185, 195, 204, 205,
211, 218, 226, 234, 237, 238, 240, 247, 254), c(0, 10, 10,
12, 13, 13, 13, 13, 13, 13, 13, 14, 46, 47, 47, 47, 47, 56,
62, 67, 71, 74, 76, 77, 83, 92, 105, 112, 124, 126, 131,
132, 132, 132, 132, 136, 139, 141, 142, 142, 144, 144, 153,
153, 153, 153, 153, 154, 155, 173, 178, 178, 182, 182, 182,
183, 192, 192, 200, 210, 217, 223, 228, 233, 237, 240, 242,
243, 243, 247, 250, 252, 253, 253, 254, 254, 254, 254), list(
c(1, 0, 1), structure(list(), .Names = character(0)),
structure(list(id = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
67, 68, 69, 70, 71, 72, 73, 74, 75, 76), label = c("Myriel",
"Napoleon", "MlleBaptistine", "MmeMagloire", "CountessDeLo",
"Geborand", "Champtercier", "Cravatte", "Count", "OldMan",
"Labarre", "Valjean", "Marguerite", "MmeDeR", "Isabeau",
"Gervais", "Tholomyes", "Listolier", "Fameuil", "Blacheville",
"Favourite", "Dahlia", "Zephine", "Fantine", "MmeThenardier",
"Thenardier", "Cosette", "Javert", "Fauchelevent", "Bamatabois",
"Perpetue", "Simplice", "Scaufflaire", "Woman1", "Judge",
"Champmathieu", "Brevet", "Chenildieu", "Cochepaille",
"Pontmercy", "Boulatruelle", "Eponine", "Anzelma", "Woman2",
"MotherInnocent", "Gribier", "Jondrette", "MmeBurgon",
"Gavroche", "Gillenormand", "Magnon", "MlleGillenormand",
"MmePontmercy", "MlleVaubois", "LtGillenormand", "Marius",
"BaronessT", "Mabeuf", "Enjolras", "Combeferre", "Prouvaire",
"Feuilly", "Courfeyrac", "Bahorel", "Bossuet", "Joly",
"Grantaire", "MotherPlutarch", "Gueulemer", "Babet",
"Claquesous", "Montparnasse", "Toussaint", "Child1",
"Child2", "Brujon", "MmeHucheloup"), maincharacter = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0)), .Names = c("id", "label", "maincharacter"
)), structure(list(value = c(1, 8, 10, 6, 1, 1, 1, 1,
2, 1, 1, 3, 3, 5, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 3, 3,
3, 4, 3, 3, 3, 3, 5, 3, 3, 3, 3, 4, 4, 3, 3, 3, 3, 4,
4, 4, 2, 9, 2, 7, 13, 1, 12, 4, 31, 1, 1, 17, 5, 5, 1,
1, 8, 1, 1, 1, 2, 1, 2, 3, 2, 1, 1, 2, 1, 3, 2, 3, 3,
2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 1, 1,
2, 3, 2, 2, 1, 3, 1, 1, 3, 1, 2, 1, 2, 1, 1, 1, 3, 2,
1, 1, 9, 2, 2, 1, 1, 1, 2, 1, 1, 6, 12, 1, 1, 21, 19,
1, 2, 5, 4, 1, 1, 1, 1, 1, 7, 7, 6, 1, 4, 15, 5, 6, 2,
1, 4, 2, 2, 6, 2, 5, 1, 1, 9, 17, 13, 7, 2, 1, 6, 3,
5, 5, 6, 2, 4, 3, 2, 1, 5, 12, 5, 4, 10, 6, 2, 9, 1,
1, 5, 7, 3, 5, 5, 5, 2, 5, 1, 2, 3, 3, 1, 2, 2, 1, 1,
1, 1, 3, 5, 1, 1, 1, 1, 1, 6, 6, 1, 1, 2, 1, 1, 4, 4,
4, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 2,
2, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = "value")),
<environment>), class = "igraph")
You are passing two different colors vectors to plot and to barplot.
Just use the same code for both, e.g. in the barplot use:
barplot(
...
col = pal[deg - min(deg) + 1]
...
)

ggvis barchart using dates as x axis

I have been switching over from ggplot to ggvis when working with shiny apps. I have figured out a lot but am really stumped when it comes to bar graphs. I have a timeseries with dates and values. I simply want bars instead of points for each value (I would ideally like to be able to plot multiple semi-transparent bars if anyone has had success there please share) but here I wanted to get one bar working.
Works with layer_points()
df %>% ggvis(~date, ~x) %>% layer_points() %>% scale_datetime("x")
Doesnt work with layer_bars()
df %>% ggvis(~date, ~x) %>% layer_bars() %>% scale_datetime("x")
Data I am using...
structure(list(date = structure(c(7680, 7687, 7694, 7701, 7708,
7715, 7722, 7729, 7736, 7743, 7750, 7757, 7764, 7771, 7778, 7785,
7792, 7799, 7806, 7813, 7820, 7827, 7834, 7841, 7848, 7855, 7862,
7869, 7876, 7883, 7890, 7897, 7904, 7911, 7918, 7925, 7932, 7939,
7946, 7953, 7960, 7967, 7974, 7981, 7988, 7995, 8002, 8009, 8016,
8023, 8030, 8037, 8044, 8051, 8058, 8065, 8072, 8079, 8086, 8093,
8100, 8107, 8114, 8121, 8128, 8135, 8142, 8149, 8156, 8163, 8170,
8177, 8184, 8191, 8198, 8205, 8212, 8219, 8226, 8233, 8240, 8247,
8254, 8261, 8268, 8275, 8282, 8289, 8296, 8303, 8310, 8317, 8324,
8331, 8338, 8345, 8352, 8359, 8366, 8373, 8380, 8387, 8394, 8401,
8408, 8415, 8422, 8429, 8436, 8443, 8450, 8457, 8464, 8471, 8478,
8485, 8492, 8499, 8506, 8513, 8520, 8527, 8534, 8541, 8548, 8555,
8562, 8569, 8576, 8583, 8590, 8597, 8604, 8611, 8618, 8625, 8632,
8639, 8646, 8653, 8660, 8667, 8674, 8681, 8688, 8695, 8702, 8709,
8716, 8723, 8730, 8737, 8744, 8751, 8758, 8765, 8772, 8779, 8786,
8793, 8800, 8807, 8814, 8821, 8828, 8835, 8842, 8849, 8856, 8863,
8870, 8877, 8884, 8891, 8898, 8905, 8912, 8919, 8926, 8933, 8940,
8947, 8954, 8961, 8968, 8975, 8982, 8989, 8996, 9003, 9010, 9017,
9024, 9031, 9038, 9045, 9052, 9059, 9066, 9073), class = "Date"),
x = c(-0.034038302, 0.122310949, -0.002797319, 0.026515253,
0.039961798, 0.034473263, 0.00549937, -0.024125944, 0.000132490000000001,
0.011038357, -0.02135072, 0.030663311, -0.008915551, 0.004855042,
0.01563688, -0.007397493, 0.013569146, -0.004968811, -0.00250391,
0.014624532, 0.036937453, -0.023685917, 0.018921356, -0.003066779,
-0.009217771, 0.005317513, 0.010378968, 0.001580798, -0.015085972,
-0.000121644000000001, 0.020468644, 0.007925229, 0.007721276,
-0.003123545, -0.018317891, -0.014900591, 0.003260844, -0.001565358,
-0.014833886, 0.00366766, 0.014297139, -0.00725552, 0.012207931,
0.024035152, -0.024195095, -0.0043564, 0.000847468, 0.033031596,
0.023685033, 0.025143071, 0.046264348, 0.038285177, -0.009180356,
-0.01630399, -0.010131294, -0.009939386, -0.007620427, 0.013062259,
0.009912238, 0.000192973, -0.01683559, -0.002627549, 0.019836063,
-0.019946159, -0.020124331, 0.012921737, 0.034604405, -0.020774015,
0.00334805, 0.002271156, -0.018676732, 0.019160923, -0.01945997,
-0.014342636, -0.004867796, -0.010002446, -0.004372991, 0.023164369,
0.019824112, -0.00321832, -0.015785746, 0.040836652, 0.00148831,
0.012084485, -0.009603897, -0.004642148, -0.008399234, 0.010463218,
0.000256571000000001, -0.01978405, -0.003439498, -0.015669975,
0.026180724, 0.020373255, 0.019160773, 0.00692683, 0.010215506,
0.010861939, 0.012041143, 0.025734568, -0.004828156, 0.006914552,
-0.00720089, -0.000538489999999999, -0.008479448, 0.022926604,
0.002131842, -0.003688597, 0.025325639, -0.009562293, -0.024336741,
0.012907537, 0.004339383, 0.010744364, -0.013058765, -0.003672014,
-0.023887493, 0.01062259, 0.02088054, -0.035249878, -0.001462821,
0.01904368, -0.001308787, 0.009203217, 0.019856479, 0.011296979,
0.010039545, -0.01559142, 0.006083419, -0.017958978, -0.007488063,
0.01236649, -0.004459064, -0.004375386, 0.025500722, 0.005557851,
0.008444321, 0.002827649, 0.020320308, 0.031611803, -0.010199803,
-0.009425874, 0.007942729, -2.59379999999999e-05, 0.016669077,
-0.011666062, 0.022835386, -0.025599107, 0.013562535, -0.018365192,
0.018148786, 0.016649144, -0.009530455, 0.012996597, 0.002034778,
-0.005926478, -0.004897238, -0.004419719, 0.010848926, -0.006039757,
-0.030287605, 0.019221837, 0.001808161, -0.009566133, 0.005009292,
0.005365023, -0.004879922, -0.024637933, -0.0186584, 0.004786059,
-0.008245254, -0.000106243, -0.001714888, -0.017804006, -0.021200061,
0.003812757, 0.021940886, 0.002270448, -0.015417493, -0.045754612,
-0.003468442, -0.006242659, 0.022383824, -0.018753927, 0.008577571,
0.008655048, 0.02374636, 0.029522811, 0.009946946, 0.015419714,
-0.016714623, -0.014616188, 0.019670855, -0.038979063, 0.020491563,
-0.009640674, 0.046051144, -0.021434575, 0.000190443999999998,
-0.029013969), id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136,
137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148,
149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
197, 198, 199, 200)), .Names = c("date", "x", "id"), row.names = 53:252, class = "data.frame")
Set format df$date as character:
df$date <- as.character(df$date)
and then:
df %>% ggvis(~date, ~x) %>% layer_bars()

Resources