отредактировано, чтобы включить dput(Nitrate_set)
вывод:
structure(list(SITE.NO = structure(c(41L, 42L, 41L, 3L, 35L,
42L, 40L, 40L, 40L, 1L, 40L, 1L, 40L, 40L, 16L, 40L, 42L, 40L,
40L, 42L, 1L, 42L, 40L, 1L, 40L, 1L, 40L, 4L, 42L, 21L, 15L,
5L, 36L, 40L, 40L, 2L, 41L, 6L, 2L, 12L, 42L, 19L, 2L, 1L, 1L,
1L, 2L, 42L, 1L, 2L, 7L, 1L, 39L, 2L, 2L, 2L, 2L, 39L, 11L, 37L,
2L, 1L, 1L, 1L, 1L, 32L, 1L, 41L, 2L, 2L, 1L, 2L, 1L, 1L, 2L,
1L, 2L, 40L, 42L, 1L, 2L, 38L, 1L, 1L, 2L, 2L, 2L, 41L, 38L,
1L, 8L, 42L, 42L, 1L, 2L, 2L, 33L, 1L, 2L, 18L, 2L, 1L, 2L, 2L,
1L, 41L, 1L, 2L, 1L, 40L, 1L, 2L, 2L, 2L, 1L, 2L, 41L, 10L, 14L,
2L, 2L, 2L, 2L, 42L, 9L, 1L, 1L, 2L, 2L, 2L, 38L, 1L, 1L, 2L,
2L, 38L, 42L, 38L, 40L, 42L, 41L, 1L, 1L, 2L, 38L, 38L, 1L, 38L,
2L, 1L, 2L, 2L, 2L, 17L, 2L, 42L, 41L, 2L, 1L, 1L, 2L, 1L, 38L,
1L, 1L, 38L, 38L, 38L, 38L, 2L, 1L, 2L, 26L, 38L, 38L, 1L, 2L,
38L, 38L, 2L, 1L, 1L, 1L, 38L, 38L, 1L, 31L, 2L, 2L, 1L, 1L,
34L, 24L, 38L, 2L, 28L, 38L, 25L, 38L, 38L, 38L, 38L, 39L, 1L,
38L, 1L, 29L, 38L, 38L, 1L, 38L, 2L, 2L, 22L, 1L, 27L, 1L, 30L,
38L, 38L, 1L, 2L, 2L, 38L, 2L, 38L, 38L, 38L, 13L, 38L, 1L, 38L,
1L, 23L, 2L, 39L, 20L, 38L, 38L, 2L, 38L, 38L, 38L, 38L, 38L,
38L, 38L, 38L, 38L, 1L, 38L, 38L, 41L, 38L, 1L, 38L, 38L, 2L,
38L, 38L, 38L, 38L, 1L, 38L, 1L, 38L, 2L, 2L, 38L, 1L, 40L, 2L,
42L, 41L, 42L, 38L, 42L, 38L, 41L, 2L, 42L, 1L, 38L, 38L, 42L,
2L, 42L, 38L, 38L, 2L, 2L, 2L, 39L, 40L, 2L, 42L, 1L, 1L, 38L,
40L, 41L, 2L, 41L, 41L, 1L, 2L, 41L, 1L, 39L, 41L, 1L, 41L, 40L,
1L, 39L, 39L, 2L, 40L, 38L, 1L, 2L, 2L, 38L, 39L, 1L, 2L, 38L,
41L, 38L, 38L, 2L, 38L, 38L, 41L, 2L, 39L, 43L, 39L, 41L, 43L,
43L, 43L, 43L, 41L, 39L, 39L, 41L, 43L, 41L, 39L, 39L, 39L, 39L,
39L, 39L, 43L, 43L, 43L, 43L, 43L, 41L), .Label = c("1110056",
"1111019", "1120054", "1120055", "1120060", "1120061", "1120062",
"1120065", "1120066", "1120067", "1120068", "1120069", "1120070",
"1120071", "1120073", "1120074", "1120075", "1120076", "1120077",
"1120078", "1120079", "1120080", "1120081", "1120082", "1120083",
"1120084", "1120085", "1120086", "1120087", "1120088", "1120089",
"1120090", "1120091", "1120092", "1120094", "1120095", "1120096",
"113006A", "122013A", "125013A", "126001A", "1260122", "141010A"
), class = "factor"), SITE.NAME = structure(c(8L, 5L, 8L, 2L,
2L, 5L, 4L, 4L, 4L, 3L, 4L, 3L, 4L, 4L, 2L, 4L, 5L, 4L, 4L, 5L,
3L, 5L, 4L, 3L, 4L, 3L, 4L, 2L, 5L, 2L, 2L, 2L, 2L, 4L, 4L, 7L,
8L, 2L, 7L, 2L, 5L, 2L, 7L, 3L, 3L, 3L, 7L, 5L, 3L, 7L, 2L, 3L,
6L, 7L, 7L, 7L, 7L, 6L, 2L, 2L, 7L, 3L, 3L, 3L, 3L, 2L, 3L, 8L,
7L, 7L, 3L, 7L, 3L, 3L, 7L, 3L, 7L, 4L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, 7L, 6L, 1L, 6L, 8L, 1L,
1L, 1L, 1L, 8L, 6L, 6L, 8L, 1L, 8L, 6L, 6L, 6L, 6L, 6L, 6L, 1L,
1L, 1L, 1L, 1L, 8L), .Label = c("Coochin Creek at Mawsons Road",
"Johnstone River at Coquette Point Gbr_Jri_Wq", "Mulgrave River at Deeral",
"Pioneer River at Dumbleton Weir Headwater", "Plane Creek at Sucrogen Weir",
"Proserpine River at Glen Isla", "Russell River at East Russell",
"Sandy Creek at Homebush", "Tully River at Euramo"), class = "factor"),
Date = structure(c(64L, 62L, 48L, 63L, 42L, 8L, 54L, 75L,
75L, 48L, 12L, 73L, 3L, 37L, 40L, 17L, 69L, 69L, 47L, 4L,
27L, 4L, 48L, 64L, 74L, 79L, 74L, 79L, 54L, 49L, 40L, 80L,
61L, 62L, 64L, 53L, 3L, 80L, 53L, 4L, 81L, 45L, 53L, 53L,
50L, 53L, 50L, 31L, 50L, 50L, 81L, 53L, 17L, 53L, 50L, 70L,
46L, 43L, 4L, 6L, 70L, 53L, 13L, 65L, 13L, 2L, 56L, 37L,
25L, 53L, 65L, 68L, 56L, 46L, 2L, 70L, 13L, 12L, 81L, 40L,
6L, 24L, 16L, 16L, 46L, 68L, 2L, 80L, 52L, 68L, 81L, 81L,
14L, 46L, 30L, 60L, 30L, 70L, 56L, 45L, 61L, 16L, 65L, 56L,
16L, 81L, 55L, 5L, 32L, 10L, 13L, 13L, 46L, 72L, 32L, 49L,
14L, 4L, 8L, 42L, 35L, 1L, 41L, 37L, 81L, 63L, 52L, 76L,
1L, 32L, 53L, 40L, 55L, 52L, 58L, 53L, 10L, 53L, 20L, 48L,
80L, 19L, 49L, 13L, 56L, 53L, 19L, 50L, 21L, 46L, 35L, 21L,
19L, 45L, 16L, 64L, 81L, 19L, 32L, 25L, 52L, 72L, 38L, 49L,
49L, 26L, 38L, 38L, 38L, 32L, 80L, 16L, 56L, 46L, 50L, 1L,
32L, 41L, 41L, 32L, 58L, 58L, 41L, 56L, 13L, 23L, 68L, 55L,
49L, 32L, 15L, 5L, 55L, 2L, 40L, 65L, 65L, 53L, 58L, 25L,
13L, 5L, 37L, 61L, 11L, 35L, 65L, 60L, 11L, 1L, 52L, 45L,
58L, 52L, 6L, 56L, 49L, 68L, 13L, 68L, 35L, 49L, 55L, 49L,
45L, 30L, 55L, 19L, 8L, 67L, 22L, 70L, 5L, 52L, 45L, 34L,
49L, 45L, 32L, 80L, 16L, 13L, 70L, 49L, 67L, 16L, 19L, 44L,
67L, 42L, 21L, 21L, 80L, 1L, 45L, 45L, 59L, 4L, 59L, 29L,
29L, 21L, 45L, 71L, 45L, 45L, 80L, 4L, 40L, 40L, 8L, 4L,
15L, 81L, 18L, 18L, 15L, 20L, 54L, 81L, 12L, 76L, 57L, 18L,
12L, 81L, 20L, 80L, 80L, 15L, 81L, 81L, 31L, 20L, 8L, 28L,
18L, 20L, 18L, 20L, 80L, 4L, 47L, 69L, 18L, 18L, 62L, 18L,
20L, 10L, 4L, 10L, 4L, 9L, 10L, 12L, 18L, 28L, 9L, 8L, 18L,
18L, 9L, 12L, 8L, 9L, 4L, 28L, 4L, 8L, 8L, 8L, 8L, 10L, 8L,
66L, 39L, 8L, 20L, 77L, 36L, 36L, 36L, 20L, 4L, 74L, 12L,
51L, 20L, 4L, 78L, 78L, 81L, 81L, 4L, 7L, 7L, 7L, 7L, 33L,
12L), .Label = c("1/01/2019", "1/02/2019", "1/11/2018", "10/12/2018",
"11/02/2019", "11/03/2019", "11/04/2019", "11/12/2018", "12/12/2018",
"13/12/2018", "14/01/2019", "14/12/2018", "15/01/2019", "15/11/2018",
"15/12/2018", "16/01/2019", "16/11/2018", "16/12/2018", "17/01/2019",
"17/12/2018", "18/01/2019", "18/02/2019", "18/03/2019", "18/09/2018",
"19/01/2019", "19/02/2019", "19/11/2018", "19/12/2018", "2/01/2019",
"2/02/2019", "2/11/2018", "20/01/2019", "20/04/2019", "20/12/2018",
"21/01/2019", "21/04/2019", "22/11/2018", "24/01/2019", "24/04/2019",
"24/12/2018", "25/01/2019", "25/02/2019", "25/09/2018", "25/10/2018",
"25/12/2018", "26/01/2019", "26/09/2018", "26/11/2018", "26/12/2018",
"27/01/2019", "27/03/2019", "27/12/2018", "28/01/2019", "28/08/2018",
"28/12/2018", "29/01/2019", "29/11/2018", "29/12/2018", "3/01/2019",
"3/02/2019", "3/03/2019", "3/07/2018", "3/09/2018", "3/12/2018",
"30/01/2019", "30/11/2018", "30/12/2018", "31/01/2019", "31/07/2018",
"31/12/2018", "4/01/2019", "4/02/2019", "5/11/2018", "5/12/2018",
"6/12/2018", "7/01/2019", "7/03/2019", "7/12/2018", "8/10/2018",
"8/12/2018", "9/12/2018"), class = "factor"), Trios.nitrate..mg.L. = c(0.01,
0.01, 0.01, 0.01, 0.02, 0.02, 0.02, 0.02, 0.02, 0.03, 0.03,
0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.04, 0.04, 0.04, 0.04,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.06, 0.06,
0.07, 0.07, 0.07, 0.08, 0.08, 0.08, 0.08, 0.08, 0.08, 0.09,
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.1, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11,
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.12, 0.12, 0.12, 0.12,
0.12, 0.12, 0.12, 0.12, 0.12, 0.13, 0.13, 0.13, 0.13, 0.13,
0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13,
0.13, 0.13, 0.13, 0.13, 0.13, 0.14, 0.14, 0.14, 0.14, 0.14,
0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14,
0.14, 0.14, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15,
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.17,
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.18, 0.18, 0.18,
0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.19, 0.19,
0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19,
0.19, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2,
0.2, 0.2, 0.2, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21,
0.21, 0.21, 0.21, 0.21, 0.21, 0.22, 0.22, 0.22, 0.22, 0.22,
0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22,
0.22, 0.22, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23,
0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23,
0.23, 0.23, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24,
0.24, 0.24, 0.24, 0.25, 0.25, 0.25, 0.26, 0.26, 0.26, 0.26,
0.26, 0.26, 0.26, 0.26, 0.26, 0.26, 0.26, 0.26, 0.26, 0.27,
0.27, 0.27, 0.28, 0.28, 0.3, 0.3, 0.3, 0.31, 0.31, 0.31,
0.31, 0.31, 0.32, 0.32, 0.33, 0.34, 0.34, 0.34, 0.35, 0.35,
0.36, 0.36, 0.37, 0.37, 0.37, 0.38, 0.38, 0.38, 0.38, 0.38,
0.39, 0.39, 0.4, 0.42, 0.42, 0.42, 0.43, 0.44, 0.48, 0.49,
0.49, 0.51, 0.51, 0.53, 0.56, 0.57, 0.58, 0.58, 0.58, 0.58,
0.58, 0.61, 0.64, 0.73, 0.76, 0.76, 0.81, 0.82, 0.85, 0.85,
0.85, 0.86, 0.88, 0.92, 0.94, 0.96, 1, 1.04, 1.04, 1.06,
1.14, 1.18, 1.34, 1.37, 1.45, 1.48, 1.48, 1.48, 1.54, 1.58,
1.73, 1.76, 1.82, 1.85, 1.95, 1.95, 1.96, 2.15, 2.18, 2.26,
2.3, 2.3, 2.3, 2.3, 2.69, 3.12), Chem.centre.Nox..mg.L. = c(5e-04,
0.001, 0.086, 0.048, 0.036, 0.015, 0.001, 0.088, 0.305, 0.012,
0.078, 0.023, 0.047, 0.002, 0.08, 0.004, 0.002, 0.003, 0.002,
0.029, 0.009, 0.045, 0.062, 0.006, 0.149, 0.087, 0.311, 0.008,
5e-04, 0.152, 0.03, 0.339, 0.103, 0.048, 0.003, 0.039, 0.159,
0.156, 0.043, 0.209, 0.091, 0.135, 0.039, 0.042, 0.041, 0.049,
0.045, 0.021, 0.045, 0.044, 0.235, 0.051, 0.03, 0.063, 0.046,
0.075, 0.058, 0.303, 0.227, 0.092, 0.094, 0.066, 0.068, 0.055,
0.073, 0.186, 0.079, 5e-04, 0.073, 0.086, 0.074, 0.097, 0.083,
0.062, 0.086, 0.084, 0.088, 0.098, 0.033, 0.093, 0.122, 0.104,
0.09, 0.093, 0.078, 0.095, 0.086, 0.178, 0.155, 0.098, 0.123,
0.134, 0.005, 0.065, 0.105, 0.109, 0.175, 0.098, 0.093, 0.081,
0.116, 0.116, 0.097, 0.098, 0.114, 0.159, 0.083, 0.126, 0.119,
0.092, 0.104, 0.116, 0.093, 0.114, 0.12, 0.118, 5e-04, 0.185,
0.265, 0.14, 0.168, 0.133, 0.116, 0.006, 0.057, 0.149, 0.086,
0.143, 0.137, 0.134, 0.164, 0.237, 0.114, 0.106, 0.123, 0.121,
0.086, 0.117, 0.184, 5e-04, 0.198, 0.148, 0.103, 0.159, 0.128,
0.134, 0.156, 0.134, 0.172, 0.082, 0.168, 0.176, 0.168, 0.153,
0.167, 0.001, 0.162, 0.171, 0.165, 0.152, 0.156, 0.17, 0.126,
0.124, 0.105, 0.188, 0.159, 0.17, 0.147, 0.174, 0.099, 0.176,
0.15, 0.141, 0.213, 0.166, 0.173, 0.181, 0.188, 0.192, 0.156,
0.062, 0.157, 0.167, 0.149, 0.234, 0.13, 0.172, 0.154, 0.194,
0.389, 0.171, 0.163, 0.181, 0.2, 0.156, 0.186, 0.17, 0.19,
0.196, 0.156, 0.218, 0.298, 0.218, 0.184, 0.206, 0.154, 0.193,
0.18, 0.192, 0.192, 0.145, 0.196, 0.158, 0.23, 0.172, 0.171,
0.154, 0.162, 0.209, 0.215, 0.179, 0.196, 0.183, 0.161, 0.208,
0.194, 0.208, 0.22, 0.178, 0.274, 0.184, 0.214, 0.222, 0.144,
0.169, 0.168, 0.183, 0.223, 0.184, 0.195, 0.181, 0.2, 0.202,
0.218, 0.191, 0.223, 0.057, 0.206, 0.237, 0.231, 0.244, 0.295,
0.217, 0.153, 0.214, 0.256, 0.17, 0.246, 0.25, 0.232, 0.247,
0.168, 0.247, 0.156, 0.214, 0.196, 0.183, 0.24, 0.205, 0.519,
0.197, 0.119, 0.306, 0.143, 0.257, 0.118, 0.269, 0.352, 0.256,
0.13, 0.307, 0.066, 0.272, 0.19, 0.261, 0.172, 0.222, 0.232,
0.236, 0.29, 0.276, 0.316, 0.217, 0.257, 0.206, 0.247, 0.28,
0.322, 0.304, 0.412, 0.36, 0.377, 0.421, 0.326, 0.33, 0.464,
0.331, 0.212, 0.456, 0.434, 0.486, 0.253, 0.48, 0.337, 0.549,
0.45, 0.611, 0.579, 0.594, 0.613, 0.672, 0.663, 0.389, 0.694,
0.718, 0.71, 0.692, 0.754, 0.816, 0.77, 0.863, 0.878, 0.388,
0.914, 0.42, 0.918, 1.06, 0.859, 1.58, 1.39, 0.922, 0.633,
1.22, 1.31, 0.399, 1.46, 1.71, 1.51, 1.58, 0.271, 0.44, 4.24,
3.65, 2.06, 2.61, 5e-04, 0.001, 0.065, 1.38, 2.91)), class = "data.frame", row.names = c(NA,
-361L))
>
Входной файл (Nitrate_set) включает 9 сайтов исследований. Мне удалось создать график корреляции между двумя интересующими переменными последовательно для каждого сайта в цикле, но я не могу добавить к соответствующему имени сайта в качестве заголовка для каждого графика. Вот пример данных:
SITE.NAME COR
1 Coochin Creek at Mawsons Road -0.1122249
2 Johnstone River at Coquette Point Gbr_Jri_Wq 0.3614868
3 Mulgrave River at Deeral 0.9338604
4 Pioneer River at Dumbleton Weir Headwater 0.7270477
5 Plane Creek at Sucrogen Weir 0.8337472
6 Proserpine River at Glen Isla 0.6695578
7 Russell River at East Russell 0.9879924
8 Sandy Creek at Homebush 0.9756037
9 Tully River at Euramo 0.9751152
Мне бы хотелось, чтобы на каждом корреляционном графике отображался заголовок, соответствующий названию его сайта. Я не уверен, где поместить этот код в цикл.
Это первый раз, когда я пытаюсь создать графики внутри цикла, и я начинающий программист. Я не знаю, что попробовать дальше ..
#Creating function func to calculate correlation for single site
func <- function(Nitrate_set){ return(data.frame(COR = cor(Nitrate_set$Trios.nitrate..mg.L., Nitrate_set$Chem.centre.Nox..mg.L.))) }
#Calling func Function for each site name and storing the results in cor_result. cor_result will show correlation for each site as a list.
cor_result <- ddply(Nitrate_set, .(SITE.NAME), func)
cor_result
#Creating function to display correlation plot for every site
funcPlot <- function(Nitrate_set){
chart.Correlation(Nitrate_set[,c(4,5)], histogram=TRUE, pch=19)
}
#Calling the function to display the plots. This will return nine plots serially. At this point we need to title site name for each site.
ddply(Nitrate_set, .(SITE.NAME), funcPlot)
Я ожидаю, что каждый график будет отображаться с заголовком, но я не могу понять, как и где поместить этот фрагмент кода в цикл