I am trying to remove the outliers from various variables at the same time in my dataset but with the function used it seems that when it finds one outlier it turns the whole row into NA.
That´s a problem because I have to apply the same process to a larger dataset and I am worried that it considerably reduces my sample...
So I would like to just turn the case where the outlier is into NA without turning the whole row into NA. Is that eventually possible?
Thank you for your input
#function used for outliers
outliers <- function(x) {
Q1 <- quantile(x, probs=.25, na.rm = TRUE)
Q3 <- quantile(x, probs=.75, na.rm = TRUE)
iqr = Q3-Q1
upper_limit = Q3 + (iqr*1.5)
lower_limit = Q1 - (iqr*1.5)
x > upper_limit | x < lower_limit
}
remove_outliers <- function(dflinear, cols = names(dflinear)) {
for (col in cols) {
dflinear <- dflinear[!outliers(dflinear[[col]]),]
}
dflinear
}
dflinear_without_outliers<-remove_outliers(dflinear, c("insuline", "glucose", "hdl","ldl"))
#Reproducible sample below
dflinear<- structure(list(id = structure(c("SA01", "SA02", "SA03", "SA04",
"SA05", "SA06", "SA07", "SA08", "SA09", "SA10", "SA11", "SA12",
"SA13", "SA14", "SA15", "SA16", "SA17", "SA18", "SA19", "SA20",
"SA21", "SA22", "SA23", "SA24", "SA25", "SA26", "SA27", "SA28",
"SA29", "SA30", "SA31", "SA32", "SA33", "SA34", "SA35", "SA36",
"SA37", "SA38", "SA39", "SA40", "SA41", "SA42", "SA43", "SA44",
"SA45", "SA46", "SA47", "SA48", "SA49", "SA50", "SA51", "SA52",
"SA53", "SA54", "SA56", "SA57", "SA58", "SA59", "SA60", "SA61",
"SA62", "SA63", "SA64", "SA65", "SA66", "SA67", "SA68", "SA69",
"SA72", "SA73", "SA74", "SA75", "SA76", "SA77", "SA78", "SA79",
"SA80", "SA81", "SA82", "SA83", "SA84", "SA85", "SA86", "SA87",
"SA88", "SA89", "SA90", "SA92", "SA93", "SA94", "SA95", "SA96",
"SA97", "SA99", "SA100", "SA101", "SA102", "SA103", "SA104",
"SA105", "SA107", "SA108", "SA109", "SA110", "SA111", "SA112",
"SA113", "SA114", "SA115", "SA116", "SA118", "SC01", "SC02",
"SC03", "SC04", "SC05", "SC06", "SC07", "SC08", "SC09", "SC10",
"SC11", "SC12", "SC13", "SC14", "SC15", "SC16", "SC17", "SC18",
"SC19", "SC20", "SC21", "SC22", "SC23", "SC24", "SC25", "SC26",
"SC27", "SC28", "SC29", "SC30", "SC31", "SC32", "SC33", "SC34",
"SC35", "SC36", "SC37", "SC38", "M01", "M02", "M03", "M04", "M05",
"M06", "M07", "M08", "M09", "M10", "M11", "M12", "M13", "M14",
"M15", "M16", "M17", "M18", "M19", "M20", "M21", "M22", "M23",
"M24", "M25", "M26", "M27", "M28", "M29", "M30", "M31", "M32",
"M33", "M34", "M35", "M36", "M37", "M38", "M39", "M40", "M41",
"M42", "M43", "M44", "M45", "M46", "M47", "M48", "M49", "M50",
"M51", "M52", "M53", "SA01", "SA02", "SA03", "SA04", "SA05",
"SA06", "SA07", "SA08", "SA09", "SA10", "SA11", "SA12", "SA13",
"SA14", "SA15", "SA16", "SA17", "SA18", "SA19", "SA20", "SA21",
"SA22", "SA23", "SA24", "SA25", "SA26", "SA27", "SA28", "SA29",
"SA30", "SA31", "SA32", "SA33", "SA34", "SA35", "SA36", "SA37",
"SA38", "SA39", "SA40", "SA41", "SA42", "SA43", "SA44", "SA45",
"SA46", "SA47", "SA48", "SA49", "SA50", "SA51", "SA52", "SA53",
"SA54", "SA56", "SA57", "SA58", "SA59", "SA60", "SA61", "SA62",
"SA63", "SA64", "SA65", "SA66", "SA67", "SA68", "SA69", "SA72",
"SA73", "SA74", "SA75", "SA76", "SA77", "SA78", "SA79", "SA80",
"SA81", "SA82", "SA83", "SA84", "SA85", "SA86", "SA87", "SA88",
"SA89", "SA90", "SA92", "SA93", "SA94", "SA95", "SA96", "SA97",
"SA99", "SA100", "SA101", "SA102", "SA103", "SA104", "SA105",
"SA107", "SA108", "SA109", "SA110", "SA111", "SA112", "SA113",
"SA114", "SA115", "SA116", "SA118", "SC01", "SC02", "SC03", "SC04",
"SC05", "SC06", "SC07", "SC08", "SC09", "SC10", "SC11", "SC12",
"SC13", "SC14", "SC15", "SC16", "SC17", "SC18", "SC19", "SC20",
"SC21", "SC22", "SC23", "SC24", "SC25", "SC26", "SC27", "SC28",
"SC29", "SC30", "SC31", "SC32", "SC33", "SC34", "SC35", "SC36",
"SC37", "SC38", "M01", "M02", "M03", "M04", "M05", "M06", "M07",
"M08", "M09", "M10", "M11", "M12", "M13", "M14", "M15", "M16",
"M17", "M18", "M19", "M20", "M21", "M22", "M23", "M24", "M25",
"M26", "M27", "M28", "M29", "M30", "M31", "M32", "M33", "M34",
"M35", "M36", "M37", "M38", "M39", "M40", "M41", "M42", "M43",
"M44", "M45", "M46", "M47", "M48", "M49", "M50", "M51", "M52",
"M53"), label = "Code of PrevenGo", format.spss = "A5", display_width = 12L),
group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), insuline = structure(c(9, 4.1, 3.3, 9.4, 22.9, 16.2,
8.7, 16.7, 21.2, 21, 12.8, 7.3, 38.4, 20.2, 19.6, 6.4, 18.9,
12.1, 8.2, 17, 15.6, 12.5, 19.1, 13.7, 8, 20.1, 19.8, 6.8,
15.4, 14.7, 11.9, 8.8, 7.9, 51.2, 10.8, 8.1, 28.6, 8.6, 27.9,
13.3, 9, 16.3, 13.3, 5.8, 27.3, 4.2, 8.2, 9.9, 20.1, 11.7,
8.7, 18.1, 10.9, 27.4, 14.6, 29.1, 10.2, 20.2, 9.7, 12.3,
18.2, 1.9, 11.6, 14.6, 7.9, 11.2, 13.8, 21.2, 23.8, 18, 23.5,
21.4, 11.4, 12, 6.6, 13.5, 10.4, 25.3, 56.8, 10.7, 21.5,
8.5, 30.2, 5.3, 7.5, 15.9, 11.6, 22.4, 25.2, 6.1, 15.1, 9.3,
24.3, 30.8, 8.9, 9.8, 34.1, 13.4, 23.1, 21.1, 4.8, 20.1,
38.5, 16.1, 34.1, 16.1, 17.7, 41.4, 20.4, 21.5, 36.3, 15.9,
8.8, 6.1, 29, 4, 23.1, 36.8, 16.4, 15.5, 28.8, 15.9, NA,
7.1, 6.1, 10, 9.1, 25.2, 19.1, 6.9, 14.7, 23.1, 19.3, 12.3,
7.3, 5.9, 8, 0.5, 9, 4, 10.4, 21.4, 14.6, 8.8, 24.5, 5.3,
9.8, 17.6, 10.2, 10.7, 23, 14.5, 4.6, 33.3, 23.3, 7.2, 3.7,
13.1, 6.7, 20, 7.5, 9.2, 4.5, 2.1, 7.7, 11.7, 7.6, 22.5,
8.8, 5.1, 14.8, 15.1, 18.8, 24.3, 14, 17.2, 16.2, 23.6, 17.4,
16.5, 12.1, 15.3, 11.4, 8.7, 22.6, 10.5, 7.4, 15.1, 13.1,
24.6, 19.3, 19.7, 14.1, 5.9, 19.7, 14.9, 5.9, 17.2, 16.9,
6.2, 11.2, 4.1, 10, 3.7, 3.6, 11.6, 16.9, NA, 8, 17.3, NA,
18.3, 4, 3.1, 26.4, 12.9, 17.9, 10.3, 22.5, NA, NA, 23.4,
15.1, NA, 11.9, 27, 6.2, NA, 21.5, 11.6, 15.8, 8.6, 15.2,
10.1, 20.6, 21.7, 45.3, 8.3, 19.5, 29.2, 21.5, 11.4, 9.5,
31.8, 35.3, 11.2, 15.4, NA, 8.5, 22.6, 14.3, NA, 11.8, 11.4,
4.2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 35.8, NA, NA,
NA, NA, NA, 19.7, 42.8, 30.6, 12.2, 5.2, 4.9, 20.4, NA, 23.5,
NA, 13.6, 19.4, 6.9, 16.7, 7.2, 14.7, 59.2, 22, 41.4, 18.1,
10.5, 19.8, 17.4, NA, 25.9, NA, 8.3, 25.9, 5.7, 17.1, 25.2,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 10.3, 9.1, 14.8,
13.7, 6.2, 17.9, 13.9, 14.6, 70.4, 23.6, 13.8, 15.2, 9.9,
14, 27.6, 14.3, 23.7, 11, 12.1, 13.5, 21, NA, 7.2, 12.3,
4.4, 6.2, 3.9, 15, 9.6, NA, 9, 10.3, NA, 13.3, 6, 11.3, 17.6,
8.5, 10, NA, 11.8, 10.4, 26.2, NA, 10, 5.7, 16.3, 4.7, 20.3,
7.7, 14.6, 9.4, 6.3, 10, 11.1, 6.7, 42.5, NA, NA, NA, 7.7,
18.6, NA, 16.7, 25.4, 21.8, 26.8, 10.2, 13.8, 11.6, 19.1,
8.3, 3.8, 31.1, NA, 7.1, 11.1, 8.7, 19, 16, 31.8, 11.7, 3.4,
17.6, 12.3, 5.1, 17.5, 6.7, 3.8, 16.6, 6.1), format.spss = "F4.2", display_width = 11L),
glucose = structure(c(90, 95, 79, 85, 95, 97, 86, 74, 88,
95, 94, 88, 86, 94, 86, 95, 97, 88, 88, 88, 83, 103, 79,
67, 88, 79, 90, 79, 97, 94, 85, 83, 88, 97, 81, 95, 92, 94,
99, 79, 83, 92, 81, 92, 79, 94, 83, 79, 81, 92, 86, 95, 92,
95, 92, 85, 94, 81, 86, 85, 99, 92, 85, 72, 86, 81, 79, 86,
97, 88, 92, 97, 83, 103, 97, 95, 85, 77, 77, 83, 99, 90,
77, 77, 83, 92, 88, 83, 88, 86, 88, 97, 101, 99, 88, 101,
94, 86, 85, 83, 86, 88, 92, 94, 94, 90, 160, 94, 83, 95,
97, 88, 88, 95, 90, 92, 113, 104, 85, 101, 91.8, 99, 94,
85, 85, 83, 86, 88, 95, 79, 101, 92, 83, 90, 85, 95, 88,
79, 90, 79, 94, 99, 83, 85, 85, 77, 99, 81, 92, 86.4, 95.4,
82.8, 73.8, 81, 90, 82.8, 79.2, 90, 82.8, 91.8, 90, 84.6,
84.6, 84.6, 77.4, 77.4, 75.6, 88.2, 79.2, 92, 90, 113, 81,
81, 81, 84.6, 88.2, 73.8, 81, 81, 82.8, 79.2, 70.2, 91.8,
97.2, 82.8, 70.2, 91.8, 93.6, 86.4, 93.6, 73.8, 95.4, 81,
97.2, 77.4, 90, 82.8, 86.4, 88.2, 88.2, 73.8, 90, 92, 83,
86, 99, NA, 86, 81, NA, 99, 83, 86, 76, 90, 85, 90, 92, NA,
NA, 79, 79, NA, 86, 81, 88, NA, 90, 86, 92, 85, 92, 83, 92,
90, 92, 95, 94, 88, 90, 86, 88, 101, 95, 92, 81, NA, 92,
90, 81, NA, 90, 81, 88, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 85, NA, NA, NA, NA, NA, 85, 88, 86, 88, 106, 101, 88,
NA, 79, NA, 85, 99, 92, 79, 88, 88, 95, 81, 86, 77, 81, 92,
97, NA, 86, NA, 88, 94, 81, 86, 85, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 85, 88, 95, 83, 92, 112, 94, 95, 108,
97, 90, 88, 86, 97, 95, 88, 90, 88, 77, 94, 81, NA, 79, 83,
95, 88, 81, 92, 92, NA, 88, 86, NA, 85, 85, 97, 81, 88, 90,
NA, 77.4, 94, 83, NA, 95, 85, 92, 83, 95, 88, 94, 94, 88,
77, 90, 86, 92, NA, NA, NA, 95, 92, NA, 90, 103, 90, 85,
92, 83, 81, 94, 81, 79, 94, NA, 92, 99, 95, 84, 95, 72, 90,
79, 97.5, 85, 88, 79, 81, 72, 85, 88), format.spss = "F4.2", display_width = 11L),
hdl = structure(c(54, 55, 48, 38, 46, 50, 45, 38, 50, 43,
39, 32, 35, 34, 40, 48, 53, 33, 42, 34, 41, 48, 51, 38, 53,
38, 37, 44, 37, 33, 54, 47, 51, 39, 44, 54, 32, 53, 39, 36,
58, 41, 34, 43, 40, 49, 49, 50, 37, 36, 54, 47, 35, 40, 50,
44, 40, 43, 45, 41, 34, 50, 46, 46, 50, 53, 53, 45, 37, 70,
51, 55, 51, 58, 58, 49, 44, 37, 32, 64, 41, 63, 46, 55, 46,
65, 43, 55, 42, 56, 39, 50, 38, 46, 45, 53, 53, 39, 45, 47,
48, 32, 45, 45, 36, 60, 30, 43, 43, 57, 36, 56, 45, 40, 40,
61, 50, 29, 55, 38, 35, 47, 42, 50, 46, 26, 60, 33, 36, 34,
44, 59, 45, 44, 55, 45, 53, 38, 50, 40, 57, 46, 48, 45, 43,
49, 53, 39, 46, 39, 36, 39, 36, 42, 40, 50, 63, 46, 45, 39,
43, 30, 57, 46, 40, 39, 39, 53, 40, 54, 56, 40, 37, 48, 43,
29, 46, 45, 82, 31, 34, 37, 41, 63, 34, 50, 37, 51, 36, 42,
41, 34, 55, 40, 42, 60, 36, 38, 52, 57, 48, 48, 46, 47, 50,
41, 48, NA, 40, 45, NA, 43, 58, 42, 48, 44, 46, 47, 55, NA,
NA, 38, 52, NA, 53, 31, 51, NA, 32, 51, 41, 38, 57, 36, 50,
41, 60, 65, 39, 52, 36, 36, 49, 43, 34, 44, 41, NA, 50, 52,
37, NA, 58, 45, 34, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
46, NA, NA, NA, NA, NA, 59, 55, 50, 46, 58, 58, 42, NA, 31,
NA, 48, 43, 66, 55, 51, 41, 50, 38, 46, 41, 43, 38, 48, NA,
46, NA, 56, 44, 46, 48, 49, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 63, 41, 39, 46, 58, 53, 33, 53, 48, 33, 44, 46,
49, 48, 44, 55, 44, 39, 32, 46, 50, NA, 47, 53, 39, 51, 61,
48, 32, NA, 42, 46, NA, 49, 48, 52, 39, 40, 38, NA, 31, 46,
48, NA, 51, 58, 43, 49, 43, 65, 41, 61, 49, 35, 37, 36, 58,
NA, NA, NA, 38, 45, NA, 58, 31, 49, 52, 65, 32, 45, 39, 37,
41, 34, NA, 42, 51, 39, 48, 36, 35, 55, 38, 48, 53, 41, 39,
49, 63, 41, 47), label = "HDL-Cholesterol", format.spss = "F3.2", display_width = 11L),
ldl = structure(c(100, 104, 171, 153, 107, 152, 87, 101,
70, 137, 96, 95, 98, 94, 92, 102, 63, 104, 62, 75, 125, 117,
114, 132, 112, 146, 121, 91, 113, 120, 96, 96, 95, 87, 96,
134, 98, 92, 88, 101, 133, 113, 77, 128, 97, 169, 136, 96,
74, 59, 121, 66, 109, 103, 116, 86, 87, 124, 88, 94, 77,
98, 90, 133, 79, 78, 98, 129, 62, 62, 96, 72, 85, 98, 101,
132, 69, 196, 76, 125, 105, 108, 89, 108, 123, 51, 92, 50,
121, 105, 80, 103, 59, 96, 89, 65, 77, 90, 92, 65, 123, 96,
80, 128, 92, 124, 96, 83, 120, 145, 114, 134, 116, 65, 91,
103, 84, 123, 99, 96, 61, 82, 85, 116, 116, 113, 121, 69,
82, 100, 108, 99, 144, 152, 158, 128, 112, 89, 119, 61, 99,
147, 109, 121, 92, 115, 95, 62, 72, 130, 96, 76, 117, 96,
108, 131, 120, 67, 99, 105, 63, 63, 103, 128, 92, 120, 146,
106, 103, 94, 85, 122, 111, 102, 143, 74, 87, 80, 67, 140,
85, 87, 101, 94, 122, 124, 82, 150, 92, 84, 119, 98, 89,
97, 117, 122, 111, 86, 90, 110, 107, 150, 103, 94, 149, 159,
91, NA, 109, 126, NA, 167, 77, 90, 103, 80, 68, 75, 55, NA,
NA, 74, 113, NA, 102, 116, 84, NA, 66, 85, 114, 111, 101,
95, 92, 86, 96, 90, 92, 77, 91, 108, 86, 118, 85, 127, 99,
NA, 160, 80, 63, NA, 123, 86, 94, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 106, NA, NA, NA, NA, NA, 70, 85, 70, 96,
102, 117, 101, NA, 146, NA, 94, 122, 122, 94, 110, 121, 39,
72, 48, 109, 110, 60, 95, NA, 83, NA, 79, 87, 113, 103, 55,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 157, 103, 56,
92, 114, 78, 97, 106, 117, 61, 72, 83, 91, 122, 106, 103,
89, 51, 89, 153, 90, NA, 132, 132, 110, 84, 84, 96, 72, NA,
104, 122, NA, 80, 113, 106, 62, 72, 121, NA, 102, 125, 130,
NA, 111, 119, 66, 109, 119, 91, 92, 120, 160, 93, 117, 126,
88, NA, NA, NA, 115, 100, NA, 200, 79, 95, 99, 89, 123, 108,
82, 108, 81, 103, NA, 103, 149, 116, 115, 122, 95, 106, 89,
128, 118, 123, 51, 90, 130, 119, 120), label = "LDL-Cholesterol", format.spss = "F4.2", display_width = 11L)), row.names = c(NA,
-404L), class = c("tbl_df", "tbl", "data.frame"), reshapeLong = list(
varying = list(c("age_1", "age_2"), c("whz_1", "whz_2"),
c("haz_1", "haz_2"), c("waz_1", "waz_2"), c("zbmi_1",
"zbmi_2"), c("wc_1", "wc_2"), c("abc_1", "abc_2"), c("PA_1",
"PA_2"), c("PAextra_1", "PAextra_2"), c("TVweekdays_1",
"TVweekdays_2"), c("TVweekend_1", "TVweekend_2"), c("kidmed_1",
"kidmed_2"), c("totalcholesterol_1", "totalcholesterol_2"
), c("ldl_1", "ldl_2"), c("hdl_1", "hdl_2"), c("triglycerides_1",
"triglycerides_2"), c("glucose_1", "glucose_2"), c("insuline_1",
"insuline_2"), c("hba1c_1", "hba1c_2"), c("homair_1",
"homair_2"), c("fatmass_1", "fatmass_2"), c("energykcal_1",
"energykcal_2"), c("protein_1", "protein_2"), c("proteinpc_1",
"proteinpc_2"), c("carbohydrates_1", "carbohydrates_2"
), c("carbohydratespc_1", "carbohydratespc_2"), c("sugar_1",
"sugar_2"), c("sugarpc_1", "sugarpc_2"), c("starch_1",
"starch_2"), c("fruitportions_1", "fruitportions_2"),
c("vegetablesportions_1", "vegetablesportions_2"), c("vegetalfiber_1",
"vegetalfiber_2"), c("solublefiber_1", "solublefiber_2"
), c("insolublefiber_1", "insolublefiber_2"), c("lipids_1",
"lipids_2"), c("lipidspc_1", "lipidspc_2"), c("sfa_1",
"sfa_2"), c("sfapc_1", "sfapc_2"), c("mufa_1", "mufa_2"
), c("mufapc_1", "mufapc_2"), c("pufa_1", "pufa_2"),
c("pufapc_1", "pufapc_2"), c("cholesterolintake_1", "cholesterolintake_2"
)), v.names = c("age", "whz", "haz", "waz", "zbmi", "wc",
"abc", "PA", "PAextra", "TVweekdays", "TVweekend", "kidmed",
"totalcholesterol", "ldl", "hdl", "triglycerides", "glucose",
"insuline", "hba1c", "homair", "fatmass", "energykcal", "protein",
"proteinpc", "carbohydrates", "carbohydratespc", "sugar",
"sugarpc", "starch", "fruitportions", "vegetablesportions",
"vegetalfiber", "solublefiber", "insolublefiber", "lipids",
" lipidspc", "sfa", "sfapc", "mufa", "mufapc", "pufa", "pufapc",
"cholesterolintake"), idvar = c("id", "group"), timevar = "time"))
You can drop the outliers by changing your remove_outlier function to this:
remove_outliers <- function(dflinear, cols = names(dflinear)) {
for (col in cols) {
dflinear[,col] <- ifelse(outliers(dflinear[[col]]),NA,dflinear[[col]])
}
dflinear
}
But I would think very carefully about whether this is a good approach to outlier detection and removal. This procedure is removing values that look like regular parts of the distribution. With a lot of values you would expect some to be outside of the range Q3+1.5IQR etc.
Eg, this is the qqnorm for the ldl variable. Doesn't look like any problematic values at all really, but your procedure is throwing out the top five and the lowest value:
Related
Is there a way to put arrows on a plot with multiple plots with different y axes? I would like to put arrows across the time series on the same x axis locations but different y locations. I cant just use "annotate("segment", x = 37, xend = 84, y = 0.0, yend = 0.0,colour = "black", size = 1, arrow = arrow(ends='both'))" because then it puts them at 0 on the y axis for all variables when I actually want to just put the arrows at the bottom of the y axis which is different for every variable.
Current code:
fin_plot <- ggplot(melted_data, aes(x = `Distance`, y = value, group = variable)) + geom_line() + theme_bw() + labs(y="", x= "") + theme_classic() + theme(text=element_text(size=16, family="serif", face = "bold", color = "black")) +
facet_wrap(variable~., scales = "free_y",ncol=2) +
scale_x_continuous(limits = c(0, 250),labels = scales::number_format(accuracy = 1)) + theme(axis.line = element_line(colour = 'black', size = 1)) +
theme(axis.ticks = element_line(colour = "black", size = 1)) + scale_y_continuous(labels = scales::number_format(accuracy = 0.1))+ theme(axis.ticks.length = unit(.3, "cm")) + coord_capped_cart(bottom='right', left='none', gap = 0.15) + geom_vline(xintercept=c(58, 132, 204, 250, 309), linetype='dashed', col = 'black')
Current output
desired output
data:
melted_data <- structure(list(Distance = 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, 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, 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, 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, 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), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L), .Label = c("Mg", "Mn", "Zn", "Ba", "All"), class = "factor"),
value = c(0.903247645, 0.912560748, 0.896003508, 0.909572697,
0.883631829, 0.905722594, 0.892465355, 0.909271173, 0.880506202,
0.889278401, 0.878534542, 0.959209459, 0.913303825, 0.929893977,
0.97778374, 0.9885554, 0.929716333, 1.028422583, 1.025638955,
1.011352651, 1.041343955, 1.092562951, 1.129761801, 1.088857171,
1.107257284, 1.116728405, 1.103053734, 1.041662037, 1.134182243,
1.104550315, 1.086952767, 1.106004784, 1.057688595, 1.034347579,
1.04641385, 1.139270945, 1.048446018, 1.033827731, 1.075554754,
1.029893202, 1.074749532, 1.001626205, 0.977053541, 0.987467665,
0.999540478, 0.945184816, 0.959677178, 0.962807712, 0.967023936,
1.024286493, 0.881264816, 0.967181342, 1.000316876, 0.956168258,
1.003214572, 1.00047837, 0.940103474, 0.929875987, 0.928227112,
0.982410241, 0.983035162, 0.976666772, 1.019755049, 1.075189042,
0.975380543, 0.981316782, 0.986876269, 1.026690916, 1.052379934,
1.001547298, 0.979888683, 1.008209647, 0.976098272, 0.944479556,
0.996767684, 1.018077758, 1.028862706, 1.08510417, 1.08963868,
1.048481179, 1.139954126, 1.107066353, 1.122920581, 1.23904326,
1.19449336, 1.179971969, 1.165865352, 1.068804094, 1.099436469,
1.073307737, 1.07045113, 1.101007051, 1.011962649, 1.11202545,
1.097883672, 1.05361424, 0.993283703, 1.046635444, 1.04951188,
1.055736151, 1.063705172, 0.977095039, 1.015650848, 1.029367222,
1.003814349, 0.973376993, 1.021665177, 0.925511352, 1.014703757,
0.933654542, 1.027336075, 0.961163947, 1.022921765, 0.910164297,
0.937410814, 0.935246588, 0.925900983, 0.934477753, 0.927973832,
0.946372309, 0.950554394, 0.9386026, 1.000712639, 0.947846812,
0.953585987, 0.967735737, 0.927914753, 0.943303715, 0.935435884,
0.987648375, 0.902379461, 0.939086878, 1.018529942, 0.973874968,
0.974093087, 0.984149676, 0.948669001, 0.934863295, 1.011232041,
0.942884239, 0.978044788, 1.023700208, 1.011714275, 0.999153709,
1.06822476, 0.967735328, 1.131133479, 1.011068503, 1.034903609,
0.086720869, 0.113119382, 0.088197332, 0.081547788, 0.079373211,
0.07888827, 0.072865285, 0.079637996, 0.066314774, 0.097585729,
0.185034982, 0.214466904, 0.294317625, 0.481389256, 0.531196058,
0.715842439, 0.865098887, 0.987242052, 1.081028291, 1.240920518,
1.313524957, 1.543771699, 1.78495042, 1.746572555, 2.048760527,
2.101438775, 1.967474033, 2.000286925, 2.014020838, 1.924470659,
1.75696549, 1.786681246, 1.633290961, 1.455799758, 1.315346538,
1.435348984, 1.27887702, 1.152818928, 1.095127218, 0.987502349,
1.062278922, 0.898540082, 0.83617998, 0.889057689, 0.825563648,
0.788347646, 0.790973555, 0.775541228, 0.815063004, 0.848723108,
0.66783059, 0.672629631, 0.747809615, 0.72338158, 0.666220438,
0.664051795, 0.597260657, 0.689282162, 0.663808452, 0.678551141,
0.672917354, 0.686199986, 0.724202364, 0.746195474, 0.686135659,
0.654148537, 0.713488795, 0.72446665, 0.699529989, 0.630120423,
0.661767463, 0.663290351, 0.705879842, 0.709399338, 0.76228353,
0.714368918, 0.720561695, 0.837036666, 0.923882149, 1.014163852,
1.221410703, 1.315825246, 1.368054705, 1.641746627, 1.630198312,
1.698589629, 1.562956393, 1.427322658, 1.53964983, 1.574583495,
1.527101216, 1.380123116, 1.28649445, 1.29251968, 1.330565441,
1.317758525, 1.19292313, 1.217953538, 1.218591815, 1.163372928,
1.091026791, 0.878691182, 0.903966928, 0.917620557, 0.838430901,
0.825709255, 0.839298558, 0.76309434, 0.97617394, 0.739885015,
0.822159341, 0.785335779, 0.771926988, 0.766619321, 0.832448556,
0.733734124, 0.787221188, 0.685452005, 0.740552711, 0.707414697,
0.781271754, 0.72652958, 0.729470139, 0.71649368, 0.681176551,
0.683977986, 0.711079301, 0.681092777, 0.747615639, 0.700953146,
0.692246657, 0.673560118, 0.820384633, 0.740567172, 0.72070082,
0.795192662, 0.773897168, 0.74552279, 0.735710787, 0.768825863,
0.746016457, 0.736542042, 0.744507532, 0.784312542, 0.758393534,
0.7600356, 0.797384742, 0.773626898, 0.744557896, 0.746612627,
0.818368055, 0.696689824, 0.748702805, 0.717457681, 0.766243608,
0.805305259, 0.855909762, 0.803357905, 0.889646097, 0.854456208,
1.067795473, 1.051422575, 1.17061972, 1.138440648, 1.052796919,
1.040998633, 1.161739158, 1.025956799, 0.971567748, 1.072911493,
0.952121155, 1.040392714, 1.069745522, 1.068549198, 1.090194087,
1.214584829, 1.157485471, 1.245813376, 1.336359991, 1.204038397,
1.126255292, 1.131057736, 0.922042386, 1.037566449, 1.100852394,
1.121842367, 0.998657748, 1.006938923, 1.002800377, 0.897387497,
0.93902937, 0.889327622, 0.802133735, 0.855245047, 0.860702407,
0.704324249, 0.905827093, 0.760155095, 0.760247698, 0.655991619,
0.677006743, 0.668001976, 0.623410532, 0.569302474, 0.523713794,
0.690042836, 0.539115342, 0.528696218, 0.57851915, 0.60294784,
0.581392042, 0.65277069, 0.65620614, 0.625397246, 0.697647782,
0.6180657, 0.632326126, 0.684659215, 0.606197513, 0.630134281,
0.637151517, 0.574538208, 0.605993607, 0.533522181, 0.544522236,
0.577535469, 0.573427383, 0.672984155, 0.735286828, 0.7532343,
0.881292245, 0.801132661, 1.122761046, 1.137397845, 1.173190388,
1.138033979, 1.126494557, 1.144871399, 1.087042815, 0.981750792,
0.992888445, 0.955352455, 1.074357698, 1.027127808, 1.083248059,
1.010304962, 1.037776316, 1.052809984, 0.959161909, 0.939369893,
0.932304641, 0.912110856, 1.035278327, 0.825391661, 0.883818816,
0.880397247, 0.775385156, 0.860535004, 0.75878312, 0.764243502,
0.788209749, 0.736029937, 0.746966542, 0.762295984, 0.804665042,
0.797845669, 0.744225613, 0.846139103, 0.806957411, 0.789078125,
0.912631032, 0.926629248, 0.807376002, 0.795165332, 0.776764645,
0.811532921, 0.740169463, 0.707007363, 0.764252403, 0.754265833,
0.656183602, 0.78602999, 0.734580057, 0.756587437, 0.750509131,
0.727536118, 0.676232276, 0.714439923, 0.720668076, 0.763533465,
0.60234143, 0.651920197, 0.744086872, 0.633919728, 0.615213712,
0.705944962, 0.667362984, 0.742636421, 0.742734852, 0.839492568,
0.743899849, 0.817080816, 0.773569657, 0.735728339, 0.715168283,
0.78077814, 0.694280484, 0.773303425, 0.768041196, 0.883401699,
0.818274274, 0.715927964, 0.696938222, 0.832246446, 0.73089346,
0.790965216, 0.799717389, 0.865896893, 0.946771069, 0.954212275,
1.023740345, 1.027036123, 1.086336263, 1.064542815, 0.9463809,
0.924081609, 0.999832641, 0.911277648, 0.922871168, 0.953134033,
0.786732115, 0.802026729, 0.832863371, 0.863952475, 0.817833153,
0.748586924, 0.72095701, 0.738213943, 0.672736744, 0.704947698,
0.531743532, 0.634123809, 0.683548549, 0.733277161, 0.608993729,
0.752162246, 0.568705823, 0.643172511, 0.597251486, 0.655514695,
0.583437677, 0.557676441, 0.646713866, 0.527005047, 0.578023512,
0.576281064, 0.600923204, 0.578475648, 0.551957027, 0.585007991,
0.623858699, 0.630936819, 0.636198589, 0.565476603, 0.658861425,
0.577557604, 0.629178306, 0.646092809, 0.566079299, 0.60953767,
0.680135261, 0.500802233, 0.704656678, 0.61109605, 0.645344144,
0.667139888, 0.734969576, 0.780062983, 0.783090234, 0.83005691,
0.905356723, 0.933746319, 0.947613375, 0.923115827, 0.873482691,
0.746883952, 0.850273618, 0.795256154, 0.800825928, 0.772630039,
0.749567395, 0.7823457, 0.772609842, 0.736269985, 0.699705666,
0.716860238, 0.65909369, 0.806743181, 0.604632102, 0.629103485,
0.669824708, 0.545219042, 0.605081484, 0.545598194, 0.612458887,
0.640840679, 0.568115521, 0.578270006, 0.642784637, 0.486235168,
0.608704086, 0.449107996, 0.603056279, 0.573624703, 0.527880861,
0.479058818, 0.608581986, 0.497792884, 0.736359035, 0.560758315,
0.59150912, 0.491623628, 0.646548159, 0.559243084, 0.554057512,
0.542344646, 0.583808567, 0.623315676, 0.521008383, 0.511710892,
0.633820855, 0.529775704, 0.590383598, 0.500021436, 0.602344336,
0.499887402, 0.534870849, 0.583225149, 0.623554367, 0.62596102,
0.585378422, 0.648988779, 0.577416685, 0.632021029, 0.644454559,
0.684966009, 0.595845502, 2.479315993, 2.683540753, 2.424790513,
2.556904106, 2.454032378, 2.486582811, 2.485804182, 2.625597071,
2.444459365, 2.649813652, 2.686066928, 3.124873535, 3.077318299,
3.297830917, 3.344358668, 3.589441204, 3.566707313, 3.968369009,
3.932341434, 4.08973781, 4.374551474, 4.54266808, 4.97884528,
4.932211371, 5.310903272, 5.372904082, 5.231493496, 5.123516042,
5.393849098, 5.276658613, 4.970827822, 4.972075355, 4.608769407,
4.214216452, 4.232190208, 4.539424798, 4.266998558, 3.933891331,
3.898577905, 3.758409871, 3.707152695, 3.544143355, 3.234304675,
3.312782898, 3.363897722, 3.32751203, 3.063968711, 3.396338279,
3.110947858, 3.27642981, 2.802338511, 2.972332411, 2.999566144,
2.860636811, 2.88545135, 2.715249006, 2.805430479, 2.734554555,
2.721654986, 2.81795618, 2.810857383, 2.829266791, 3.020586802,
3.108527475, 2.923112037, 2.898589704, 2.977292189, 2.961041296,
3.065747444, 2.883958043, 2.837869726, 2.918189185, 2.936651583,
2.760674734, 2.997230073, 2.888064962, 2.972304014, 3.162708107,
3.42147456, 3.577994842, 3.897689363, 4.134240754, 4.19746467,
4.937297252, 4.909702892, 4.974867813, 4.740338415, 4.369505261,
4.634231316, 4.530190201, 4.380129066, 4.246648651, 4.003376949,
4.261248528, 4.228186763, 4.190890809, 3.896217461, 4.019225536,
3.980007369, 3.985014169, 3.698733958, 3.417194347, 3.50155334,
3.527485148, 3.272718395, 3.228503258, 3.353819869, 3.104831527,
3.419528222, 3.010592683, 3.256523555, 3.020944643, 3.139582776,
2.872858156, 3.135211633, 3.047270457, 3.038848701, 2.843214189,
3.123247632, 2.958537301, 3.257263308, 3.138521527, 3.248321146,
2.963340122, 3.076476029, 2.987721452, 3.004584487, 2.906910601,
2.973867453, 3.0761696, 2.869900334, 2.78054149, 3.25876542,
2.978797901, 3.041764942, 3.029872905, 3.052446623, 2.856505763,
2.9962536, 3.015603327, 3.111149077, 2.9885447, 2.993520426,
3.176541902, 3.037954707, 2.975005669, 3.278917742, 3.137024394,
3.117943428)), row.names = c(NA, -745L), class = "data.frame")
Use geom_segment - this allows you to make use of the faceting variable. You will then want to pass a data frame with the respective x/xend/y/yend.
library(dplyr)
## create a data frame first for the segments
## it makes sense to use the mininimum of your y for each facet
annot_df <- melted_data %>%
group_by(variable) %>%
summarise(y = min(value), yend = min(value), x = 25, xend = 75)
ggplot(melted_data, aes(x = Distance, y = value, group = variable)) +
geom_line() +
## now use the new data frame for geom_segment
geom_segment(data = annot_df, aes(x = x, xend = xend, y = y, yend = yend),
arrow = arrow(ends = "both", length = unit(5, "pt"))) +
facet_wrap(variable~., scales = "free_y",ncol=2)
Created on 2022-07-14 by the reprex package (v2.0.1)
This question already has answers here:
Changing the line type in the ggplot legend
(2 answers)
ggplot2 for grayscale printouts
(3 answers)
Closed 7 months ago.
I am trying to make a time-series graph with multiple y values. I would like to change the shape of the different variables so some are solid, some are dashed etc. I would also like all the colors to be on greyscale.
Does anyone know how I can accomplish this?
I know how to melt my data so that I can plot them all together by the value of the variables but right now I cannot get the shapes to change or the greyscale. Thank you in advance.
ggplot(melted_data, aes(x = Distance, y = value, color = variable)) + geom_line()
data <- structure(list(Distance = 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, 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, 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, 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), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Mg",
"Mn", "Zn", "Ba"), class = "factor"), value = c(0.903247645,
0.912560748, 0.896003508, 0.909572697, 0.883631829, 0.905722594,
0.892465355, 0.909271173, 0.880506202, 0.889278401, 0.878534542,
0.959209459, 0.913303825, 0.929893977, 0.97778374, 0.9885554,
0.929716333, 1.028422583, 1.025638955, 1.011352651, 1.041343955,
1.092562951, 1.129761801, 1.088857171, 1.107257284, 1.116728405,
1.103053734, 1.041662037, 1.134182243, 1.104550315, 1.086952767,
1.106004784, 1.057688595, 1.034347579, 1.04641385, 1.139270945,
1.048446018, 1.033827731, 1.075554754, 1.029893202, 1.074749532,
1.001626205, 0.977053541, 0.987467665, 0.999540478, 0.945184816,
0.959677178, 0.962807712, 0.967023936, 1.024286493, 0.881264816,
0.967181342, 1.000316876, 0.956168258, 1.003214572, 1.00047837,
0.940103474, 0.929875987, 0.928227112, 0.982410241, 0.983035162,
0.976666772, 1.019755049, 1.075189042, 0.975380543, 0.981316782,
0.986876269, 1.026690916, 1.052379934, 1.001547298, 0.979888683,
1.008209647, 0.976098272, 0.944479556, 0.996767684, 1.018077758,
1.028862706, 1.08510417, 1.08963868, 1.048481179, 1.139954126,
1.107066353, 1.122920581, 1.23904326, 1.19449336, 1.179971969,
1.165865352, 1.068804094, 1.099436469, 1.073307737, 1.07045113,
1.101007051, 1.011962649, 1.11202545, 1.097883672, 1.05361424,
0.993283703, 1.046635444, 1.04951188, 0.086720869, 0.113119382,
0.088197332, 0.081547788, 0.079373211, 0.07888827, 0.072865285,
0.079637996, 0.066314774, 0.097585729, 0.185034982, 0.214466904,
0.294317625, 0.481389256, 0.531196058, 0.715842439, 0.865098887,
0.987242052, 1.081028291, 1.240920518, 1.313524957, 1.543771699,
1.78495042, 1.746572555, 2.048760527, 2.101438775, 1.967474033,
2.000286925, 2.014020838, 1.924470659, 1.75696549, 1.786681246,
1.633290961, 1.455799758, 1.315346538, 1.435348984, 1.27887702,
1.152818928, 1.095127218, 0.987502349, 1.062278922, 0.898540082,
0.83617998, 0.889057689, 0.825563648, 0.788347646, 0.790973555,
0.775541228, 0.815063004, 0.848723108, 0.66783059, 0.672629631,
0.747809615, 0.72338158, 0.666220438, 0.664051795, 0.597260657,
0.689282162, 0.663808452, 0.678551141, 0.672917354, 0.686199986,
0.724202364, 0.746195474, 0.686135659, 0.654148537, 0.713488795,
0.72446665, 0.699529989, 0.630120423, 0.661767463, 0.663290351,
0.705879842, 0.709399338, 0.76228353, 0.714368918, 0.720561695,
0.837036666, 0.923882149, 1.014163852, 1.221410703, 1.315825246,
1.368054705, 1.641746627, 1.630198312, 1.698589629, 1.562956393,
1.427322658, 1.53964983, 1.574583495, 1.527101216, 1.380123116,
1.28649445, 1.29251968, 1.330565441, 1.317758525, 1.19292313,
1.217953538, 1.218591815, 0.746612627, 0.818368055, 0.696689824,
0.748702805, 0.717457681, 0.766243608, 0.805305259, 0.855909762,
0.803357905, 0.889646097, 0.854456208, 1.067795473, 1.051422575,
1.17061972, 1.138440648, 1.052796919, 1.040998633, 1.161739158,
1.025956799, 0.971567748, 1.072911493, 0.952121155, 1.040392714,
1.069745522, 1.068549198, 1.090194087, 1.214584829, 1.157485471,
1.245813376, 1.336359991, 1.204038397, 1.126255292, 1.131057736,
0.922042386, 1.037566449, 1.100852394, 1.121842367, 0.998657748,
1.006938923, 1.002800377, 0.897387497, 0.93902937, 0.889327622,
0.802133735, 0.855245047, 0.860702407, 0.704324249, 0.905827093,
0.760155095, 0.760247698, 0.655991619, 0.677006743, 0.668001976,
0.623410532, 0.569302474, 0.523713794, 0.690042836, 0.539115342,
0.528696218, 0.57851915, 0.60294784, 0.581392042, 0.65277069,
0.65620614, 0.625397246, 0.697647782, 0.6180657, 0.632326126,
0.684659215, 0.606197513, 0.630134281, 0.637151517, 0.574538208,
0.605993607, 0.533522181, 0.544522236, 0.577535469, 0.573427383,
0.672984155, 0.735286828, 0.7532343, 0.881292245, 0.801132661,
1.122761046, 1.137397845, 1.173190388, 1.138033979, 1.126494557,
1.144871399, 1.087042815, 0.981750792, 0.992888445, 0.955352455,
1.074357698, 1.027127808, 1.083248059, 1.010304962, 1.037776316,
1.052809984, 0.742734852, 0.839492568, 0.743899849, 0.817080816,
0.773569657, 0.735728339, 0.715168283, 0.78077814, 0.694280484,
0.773303425, 0.768041196, 0.883401699, 0.818274274, 0.715927964,
0.696938222, 0.832246446, 0.73089346, 0.790965216, 0.799717389,
0.865896893, 0.946771069, 0.954212275, 1.023740345, 1.027036123,
1.086336263, 1.064542815, 0.9463809, 0.924081609, 0.999832641,
0.911277648, 0.922871168, 0.953134033, 0.786732115, 0.802026729,
0.832863371, 0.863952475, 0.817833153, 0.748586924, 0.72095701,
0.738213943, 0.672736744, 0.704947698, 0.531743532, 0.634123809,
0.683548549, 0.733277161, 0.608993729, 0.752162246, 0.568705823,
0.643172511, 0.597251486, 0.655514695, 0.583437677, 0.557676441,
0.646713866, 0.527005047, 0.578023512, 0.576281064, 0.600923204,
0.578475648, 0.551957027, 0.585007991, 0.623858699, 0.630936819,
0.636198589, 0.565476603, 0.658861425, 0.577557604, 0.629178306,
0.646092809, 0.566079299, 0.60953767, 0.680135261, 0.500802233,
0.704656678, 0.61109605, 0.645344144, 0.667139888, 0.734969576,
0.780062983, 0.783090234, 0.83005691, 0.905356723, 0.933746319,
0.947613375, 0.923115827, 0.873482691, 0.746883952, 0.850273618,
0.795256154, 0.800825928, 0.772630039, 0.749567395, 0.7823457,
0.772609842, 0.736269985, 0.699705666, 0.716860238, 0.65909369
)), row.names = c(NA, -396L), class = "data.frame")
You can use the linetype parameter with the aestethics :
ggplot(data) +
geom_line(aes(x = Distance, y = value, color = variable, linetype = variable))
I have this function that allows me to create multiple graphs on various variables of the dataset.
However in the output on the y-axis it always put the name of the list "varlist" instead of the name of each variable in the list, i.e. insuline, glucose, hdl and ldl.
How could I do that? thank you
# Multiple box plot per group per time
library(ggplot2)
names(dflinear) <- c("id", "group", "sex", "time", "insuline", "glucose", "hdl", "ldl")
# Create a list wherein the function will be applied to
varlist<-c(list(dflinear$insuline, dflinear$glucose, dflinear$hdl, dflinear$ldl))
names(varlist)<-c("insuline", "glucose", "hdl", "ldl")
# Create the function boxplot
A <- function (varlist) {
dflinear %>% group_by('group')%>%
ggplot(mapping = aes_string(x='time', y='varlist', fill='group')) +
geom_boxplot()
}
# Apply it to the whole list and graph the plots
plots<-lapply(varlist, FUN = A)
plots
Reproducible dataset
dflinear<- structure(list(id = structure(c("SA01", "SA02", "SA03", "SA04",
"SA05", "SA06", "SA07", "SA08", "SA09", "SA10", "SA11", "SA12",
"SA13", "SA14", "SA15", "SA16", "SA17", "SA18", "SA19", "SA20",
"SA21", "SA22", "SA23", "SA24", "SA25", "SA26", "SA27", "SA28",
"SA29", "SA30", "SA31", "SA32", "SA33", "SA34", "SA35", "SA36",
"SA37", "SA38", "SA39", "SA40", "SA41", "SA42", "SA43", "SA44",
"SA45", "SA46", "SA47", "SA48", "SA49", "SA50", "SA51", "SA52",
"SA53", "SA54", "SA56", "SA57", "SA58", "SA59", "SA60", "SA61",
"SA62", "SA63", "SA64", "SA65", "SA66", "SA67", "SA68", "SA69",
"SA72", "SA73", "SA74", "SA75", "SA76", "SA77", "SA78", "SA79",
"SA80", "SA81", "SA82", "SA83", "SA84", "SA85", "SA86", "SA87",
"SA88", "SA89", "SA90", "SA92", "SA93", "SA94", "SA95", "SA96",
"SA97", "SA99", "SA100", "SA101", "SA102", "SA103", "SA104",
"SA105", "SA107", "SA108", "SA109", "SA110", "SA111", "SA112",
"SA113", "SA114", "SA115", "SA116", "SA118", "SC01", "SC02",
"SC03", "SC04", "SC05", "SC06", "SC07", "SC08", "SC09", "SC10",
"SC11", "SC12", "SC13", "SC14", "SC15", "SC16", "SC17", "SC18",
"SC19", "SC20", "SC21", "SC22", "SC23", "SC24", "SC25", "SC26",
"SC27", "SC28", "SC29", "SC30", "SC31", "SC32", "SC33", "SC34",
"SC35", "SC36", "SC37", "SC38", "M01", "M02", "M03", "M04", "M05",
"M06", "M07", "M08", "M09", "M10", "M11", "M12", "M13", "M14",
"M15", "M16", "M17", "M18", "M19", "M20", "M21", "M22", "M23",
"M24", "M25", "M26", "M27", "M28", "M29", "M30", "M31", "M32",
"M33", "M34", "M35", "M36", "M37", "M38", "M39", "M40", "M41",
"M42", "M43", "M44", "M45", "M46", "M47", "M48", "M49", "M50",
"M51", "M52", "M53", "SA01", "SA02", "SA03", "SA04", "SA05",
"SA06", "SA07", "SA08", "SA09", "SA10", "SA11", "SA12", "SA13",
"SA14", "SA15", "SA16", "SA17", "SA18", "SA19", "SA20", "SA21",
"SA22", "SA23", "SA24", "SA25", "SA26", "SA27", "SA28", "SA29",
"SA30", "SA31", "SA32", "SA33", "SA34", "SA35", "SA36", "SA37",
"SA38", "SA39", "SA40", "SA41", "SA42", "SA43", "SA44", "SA45",
"SA46", "SA47", "SA48", "SA49", "SA50", "SA51", "SA52", "SA53",
"SA54", "SA56", "SA57", "SA58", "SA59", "SA60", "SA61", "SA62",
"SA63", "SA64", "SA65", "SA66", "SA67", "SA68", "SA69", "SA72",
"SA73", "SA74", "SA75", "SA76", "SA77", "SA78", "SA79", "SA80",
"SA81", "SA82", "SA83", "SA84", "SA85", "SA86", "SA87", "SA88",
"SA89", "SA90", "SA92", "SA93", "SA94", "SA95", "SA96", "SA97",
"SA99", "SA100", "SA101", "SA102", "SA103", "SA104", "SA105",
"SA107", "SA108", "SA109", "SA110", "SA111", "SA112", "SA113",
"SA114", "SA115", "SA116", "SA118", "SC01", "SC02", "SC03", "SC04",
"SC05", "SC06", "SC07", "SC08", "SC09", "SC10", "SC11", "SC12",
"SC13", "SC14", "SC15", "SC16", "SC17", "SC18", "SC19", "SC20",
"SC21", "SC22", "SC23", "SC24", "SC25", "SC26", "SC27", "SC28",
"SC29", "SC30", "SC31", "SC32", "SC33", "SC34", "SC35", "SC36",
"SC37", "SC38", "M01", "M02", "M03", "M04", "M05", "M06", "M07",
"M08", "M09", "M10", "M11", "M12", "M13", "M14", "M15", "M16",
"M17", "M18", "M19", "M20", "M21", "M22", "M23", "M24", "M25",
"M26", "M27", "M28", "M29", "M30", "M31", "M32", "M33", "M34",
"M35", "M36", "M37", "M38", "M39", "M40", "M41", "M42", "M43",
"M44", "M45", "M46", "M47", "M48", "M49", "M50", "M51", "M52",
"M53"), label = "Code of PrevenGo", format.spss = "A5", display_width = 12L),
group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1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2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), insuline = structure(c(9, 4.1, 3.3, 9.4, 22.9, 16.2,
8.7, 16.7, 21.2, 21, 12.8, 7.3, 38.4, 20.2, 19.6, 6.4, 18.9,
12.1, 8.2, 17, 15.6, 12.5, 19.1, 13.7, 8, 20.1, 19.8, 6.8,
15.4, 14.7, 11.9, 8.8, 7.9, 51.2, 10.8, 8.1, 28.6, 8.6, 27.9,
13.3, 9, 16.3, 13.3, 5.8, 27.3, 4.2, 8.2, 9.9, 20.1, 11.7,
8.7, 18.1, 10.9, 27.4, 14.6, 29.1, 10.2, 20.2, 9.7, 12.3,
18.2, 1.9, 11.6, 14.6, 7.9, 11.2, 13.8, 21.2, 23.8, 18, 23.5,
21.4, 11.4, 12, 6.6, 13.5, 10.4, 25.3, 56.8, 10.7, 21.5,
8.5, 30.2, 5.3, 7.5, 15.9, 11.6, 22.4, 25.2, 6.1, 15.1, 9.3,
24.3, 30.8, 8.9, 9.8, 34.1, 13.4, 23.1, 21.1, 4.8, 20.1,
38.5, 16.1, 34.1, 16.1, 17.7, 41.4, 20.4, 21.5, 36.3, 15.9,
8.8, 6.1, 29, 4, 23.1, 36.8, 16.4, 15.5, 28.8, 15.9, NA,
7.1, 6.1, 10, 9.1, 25.2, 19.1, 6.9, 14.7, 23.1, 19.3, 12.3,
7.3, 5.9, 8, 0.5, 9, 4, 10.4, 21.4, 14.6, 8.8, 24.5, 5.3,
9.8, 17.6, 10.2, 10.7, 23, 14.5, 4.6, 33.3, 23.3, 7.2, 3.7,
13.1, 6.7, 20, 7.5, 9.2, 4.5, 2.1, 7.7, 11.7, 7.6, 22.5,
8.8, 5.1, 14.8, 15.1, 18.8, 24.3, 14, 17.2, 16.2, 23.6, 17.4,
16.5, 12.1, 15.3, 11.4, 8.7, 22.6, 10.5, 7.4, 15.1, 13.1,
24.6, 19.3, 19.7, 14.1, 5.9, 19.7, 14.9, 5.9, 17.2, 16.9,
6.2, 11.2, 4.1, 10, 3.7, 3.6, 11.6, 16.9, NA, 8, 17.3, NA,
18.3, 4, 3.1, 26.4, 12.9, 17.9, 10.3, 22.5, NA, NA, 23.4,
15.1, NA, 11.9, 27, 6.2, NA, 21.5, 11.6, 15.8, 8.6, 15.2,
10.1, 20.6, 21.7, 45.3, 8.3, 19.5, 29.2, 21.5, 11.4, 9.5,
31.8, 35.3, 11.2, 15.4, NA, 8.5, 22.6, 14.3, NA, 11.8, 11.4,
4.2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 35.8, NA, NA,
NA, NA, NA, 19.7, 42.8, 30.6, 12.2, 5.2, 4.9, 20.4, NA, 23.5,
NA, 13.6, 19.4, 6.9, 16.7, 7.2, 14.7, 59.2, 22, 41.4, 18.1,
10.5, 19.8, 17.4, NA, 25.9, NA, 8.3, 25.9, 5.7, 17.1, 25.2,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 10.3, 9.1, 14.8,
13.7, 6.2, 17.9, 13.9, 14.6, 70.4, 23.6, 13.8, 15.2, 9.9,
14, 27.6, 14.3, 23.7, 11, 12.1, 13.5, 21, NA, 7.2, 12.3,
4.4, 6.2, 3.9, 15, 9.6, NA, 9, 10.3, NA, 13.3, 6, 11.3, 17.6,
8.5, 10, NA, 11.8, 10.4, 26.2, NA, 10, 5.7, 16.3, 4.7, 20.3,
7.7, 14.6, 9.4, 6.3, 10, 11.1, 6.7, 42.5, NA, NA, NA, 7.7,
18.6, NA, 16.7, 25.4, 21.8, 26.8, 10.2, 13.8, 11.6, 19.1,
8.3, 3.8, 31.1, NA, 7.1, 11.1, 8.7, 19, 16, 31.8, 11.7, 3.4,
17.6, 12.3, 5.1, 17.5, 6.7, 3.8, 16.6, 6.1), format.spss = "F4.2", display_width = 11L),
glucose = structure(c(90, 95, 79, 85, 95, 97, 86, 74, 88,
95, 94, 88, 86, 94, 86, 95, 97, 88, 88, 88, 83, 103, 79,
67, 88, 79, 90, 79, 97, 94, 85, 83, 88, 97, 81, 95, 92, 94,
99, 79, 83, 92, 81, 92, 79, 94, 83, 79, 81, 92, 86, 95, 92,
95, 92, 85, 94, 81, 86, 85, 99, 92, 85, 72, 86, 81, 79, 86,
97, 88, 92, 97, 83, 103, 97, 95, 85, 77, 77, 83, 99, 90,
77, 77, 83, 92, 88, 83, 88, 86, 88, 97, 101, 99, 88, 101,
94, 86, 85, 83, 86, 88, 92, 94, 94, 90, 160, 94, 83, 95,
97, 88, 88, 95, 90, 92, 113, 104, 85, 101, 91.8, 99, 94,
85, 85, 83, 86, 88, 95, 79, 101, 92, 83, 90, 85, 95, 88,
79, 90, 79, 94, 99, 83, 85, 85, 77, 99, 81, 92, 86.4, 95.4,
82.8, 73.8, 81, 90, 82.8, 79.2, 90, 82.8, 91.8, 90, 84.6,
84.6, 84.6, 77.4, 77.4, 75.6, 88.2, 79.2, 92, 90, 113, 81,
81, 81, 84.6, 88.2, 73.8, 81, 81, 82.8, 79.2, 70.2, 91.8,
97.2, 82.8, 70.2, 91.8, 93.6, 86.4, 93.6, 73.8, 95.4, 81,
97.2, 77.4, 90, 82.8, 86.4, 88.2, 88.2, 73.8, 90, 92, 83,
86, 99, NA, 86, 81, NA, 99, 83, 86, 76, 90, 85, 90, 92, NA,
NA, 79, 79, NA, 86, 81, 88, NA, 90, 86, 92, 85, 92, 83, 92,
90, 92, 95, 94, 88, 90, 86, 88, 101, 95, 92, 81, NA, 92,
90, 81, NA, 90, 81, 88, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 85, NA, NA, NA, NA, NA, 85, 88, 86, 88, 106, 101, 88,
NA, 79, NA, 85, 99, 92, 79, 88, 88, 95, 81, 86, 77, 81, 92,
97, NA, 86, NA, 88, 94, 81, 86, 85, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 85, 88, 95, 83, 92, 112, 94, 95, 108,
97, 90, 88, 86, 97, 95, 88, 90, 88, 77, 94, 81, NA, 79, 83,
95, 88, 81, 92, 92, NA, 88, 86, NA, 85, 85, 97, 81, 88, 90,
NA, 77.4, 94, 83, NA, 95, 85, 92, 83, 95, 88, 94, 94, 88,
77, 90, 86, 92, NA, NA, NA, 95, 92, NA, 90, 103, 90, 85,
92, 83, 81, 94, 81, 79, 94, NA, 92, 99, 95, 84, 95, 72, 90,
79, 97.5, 85, 88, 79, 81, 72, 85, 88), format.spss = "F4.2", display_width = 11L),
hdl = structure(c(54, 55, 48, 38, 46, 50, 45, 38, 50, 43,
39, 32, 35, 34, 40, 48, 53, 33, 42, 34, 41, 48, 51, 38, 53,
38, 37, 44, 37, 33, 54, 47, 51, 39, 44, 54, 32, 53, 39, 36,
58, 41, 34, 43, 40, 49, 49, 50, 37, 36, 54, 47, 35, 40, 50,
44, 40, 43, 45, 41, 34, 50, 46, 46, 50, 53, 53, 45, 37, 70,
51, 55, 51, 58, 58, 49, 44, 37, 32, 64, 41, 63, 46, 55, 46,
65, 43, 55, 42, 56, 39, 50, 38, 46, 45, 53, 53, 39, 45, 47,
48, 32, 45, 45, 36, 60, 30, 43, 43, 57, 36, 56, 45, 40, 40,
61, 50, 29, 55, 38, 35, 47, 42, 50, 46, 26, 60, 33, 36, 34,
44, 59, 45, 44, 55, 45, 53, 38, 50, 40, 57, 46, 48, 45, 43,
49, 53, 39, 46, 39, 36, 39, 36, 42, 40, 50, 63, 46, 45, 39,
43, 30, 57, 46, 40, 39, 39, 53, 40, 54, 56, 40, 37, 48, 43,
29, 46, 45, 82, 31, 34, 37, 41, 63, 34, 50, 37, 51, 36, 42,
41, 34, 55, 40, 42, 60, 36, 38, 52, 57, 48, 48, 46, 47, 50,
41, 48, NA, 40, 45, NA, 43, 58, 42, 48, 44, 46, 47, 55, NA,
NA, 38, 52, NA, 53, 31, 51, NA, 32, 51, 41, 38, 57, 36, 50,
41, 60, 65, 39, 52, 36, 36, 49, 43, 34, 44, 41, NA, 50, 52,
37, NA, 58, 45, 34, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
46, NA, NA, NA, NA, NA, 59, 55, 50, 46, 58, 58, 42, NA, 31,
NA, 48, 43, 66, 55, 51, 41, 50, 38, 46, 41, 43, 38, 48, NA,
46, NA, 56, 44, 46, 48, 49, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 63, 41, 39, 46, 58, 53, 33, 53, 48, 33, 44, 46,
49, 48, 44, 55, 44, 39, 32, 46, 50, NA, 47, 53, 39, 51, 61,
48, 32, NA, 42, 46, NA, 49, 48, 52, 39, 40, 38, NA, 31, 46,
48, NA, 51, 58, 43, 49, 43, 65, 41, 61, 49, 35, 37, 36, 58,
NA, NA, NA, 38, 45, NA, 58, 31, 49, 52, 65, 32, 45, 39, 37,
41, 34, NA, 42, 51, 39, 48, 36, 35, 55, 38, 48, 53, 41, 39,
49, 63, 41, 47), label = "HDL-Cholesterol", format.spss = "F3.2", display_width = 11L),
ldl = structure(c(100, 104, 171, 153, 107, 152, 87, 101,
70, 137, 96, 95, 98, 94, 92, 102, 63, 104, 62, 75, 125, 117,
114, 132, 112, 146, 121, 91, 113, 120, 96, 96, 95, 87, 96,
134, 98, 92, 88, 101, 133, 113, 77, 128, 97, 169, 136, 96,
74, 59, 121, 66, 109, 103, 116, 86, 87, 124, 88, 94, 77,
98, 90, 133, 79, 78, 98, 129, 62, 62, 96, 72, 85, 98, 101,
132, 69, 196, 76, 125, 105, 108, 89, 108, 123, 51, 92, 50,
121, 105, 80, 103, 59, 96, 89, 65, 77, 90, 92, 65, 123, 96,
80, 128, 92, 124, 96, 83, 120, 145, 114, 134, 116, 65, 91,
103, 84, 123, 99, 96, 61, 82, 85, 116, 116, 113, 121, 69,
82, 100, 108, 99, 144, 152, 158, 128, 112, 89, 119, 61, 99,
147, 109, 121, 92, 115, 95, 62, 72, 130, 96, 76, 117, 96,
108, 131, 120, 67, 99, 105, 63, 63, 103, 128, 92, 120, 146,
106, 103, 94, 85, 122, 111, 102, 143, 74, 87, 80, 67, 140,
85, 87, 101, 94, 122, 124, 82, 150, 92, 84, 119, 98, 89,
97, 117, 122, 111, 86, 90, 110, 107, 150, 103, 94, 149, 159,
91, NA, 109, 126, NA, 167, 77, 90, 103, 80, 68, 75, 55, NA,
NA, 74, 113, NA, 102, 116, 84, NA, 66, 85, 114, 111, 101,
95, 92, 86, 96, 90, 92, 77, 91, 108, 86, 118, 85, 127, 99,
NA, 160, 80, 63, NA, 123, 86, 94, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 106, NA, NA, NA, NA, NA, 70, 85, 70, 96,
102, 117, 101, NA, 146, NA, 94, 122, 122, 94, 110, 121, 39,
72, 48, 109, 110, 60, 95, NA, 83, NA, 79, 87, 113, 103, 55,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 157, 103, 56,
92, 114, 78, 97, 106, 117, 61, 72, 83, 91, 122, 106, 103,
89, 51, 89, 153, 90, NA, 132, 132, 110, 84, 84, 96, 72, NA,
104, 122, NA, 80, 113, 106, 62, 72, 121, NA, 102, 125, 130,
NA, 111, 119, 66, 109, 119, 91, 92, 120, 160, 93, 117, 126,
88, NA, NA, NA, 115, 100, NA, 200, 79, 95, 99, 89, 123, 108,
82, 108, 81, 103, NA, 103, 149, 116, 115, 122, 95, 106, 89,
128, 118, 123, 51, 90, 130, 119, 120), label = "LDL-Cholesterol", format.spss = "F4.2", display_width = 11L)), row.names = c(NA,
-404L), class = c("tbl_df", "tbl", "data.frame"), reshapeLong = list(
varying = list(c("age_1", "age_2"), c("whz_1", "whz_2"),
c("haz_1", "haz_2"), c("waz_1", "waz_2"), c("zbmi_1",
"zbmi_2"), c("wc_1", "wc_2"), c("abc_1", "abc_2"), c("PA_1",
"PA_2"), c("PAextra_1", "PAextra_2"), c("TVweekdays_1",
"TVweekdays_2"), c("TVweekend_1", "TVweekend_2"), c("kidmed_1",
"kidmed_2"), c("totalcholesterol_1", "totalcholesterol_2"
), c("ldl_1", "ldl_2"), c("hdl_1", "hdl_2"), c("triglycerides_1",
"triglycerides_2"), c("glucose_1", "glucose_2"), c("insuline_1",
"insuline_2"), c("hba1c_1", "hba1c_2"), c("homair_1",
"homair_2"), c("fatmass_1", "fatmass_2"), c("energykcal_1",
"energykcal_2"), c("protein_1", "protein_2"), c("proteinpc_1",
"proteinpc_2"), c("carbohydrates_1", "carbohydrates_2"
), c("carbohydratespc_1", "carbohydratespc_2"), c("sugar_1",
"sugar_2"), c("sugarpc_1", "sugarpc_2"), c("starch_1",
"starch_2"), c("fruitportions_1", "fruitportions_2"),
c("vegetablesportions_1", "vegetablesportions_2"), c("vegetalfiber_1",
"vegetalfiber_2"), c("solublefiber_1", "solublefiber_2"
), c("insolublefiber_1", "insolublefiber_2"), c("lipids_1",
"lipids_2"), c("lipidspc_1", "lipidspc_2"), c("sfa_1",
"sfa_2"), c("sfapc_1", "sfapc_2"), c("mufa_1", "mufa_2"
), c("mufapc_1", "mufapc_2"), c("pufa_1", "pufa_2"),
c("pufapc_1", "pufapc_2"), c("cholesterolintake_1", "cholesterolintake_2"
)), v.names = c("age", "whz", "haz", "waz", "zbmi", "wc",
"abc", "PA", "PAextra", "TVweekdays", "TVweekend", "kidmed",
"totalcholesterol", "ldl", "hdl", "triglycerides", "glucose",
"insuline", "hba1c", "homair", "fatmass", "energykcal", "protein",
"proteinpc", "carbohydrates", "carbohydratespc", "sugar",
"sugarpc", "starch", "fruitportions", "vegetablesportions",
"vegetalfiber", "solublefiber", "insolublefiber", "lipids",
" lipidspc", "sfa", "sfapc", "mufa", "mufapc", "pufa", "pufapc",
"cholesterolintake"), idvar = c("id", "group"), timevar = "time"))
Instead of making your varlist a list of vectors you could simply pass a vector with names of the colums you want to plot. Then use aes_string(..., y = varlist) inside your function and you will automatically get the name of the variable as the y axis title:
# Multiple box plot per group per time
library(ggplot2)
library(dplyr)
# Create a list wherein the function will be applied to
varlist <- c("insuline", "glucose", "hdl", "ldl")
names(varlist) <- varlist
# Create the function boxplot
A <- function(varlist) {
dflinear %>%
group_by("group") %>%
ggplot(mapping = aes_string(x = "time", y = varlist, fill = "group")) +
geom_boxplot()
}
# Apply it to the whole list and graph the plots
plots <- lapply(varlist, FUN = A)
plots[[1]]
Is it possible to randomly sample patients by group so that they have similar distributions based on other variables? To me, this sounds like a matching problem, but there's no "treatment" here, so I'm not sure if the concept applies.
Sample data:
structure(list(id = c(8350L, 22543L, 24144L, 9392L, 27648L, 2943L,
34686L, 27153L, 11143L, 15209L, 11952L, 22669L, 8211L, 27765L,
28671L, 9693L, 30274L, 25807L, 14839L, 22400L, 24494L, 6540L,
6861L, 31825L, 34190L, 19606L, 21077L, 5037L, 25943L, 20530L,
23730L, 34774L, 7210L, 2051L, 28410L, 18318L, 34848L, 26596L,
8973L, 24885L, 9652L, 8387L, 16168L, 36893L, 24048L, 17769L,
1273L, 22734L, 36796L, 25497L, 28300L, 166L, 21172L, 20026L,
16265L, 1699L, 33140L, 23997L, 10216L, 27408L, 6813L, 10196L,
15015L, 2748L, 34979L, 21763L, 27438L, 6255L, 17047L, 30593L,
30723L, 7914L, 218L, 20134L, 29952L, 27126L, 3795L, 1367L, 33585L,
5940L, 26250L, 22519L, 35611L, 26168L, 26848L, 21276L, 8971L,
22554L, 16655L, 5315L, 18121L, 32526L, 21513L, 9262L, 36882L,
7408L, 18873L, 17238L, 15216L, 23667L, 30138L, 2978L, 25451L,
2492L, 30983L, 7677L, 22880L, 29674L, 7093L, 24910L, 20839L,
18176L, 23031L, 17197L, 4613L, 35801L, 30822L, 3889L, 11752L,
11314L, 22317L, 12825L, 17433L, 4407L, 3986L, 10173L, 32409L,
2697L, 3410L, 26834L, 3203L, 5474L, 34678L, 35336L, 19462L, 15835L,
7888L, 27897L, 9245L, 16524L, 13316L, 21604L, 30458L, 9191L,
1220L, 1779L, 1724L, 26382L, 11566L, 21310L, 12600L, 25063L,
30912L, 31189L, 9480L, 16804L, 2372L, 26238L, 20113L, 33753L,
32711L, 11543L, 10578L, 4475L, 13187L, 23395L, 35342L, 6903L,
26905L, 12026L, 5697L, 15352L, 33985L, 1132L, 15806L, 13611L,
29930L, 15896L, 6057L, 10849L, 12944L, 25561L, 3328L, 27481L,
28790L, 3260L, 24986L, 22177L, 26580L, 11639L, 2256L, 4839L,
22805L, 616L, 6702L, 18360L, 4439L, 1300L, 33779L, 24940L, 10043L,
21268L, 35127L, 36621L, 17618L, 6688L, 15937L, 31057L, 2144L,
30866L, 12500L, 29753L, 36497L, 21247L, 9481L, 36465L, 20665L,
15017L, 21234L, 34258L, 576L, 31187L, 4528L, 15314L, 3657L, 24489L,
33871L, 106L, 24916L, 2524L, 17469L, 2799L, 13311L, 26585L, 7131L,
21401L, 6191L, 22338L, 11647L, 11681L, 22744L, 14000L, 5356L,
2892L, 24481L, 24116L, 21461L, 13992L, 22751L, 11129L, 8802L,
29963L, 4660L, 29020L, 20843L, 21796L, 3607L, 10692L, 29168L,
25034L, 3307L, 35010L, 20280L, 31894L, 7276L, 24259L, 34059L,
35867L, 11165L, 16010L, 34082L, 26586L, 30958L, 25030L, 34851L,
29185L, 25721L, 8968L, 29427L, 20213L, 34667L, 28721L, 21472L,
17132L, 35247L, 9798L, 36826L, 21226L, 28335L, 16077L, 2654L,
20466L, 21324L, 36969L, 22553L, 5895L, 16514L, 10644L, 4376L,
13592L, 11206L, 32440L, 13413L, 31416L, 22540L, 15986L, 11506L,
16928L, 18652L, 17858L, 13522L, 8566L, 10665L, 29442L, 28219L,
22549L, 2209L, 8017L, 6066L, 21718L, 21930L, 11540L, 4100L, 35236L,
240L, 24900L, 425L, 26880L, 21409L, 18885L, 5803L, 33335L, 25597L,
12547L, 8930L, 4328L, 17360L, 4696L, 25198L, 26469L, 14679L,
1691L, 32989L, 6099L, 14427L, 31797L, 23408L, 29296L, 23928L,
31889L, 31737L, 6420L, 11304L, 34798L, 20785L, 9806L, 35018L,
35008L, 1450L, 3246L, 15123L, 19603L, 8519L, 32012L, 3397L, 11682L,
27102L, 18022L, 20408L, 15836L, 18284L, 12897L, 29580L, 14510L,
23925L, 28821L, 35825L, 14922L, 36643L, 10948L, 4220L, 23791L,
65L, 35772L, 1423L, 29386L, 755L, 23627L, 27201L, 12353L, 3578L,
1914L, 35373L, 16702L, 13057L, 3021L, 27531L, 1990L, 205L, 21559L,
29081L, 26301L, 18894L, 3088L, 9782L, 10522L, 12570L, 8948L,
36240L, 33943L, 33022L, 2750L, 32649L, 30134L, 13920L, 11498L,
8314L, 16849L, 15559L, 22529L, 31406L, 5680L, 17908L, 14931L,
2122L, 2581L, 33546L, 12143L, 17220L, 16713L, 7454L, 13659L,
15973L, 20116L, 27689L, 35285L, 36106L, 21834L, 29850L, 29030L,
7957L, 31698L, 12307L, 23642L, 5615L, 12016L, 1161L, 15291L,
32738L, 1089L, 32988L, 33382L, 3642L, 18661L, 35584L, 8009L,
24000L, 30587L, 25870L, 19944L, 34970L, 29983L, 24774L, 28702L,
21199L, 17292L, 29831L, 476L, 18881L, 29923L, 31476L, 4570L,
31081L, 10544L, 3373L, 13435L, 22651L, 17861L, 3818L, 35387L,
11459L, 35637L, 308L, 35697L, 12696L, 15175L, 7990L, 16691L,
19494L, 9008L, 30695L, 28889L, 446L, 22178L, 13000L, 26166L,
15431L, 19332L, 35991L, 2840L), race_f = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 4L, 2L, 3L, 4L, 1L, 1L, 3L, 3L, 3L, 3L, 1L,
3L, 1L, 3L, 3L, 1L, 1L, 3L, 2L, 2L, 1L, 4L, 5L, 1L, 4L, 1L, 1L,
5L, 1L, 1L, 3L, 2L, 3L, 3L, 1L, 1L, 1L, 2L, 1L, 3L, 2L, 1L, 1L,
2L, 1L, 3L, 1L, 2L, 1L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 3L, 3L, 3L,
1L, 1L, 1L, 3L, 3L, 2L, 1L, 1L, 3L, 4L, 4L, 1L, 1L, 3L, 1L, 2L,
3L, 4L, 1L, 1L, 1L, 3L, 1L, 1L, 5L, 3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L, 1L, 3L, 1L, 5L, 3L, 4L,
1L, 4L, 4L, 1L, 3L, 4L, 1L, 4L, 1L, 1L, 1L, 3L, 2L, 1L, 2L, 4L,
1L, 1L, 5L, 4L, 1L, 1L, 4L, 3L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 2L,
1L, 3L, 3L, 3L, 1L, 2L, 3L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 3L, 1L,
1L, 3L, 4L, 1L, 1L, 2L, 5L, 3L, 3L, 1L, 1L, 4L, 1L, 4L, 1L, 4L,
2L, 3L, 3L, 1L, 1L, 1L, 4L, 1L, 4L, 3L, 1L, 1L, 1L, 1L, 3L, 1L,
3L, 1L, 1L, 1L, 1L, 4L, 3L, 4L, 3L, 3L, 3L, 2L, 3L, 1L, 1L, 1L,
3L, 5L, 2L, 5L, 1L, 1L, 4L, 3L, 1L, 3L, 2L, 1L, 1L, 5L, 1L, 3L,
3L, 4L, 1L, 1L, 1L, 2L, 5L, 1L, 1L, 4L, 3L, 1L, 1L, 1L, 2L, 1L,
1L, 3L, 3L, 1L, 1L, 4L, 3L, 2L, 4L, 4L, 1L, 1L, 2L, 3L, 1L, 3L,
3L, 1L), .Label = c("White", "Black", "Hispanic", "Asian", "Other"
), class = "factor"), cops2_avg_12mo = c(82.9166666666667, 66,
23.3333333333333, 28, 9.33333333333333, 69.9166666666667, 6,
33.3333333333333, 0, 12, 102, NA, 66, 6, 45, 58.5, 10, 55.9166666666667,
19.5, 6, 10, 234.666666666667, 28, 23, 51.5833333333333, 10,
38, 123.5, 0, 24, 10, 0, 73, 10, 25, 6, 20, 13.4166666666667,
13.8333333333333, 8, 14.8333333333333, 53.5, 42, NA, 57.1666666666667,
0, 24.6666666666667, 10, NA, 54.6666666666667, 38.75, 41, 22,
0.833333333333333, 13, 113.083333333333, 27.3333333333333, 9,
33.1666666666667, 18.75, 57.75, 30, 60.3333333333333, 23.1666666666667,
37, 16.5, 0, 145.5, 45, 31.3333333333333, 0, 10, 187.5, 27.4166666666667,
10, 54.9166666666667, 78.8333333333333, 103.75, 6.66666666666667,
30.4166666666667, 10, 10, 24.6666666666667, 10, 118.333333333333,
61.25, 17, 10, 28, 51, 6, 32.0833333333333, 80.75, 8.83333333333333,
NA, 10, 74.25, 42.25, 47, 60, 41.6666666666667, 19.0833333333333,
98.5, 73.5, 10, 6.66666666666667, 49.8333333333333, 10, 79.8333333333333,
10, 42, 95.8333333333333, 130.583333333333, 5.41666666666667,
47.25, 6, 8, 17.8333333333333, 10, 73.9166666666667, 10, 8, 27.8333333333333,
125.916666666667, 134.166666666667, 88, 10, 58, 62.5, 10.3333333333333,
28.8333333333333, 100.083333333333, 35.5, 0, 0, 10, 105, 7.33333333333333,
35, 9.66666666666667, 10, 4.16666666666667, 10, 8.33333333333333,
70.6666666666667, 28.4166666666667, 38.1666666666667, 8, 101.5,
26.75, 61.1666666666667, 14, 95.5833333333333, 35, 65, 0, 51.75,
57.5, 10, 13.6666666666667, 10, 67.5, 10, 62.3333333333333, 72.6666666666667,
10, 45.5, 20.8333333333333, 31, 84.5, 10, 98.1666666666667, 47.5,
56, 126, 14, 10, 10, 8, 36, 111.5, 54.5, 45.5, 8, 37.5, 84.8333333333333,
39.1666666666667, 56.25, 37.9166666666667, 37.75, 27, 55.6666666666667,
10, 34, 5.83333333333333, 37, 80.0833333333333, 57, 102.166666666667,
12.6666666666667, 10, 19.3333333333333, 10, NA, 51, 25.9166666666667,
14, 36.9090909090909, 38.6666666666667, 0, 6.33333333333333,
NA, 31, 43, 26.5, 10, 34.4166666666667, 77.1666666666667, 10,
10, 89.9166666666667, 59, 37, 77.3333333333333, 64, 52, 19.6666666666667,
66.5, 24, 106.083333333333, 29.6666666666667, 38.1666666666667,
6.66666666666667, 10, 16.75, NA, 86.75, 1, 14, 20.3333333333333,
8, 21, 38.9166666666667, 50.8333333333333, 57.5, 29, 0, 26.5,
51.9166666666667, 71.25, 42.6666666666667, 82, 58.0833333333333,
11.3333333333333, 82, 9.5, 78.6666666666667, 102.5, 71, 10, 70.6666666666667,
NA, 33.8333333333333, 61.25, 87, 36.5, 10, 40.4166666666667,
51.8333333333333, 23, 9.66666666666667, 44.5, 8, 10, 4.16666666666667,
0, 48.8333333333333, 49.25, 15, 70, 10, 6, 10, 34.8333333333333,
108.75, 36, NA, 31, 51, 69.5, 122.5, 48, 43.5833333333333, NA,
10, 20, 80.75, 54.75, 106.916666666667, 53.5, 90.6666666666667,
8.33333333333333, 85.5, 40.5833333333333, 5.5, 10, 61.3333333333333,
69.8333333333333, 10, 51, 0, 49.0833333333333, 13.6666666666667,
13.3333333333333, 5.83333333333333, 33.8333333333333, 14.4166666666667,
11.25, 14, 6, 14.5833333333333, 36, 21, 10, 29.5833333333333,
13, 34, 10, 2.5, 10, 211.916666666667, 19.75, 7.33333333333333,
6, 59.6666666666667, 30.25, 34.25, 16.1666666666667, 10, NA,
NA, 97, 75, 26.5, 8, 32.25, 0, 39, 37, 165.333333333333, 45,
33.1666666666667, 21, 10, 57, 70.3333333333333, 10, 10, 62, 79.1666666666667,
38, 26.1666666666667, 13, 8, 69.6666666666667, 40.5, 100, 0.833333333333333,
8, 82.5, 10, 19.8333333333333, 20.0833333333333, 8, 25.8333333333333,
16.75, 10, 36, NA, 12.8333333333333, 31.4166666666667, 10, 61.4166666666667,
14, 67.5, 3, 83.1666666666667, 48, 43.75, 35.4166666666667, 73,
44.1666666666667, 8, 29.75, 10, 10, 62.6666666666667, 26.9166666666667,
29.6666666666667, 10, NA, 15, 19.4166666666667, 112, 29, 3, 33.5,
62.5, 10, 84.6666666666667, 8, 84.4166666666667, 81.5, 56.1666666666667,
10, 101.416666666667, 16, 10, 19.6666666666667, 60, 73.6666666666667,
74.9166666666667, 21, 5, 15.0833333333333, 17.0833333333333,
17.5, 46, 61.8333333333333, 115.333333333333, 92, 30, 0, 22.75,
16.6666666666667, 15, 15, 10, NA, 56.25, 54, 10, 40, 9.83333333333333,
10.9166666666667, 22.25, 84.75, 80, 1.66666666666667, 99.8333333333333,
10, 38.6666666666667, 169.75, 35.0833333333333, 8, 78.5, 6.33333333333333,
21, 10, 42, 105.166666666667, 162.416666666667, 14, 69.25, 35.8333333333333,
13, 5.83333333333333, 34, 51, 12.75, 44.3333333333333, 39.5,
10, 23, 46.8333333333333, 89.9166666666667, 15, 28, 128.416666666667,
10, 91.6666666666667, 3.5, 54, 23, NA, 29.75, 37.1666666666667,
12.6666666666667, 31.9166666666667, 23, 0, 11, 67.9166666666667,
3.16666666666667, 8.33333333333333, 51, NA, 10, 0, 58.8333333333333
), AGE = c(86, 82, 83, 92, 45, 81, 52, 64, 71, 96, 79, 64, 76,
37, 81, 79, 72, 79, 74, 46, 45, 71, 89, 76, 53, 48, 52, 77, 63,
52, 57, 62, 84, 88, 55, 69, 67, 63, 67, 51, 86, 53, 65, 59, 71,
60, 70, 20, 78, 62, 58, 73, 68, 71, 66, 72, 71, 65, 95, 67, 79,
70, 86, 77, 81, 54, 44, 66, 80, 71, 30, 77, 67, 75, 48, 65, 83,
85, 70, 70, 74, 58, 81, 28, 78, 66, 79, 47, 74, 41, 74, 58, 73,
55, 53, 56, 84, 74, 62, 85, 68, 47, 78, 72, 57, 56, 64, 55, 86,
76, 77, 58, 74, 55, 71, 61, 74, 62, 65, 75, 81, 68, 39, 58, 65,
76, 27, 79, 86, 61, 87, 52, 72, 58, 53, 69, 78, 65, 81, 69, 66,
68, 61, 72, 74, 80, 88, 46, 53, 77, 89, 83, 41, 67, 83, 62, 90,
70, 60, 62, 33, 78, 80, 62, 81, 37, 55, 90, 81, 73, 67, 97, 32,
71, 70, 69, 46, 57, 60, 79, 79, 56, 75, 60, 52, 78, 61, 51, 70,
67, 71, 36, 53, 70, 53, 74, 89, 78, 70, 56, 58, 83, 50, 77, 70,
50, 75, 53, 86, 65, 45, 63, 62, 78, 65, 69, 75, 79, 71, 56, 88,
63, 72, 85, 68, 72, 45, 81, 46, 70, 84, 71, 82, 63, 57, 77, 70,
42, 87, 84, 61, 64, 79, 53, 65, 64, 69, 68, 71, 89, 49, 70, 82,
63, 79, 65, 64, 54, 73, 36, 80, 38, 68, 62, 84, 80, 65, 73, 91,
59, 35, 80, 67, 68, 65, 47, 60, 67, 72, 81, 22, 35, 58, 57, 68,
94, 38, 77, 75, 73, 78, 71, 78, 53, 58, 61, 77, 44, 95, 53, 72,
68, 72, 73, 78, 41, 75, 80, 60, 53, 68, 79, 80, 74, 25, 79, 55,
68, 85, 64, 72, 78, 78, 71, 73, 82, 73, 73, 58, 69, 58, 72, 78,
56, 74, 67, 66, 72, 38, 58, 62, 77, 81, 37, 46, 88, 55, 76, 50,
57, 72, 39, 56, 29, 76, 77, 36, 31, 70, 70, 70, 54, 74, 47, 81,
46, 81, 55, 53, 70, 28, 71, 79, 68, 78, 81, 30, 83, 43, 70, 79,
47, 94, 60, 64, 82, 81, 92, 57, 90, 86, 58, 61, 69, 50, 64, 79,
56, 76, 52, 55, 53, 85, 89, 64, 86, 58, 82, 64, 74, 45, 64, 71,
75, 61, 79, 82, 63, 81, 60, 70, 79, 63, 59, 80, 53, 80, 41, 83,
67, 90, 60, 82, 74, 75, 52, 62, 35, 53, 49, 71, 69, 73, 67, 44,
77, 81, 96, 52, 75, 30, 83, 74, 56, 62, 78, 63, 63, 62, 71, 62,
89, 83, 77, 66, 64, 24, 96, 63, 51, 65, 71, 50, 68, 83, 82, 90,
91, 84, 90, 76, 62, 79, 20, 75, 79, 80, 62, 62, 71, 51, 81, 84,
65, 65, 55, 65, 51, 26, 70)), row.names = c(NA, -500L), class = c("tbl_df",
"tbl", "data.frame"))
I'm hoping to sample by race_f so that the different race groups are similar in AGE and cops2_avg_12mo. Is this at all possible? Thank you!
The answer depends on if you want to ensure that their ages/cops2_avg_12mo will always be within a specific range - in which case you would simply create a subset of your data frame with only the patients whose age and cops2_avg_12mo are within some range. I do think that this is the safer thing to do in terms of quality control. You can view a plot of the two columns of your data (AGE and cops2_avg_12mo) to get an idea of what ranges of values most of the patients fall into:
plot(x[,c("AGE", "cops2_avg_12mo")])
Pick ranges for these values that contain enough patients to sample from. (I don't know how many samples you need). Basically, draw a box in the dot plot which contains enough patients to sample from.
So once you determine the ranges/boundaries of the box, just create indexes like so:
idx = (x[,"AGE"] > 50) & (x[,"AGE"] < 75) & (x[,"cops2_avg_12mo"] > 0) & (x[,"cops2_avg_12mo"] < 75) & !is.na(x[,"cops2_avg_12mo"])
then get the subset of your data:
subsetX = x[idx,]
After you create that subset, you can randomly sample using R's sample() function. If you want to do sampling from each race equally, then call sample() with the subsetX data, with each race selected at a time, to get n samples at a time:
sample(subsetX[subsetX[,"race_f"]=="Asian",], n, replace=FALSE)
Alternatively, if you are ok with sampling patients that have outlier values (but I feel like this will produce more variation in your results), then you can create a histogram of each of the columns - for example, AGE - then get the histogram bin counts, divide them by the total number of patients to get a probability distribution, then create a vector the same length as the number of patients where each value is the probability we calculated for the bin it belongs to (found by getting bin indexes when calculating the histogram), then pass that vector into the sample() function as the prob input argument so that values are sampled with their specified probability.
This is the code I have for the heatmap.
sd1<-melt(Mstressed,id.vars = "Period")
library(plotly)
P1 <- ggplot(data=sd1, aes(x=Period, y=variable, fill=value)) +
geom_tile() +
ggtitle("Stress Portfolio Returns") +
scale_fill_gradientn(colors=colorRampPalette(c("lightgray","royalblue","seagreen","orange","red","brown"))(500),name="Returns") +
labs(x = "Period",y="Size") +
theme_bw()
ggplotly(P1)
Here is sd1 which is already in the melted format:
Period variable value
1 1 Size5 -1.124193e-03
2 2 Size5 2.859438e-05
3 3 Size5 -2.432560e-03
4 4 Size5 -2.544023e-03
5 5 Size5 -1.577432e-03
6 6 Size5 -1.480790e-03
and here it is sd1
> dput(sd1)
structure(list(Period = 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, 201, 202, 203, 204, 205, 206, 207, 208, 209,
210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222,
223, 224, 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, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212,
213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 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, 201, 202, 203,
204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216,
217, 218, 219, 220, 221, 222, 223, 224, 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
), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1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L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Size5", "Size15", "Size25",
"Size50", NA), class = "factor"), value = c(-0.0011241931, 2.85943772727272e-05,
-0.00243256027727273, -0.00254402339069767, -0.00157743184782609,
-0.00148078985416667, -0.00331852698695652, 0.000848599395833333,
-0.00297349628181818, -0.000345186711111111, -0.000501344534782609,
-0.000800074080851064, -0.00221098331428571, 0.00129236565531915,
-0.00131892916086957, -0.00177610596170213, -0.00319288334468085,
0.00113381609787234, -0.00107374819148936, -0.00144620190638298,
0.000141176244444444, -0.000204472952173913, -0.00153988329130435,
3.81790045454546e-05, -0.0011685701173913, 0.000593231404255319,
0.00081186575, 0.000795386733333333, -0.00260122034583333, -0.0021938114875,
0.000553855472340425, -0.0059582546893617, 0.868693839468085,
-0.002801906475, -0.00463708955833333, -0.00272099271111111,
0.00108590003333333, -0.00247600357826087, -0.00372859965957447,
0.000286994587234042, 0.000746639587234043, 0.000276679165957447,
-0.001046501125, 0.0001712814375, -0.0024568735875, 0.000996786336170213,
-0.00182627393191489, -0.000544925195652174, -0.00357635526086957,
-0.00054329072173913, 0.00013407957826087, -0.00103177774347826,
0.000500834638297872, 0.793977075161702, -7.5436685106383e-05,
-0.000485326586956522, -0.000606974591304348, 0.00238437179166667,
0.00100126108888889, -0.000513758953191489, 8.69377181818182e-05,
-0.00123867083829787, 1.3438354628383, -0.00031518505, -0.00191560200454545,
0.805996328804348, -2.61513454545454e-05, -0.000103250163636364,
-0.000778098191489362, -0.0015234903, -0.00114211408636364, -0.00159511182978723,
1.87802078087826, 6.72123086956521e-05, 0.00100891171111111,
-0.000902390629787234, 0.0011009548, 0.000312209177777778, -0.000768029209090909,
-0.000303889769565218, 2.55336222222222e-05, -0.000205546365217391,
0.000216974877272727, 1.47901930388571, -0.000277860133333333,
-0.000214537138095238, -0.00209356162222222, 8.97567761904762e-05,
-0.00559485620909091, 0.000268375711111111, 1.39724291668889,
0.000208154288888889, -0.000634756711111111, 1.22926746946512,
0.00201531319534884, 0.252617265511111, -0.00113930945106383,
0.000837345108695652, 0.00246463208780488, 0.000899249748837209,
0.00274860853333333, 0.00547736369047619, 0.00163468337674419,
-0.000276660977272727, -0.00149508101818182, -0.00221645797777778,
0.00217117751363636, -0.0024888984, -0.000886121655813954, 1.04398107523478,
0.00393339840930233, -0.000922796323809524, -0.00103749403636364,
0.0028039828, 0.00551933685, -0.00123477008888889, -0.005628441,
-0.00121736573043478, -0.000539308567441861, 0.000262361868181818,
0.000343038088888889, -0.0019510897, -0.00288396375454546, 0.359000083955556,
0.000433049466666666, 1.36924029128889, -0.000659901990909091,
0.00304780745454545, 0.000657599762790698, -0.00163171464444444,
-0.000950691240909091, 0.00140542772727273, -0.00107216327272727,
0.000923795111111111, -0.000474348352173913, -0.000865860582608696,
-0.00326246348888889, -0.0006538472, 0.000691390128571429, -9.56189619047619e-05,
-0.00127241684651163, 0.000941450827272727, 0.000531463427906977,
-0.0024702198, 1.36070394653333, -0.00128264022222222, -0.000889438852173913,
-0.000750582844444444, -0.000299201959090909, -0.00191169516363636,
-0.000670931155555555, 2.36634136363636e-05, -0.00117427344186047,
-0.00024379075, -0.000986528844444444, 0.000683873, -0.00243973785909091,
-0.00394431812727273, -0.00293166445, -0.00264133897021277, 0.0029811113627907,
-0.00108674306666667, -0.00145431042727273, 0.00250975572340426,
0.0009900039, -0.00208648798636364, -3.64380465116271e-06, 0.000535875804545454,
0.00197072025217391, -0.00175674031111111, -0.00122569961276596,
1.77897318181817e-05, 0.000638891873913043, -0.000582162311111111,
0.00169907495555556, -0.0017897038826087, 0.657609502855814,
-0.00276549255555556, 0.000570080259574468, 0.00242968832272727,
-0.000416251471428571, 0.000278305236363636, -0.000308473874418605,
0.0015745695875, -0.00119187120476191, 0.00315008771111111, 0.624268614481818,
0.000191677111111111, 0.26118118567234, 0.00184649572608696,
-0.00148222706521739, -0.00224949090232558, -0.00098840725, -0.000267329995744681,
-0.00113342224680851, -0.000558455731914894, 0.000867598088888889,
0.000182637295454545, 0.000666522423255814, -0.000249393468181818,
0.00137624896744186, -0.000641461824390244, -0.000726498320930232,
0.0002189695, 0.000304278144186047, -0.000552308133333333, -8.48839142857143e-05,
2.33358333333333e-05, -0.000115722561904762, 0.91983811283913,
-0.000417791762790698, -0.000322370023255814, -0.000398199302127659,
-9.84947333333333e-05, -0.00031643990952381, -0.000323731279069767,
-0.0006542225, -0.000273647675, 1.15457317073172e-05, -7.34662954545455e-05,
-0.000362220495238095, -0.000537859323809524, -8.60688604651162e-05,
-0.000820713952380952, -0.00153372003333333, -0.000662153756060606,
-0.00275888667727273, -0.00292078759069767, -0.00191772458115942,
-0.000874108720833333, -0.00347106492028986, 0.000208326329166667,
-0.00348595174848485, 0.324998960088889, -0.000291810668115942,
-0.0026285438141844, -0.00318451878095238, 0.00141962778865248,
0.54385947323913, -0.00152278529503546, -0.00409083314468085,
0.195643822364539, -0.000821318458156028, 0.481235884893617,
0.000631022044444445, -0.000154888085507247, -0.00238089515797101,
0.000598294737878788, -0.000853384650724638, 0.00126600480425532,
0.317600938083333, 0.000601763333333333, -0.00106298107916667,
0.316528564245833, 0.000802788139007092, -0.00681549495602837,
0.288553514534752, -0.001983084275, -0.00462916789166667, -0.00399624291111111,
0.00133947723333333, -0.00233989184492754, -0.00326993565957447,
-5.17553460992909e-05, 0.000316770853900709, 0.114606201365957,
0.311149128608333, 0.0994249547041667, -0.00179180045416667,
0.000758236002836879, -0.00229026533191489, -0.000906085528985507,
-0.0034218825942029, 0.411083387011594, 0.407663510978261, -0.000700014143478261,
9.04682382978723e-05, 0.264309987961702, 4.96020482269504e-05,
-0.00112393678695652, -0.00112170092463768, 0.00237529965833333,
-0.000567775777777778, -0.000236103953191489, 0.000608177318181818,
-0.00102851437163121, 0.448221119304964, 0.13397610235, -0.00241395220454545,
0.443765600537681, -0.000226488945454545, 0.000616203103030303,
-0.0013208753248227, -0.000716755566666667, 0.091597368380303,
-0.00170434656312057, 0.626332195011594, 0.000150197242028985,
0.000136383977777778, 0.381103545036879, 0.221540732266667, 0.360871791444444,
-0.000592961009090909, 0.000155833097101449, -0.000866745711111111,
0.000216817234782609, 0.609538703077273, 0.645238149819048, -0.000398335866666667,
-0.000344592138095238, -0.00137507775555556, -0.000256117023809524,
-0.00192587620909091, -6.42416888888889e-05, 1.02013637508889,
-9.28306444444445e-05, -0.00147465644444444, 0.409911313531783,
0.0019891590620155, 0.0851806975111111, -0.00204437158439716,
-0.000579642224637681, 0.000905066487804878, 0.643067721215504,
0.00147966286666667, 0.00508458082380952, 0.000539691310077519,
0.240211235089394, -0.000708393018181818, -0.00109935424444444,
0.0026950974469697, 0.6396645562, -0.00126269592248062, 0.347808651568116,
0.00457931780930233, 0.000302961476190476, -0.0012148691030303,
0.0923351892, 0.00567091231666667, 0.620369322511111, -0.00404364773333333,
-0.00241288513043478, 0.202742444965891, -0.000608278798484849,
0.000356460755555555, -0.00161257603333333, -0.00190840115454545,
0.120414817088889, 0.2228905758, 0.455057063555556, -0.000602834124242424,
0.00261724412121212, 0.452285066562791, -0.00246991411111111,
-0.00152651097424242, 0.000518139793939394, -0.00184454593939394,
0.000185236244444444, -0.00128692128550725, -0.000827895915942029,
-0.00243018582222222, -0.00120132326666667, 0.416215973261905,
0.000253243971428571, -0.00170670997984496, 0.000686898960606061,
-0.00033481963875969, -0.0020584786, 0.4539038956, -0.00121793155555556,
-0.00123285105217391, -0.000559311711111111, 0.000337146574242424,
-0.00228272816363636, -0.00138098148888889, -0.000646004586363636,
-0.0014501903751938, -7.21782833333334e-05, -0.000256129177777778,
0.000223322266666667, -0.00308267385909091, -0.00280884286060606,
-0.00280925431666667, -0.00175704863687943, 0.380177369362791,
-0.0012472598, -0.00215720449393939, 0.00190565352340426, 0.000277635766666667,
-0.00224420778636364, -0.000902296071317829, 0.00110526400454545,
0.00164177331884058, -0.000970322977777778, 0.488807391653901,
-0.000405886401515152, -0.000386372792753623, -0.000187081244444444,
0.00191570668888889, -0.00188197921594203, 0.219782496255814,
-0.00182879988888889, 4.84401929078014e-05, 0.000748088722727273,
-0.0012550096047619, 0.000173737503030303, -0.000151241941085271,
0.581544109520833, -0.00156196413809524, 0.00255382897777778,
0.208691561081818, 0.664795524311111, 0.42483048627234, 0.00175584192608696,
0.201560880468116, -0.00209381856899225, 0.000321025016666667,
-0.000421247062411348, -0.00215724371347518, 0.646012675801418,
0.350737970755556, 0.178416441162121, 0.00103144908992248, -0.000187515334848485,
0.000174517300775194, -0.000258069957723577, -0.00119511305426357,
0.0003255877, 0.00023375347751938, -0.0005782456, -0.000402424114285714,
0.1384140809, 0.418395168104762, 0.306135187705797, -0.00102090409612403,
-0.000444740023255814, 0.138124852631206, -0.0004063458, 0.00029016809047619,
-0.000799771212403101, -0.000183847566666667, 0.000114925325,
-0.000269002668292683, -0.000733895628787879, -0.000981041028571429,
-0.000873523457142857, 3.48687286821712e-06, -0.00107125035238095,
0.36082286618, -0.000514447102727273, -0.00280299127727273, -0.00263217947069767,
-0.00203968404782609, -0.000458293174166667, -0.00351976894695652,
0.000204725635833333, -0.00339431988181818, 0.193479343088889,
0.000112535625217391, -0.00213525120085106, -0.00288490815428571,
0.343934077535319, 0.32598547811913, 0.155322520318298, -0.00360304194468085,
0.116695380217872, -0.00115564579148936, 0.288642051333617, 0.00110193360444444,
0.000231925527826087, -0.00271350401130435, 0.000528825044545455,
-0.000843372957391304, 0.00152301136425532, 0.42680129887, 0.000348928813333333,
-0.00106065298583333, 0.1891960110325, 0.00144156823234043, -0.0058980177693617,
0.172238823228085, 0.180649895645, -0.00399227711833333, -0.00396987499111111,
-0.000279930486666667, -0.00221300633826087, -0.00437551217957447,
0.373557485947234, -0.000553946612765957, 0.180780737205957,
0.186384874315, 0.0601822571175, -0.0013033474675, 0.000607181016170213,
-0.00182825481191489, -0.00101525615565217, -0.00307393246086957,
0.246870552998261, 0.244639116138261, -9.59920234782608e-05,
0.00103178067829787, 0.158475002401702, 0.0957530407148936, -0.00108818790695652,
-0.00106499063130435, 0.00182353239166667, -0.000770247991111111,
0.251169645886809, 0.00135098203818182, -0.00106422307829787,
0.268188202118298, 0.08014729743, 0.173129845155455, 0.266595692084348,
-0.000510779745454545, 0.000609720036363636, -0.000709136471489362,
-0.00074677718, 0.273296411113636, -0.00150910594978723, 0.375676878958261,
-7.46597313043479e-05, 0.222301664431111, 0.599549396090213,
0.13323872568, 0.216335941017778, -0.000372647409090909, 0.000250945590434783,
-0.000609455297777778, 0.163203653554783, 0.365593381717273,
0.387049939765714, 0.377756158386667, -0.000608418738095238,
-0.000877734902222222, 0.29536194985619, -0.00151932240909091,
8.84523111111115e-06, 0.887420447928889, 6.04058888888889e-05,
-0.00168793287111111, 0.245879004745116, 0.00210788515534884,
0.0518048467911111, -0.00204896865106383, -0.000766938131304348,
0.000369568767804878, 0.385807118748837, 0.00125597745333333,
0.00342261065047619, 0.000917297056744186, 0.145203749862727,
-0.000608651458181818, -0.000619898377777778, 0.00255708987363636,
0.46996938808, 0.0463716679041861, 0.209438538634783, 0.00629131560930233,
0.000908092756190476, 0.118311483963636, 0.055266674, 0.00411797937,
0.371606156751111, -0.00341520444, -0.00207519669043478, 0.388790875192558,
0.382199056508182, 0.000288075208888889, -0.00233150982, 0.190417886325455,
0.367062582115556, 0.133206785506667, 0.591484569088889, -0.000679957030909091,
0.00235175421454545, 0.271733587002791, -0.00258801096444444,
0.209600095759091, 0.268993620327273, -0.00175308111272727, 0.000141544911111111,
-0.00146279359217391, -0.000720371102608696, -0.00227193868888889,
-0.00099748604, 0.249673461088571, 0.00122685363809524, -0.00157589268651163,
0.000390671067272727, -0.000246003732093023, -0.00189639072,
0.272506430813333, -0.00121220570222222, -0.00129201353217391,
-0.000471348084444445, 0.108562531000909, -0.00230415824363636,
-0.00146539943555556, -0.000698085466363636, -0.00125351572186047,
0.32477301345, -0.000655153204444444, 0.23415397968, -0.00311150981909091,
-0.00240564984727273, -0.00271525109, 0.124826008829787, 0.228650982202791,
-0.000831444746666667, -0.00208768206727273, 0.00210693784340426,
-0.00032504578, -0.00201565726636364, -0.00121992036465116, 0.241373214804545,
0.00180044913217391, -0.00144649063111111, 0.293257521547234,
0.156942076691818, -0.000396712526086957, 0.333872094768889,
0.00126618459555556, -0.0022017223626087, 0.132416393375814,
-0.00154369867555556, -0.000127951540425532, 0.000643781042727273,
-0.00149518583142857, 0.000159095756363636, 0.431103792525581,
0.3497642155475, -0.0010894085247619, 0.00271588987111111, 0.347565654681818,
0.554514602551111, 0.25466477267234, 0.00141746724608696, 0.120691384734783,
-0.00244462910232558, 0.06381322055, -0.000572060355744681, 0.212942333313191,
0.387555468868085, 0.210574743968889, 0.107224934255455, 0.00119969578325581,
-0.000265811628181818, -0.00014974783255814, -6.32937443902438e-05,
-0.00163017968093023, 0.00031326598, -0.000229250015813954, 0.281825955426667,
-0.000219622714285714, 0.0828831356333333, 0.251266484118095,
0.18387889279913, -0.000723895042790698, 0.362715330336744, 0.0829555198178723,
-0.000342741853333333, 0.00098233181047619, 3.61713209302325e-05,
0.00060474866, 0.000214934285, -0.000507613948292683, -0.00126401089545455,
0.295409336744762, 0.49940544567619, 0.000444745259534884, 0.075755845887619,
0.2663678565, -0.00112328892272727, -0.00252072917727273, 0.0919692581493023,
-0.00293427190782609, -7.54536341666666e-05, -0.00354078930695652,
2.57954158333333e-05, -0.00262876556181818, 0.0960986548688889,
-0.000307806654782609, -0.00256542482085106, -0.00344343351428571,
0.171732012655319, 0.16290640195913, 0.0771365308982979, -0.00323413486468085,
0.239592012997872, 0.0929770842485106, 0.143919237313617, 0.0872022225044444,
-0.000374014952173913, -0.00198149831130435, 0.150230880804545,
0.0495306889826087, 0.00207576486425532, 0.21266222853, 6.23467733333334e-05,
-0.000502785265833333, 0.1146813025125, 0.00236195601234043,
-0.0055398928293617, 0.0850331580480851, 0.091095192845, -0.00308972659833333,
-0.00268806053111111, -0.000621778406666667, -0.00211494293826087,
0.0788185111804255, 0.187436011507234, -0.00105928309276596,
0.273111877825957, 0.091654408075, 0.1881146334375, -0.0012845890675,
0.000535671256170213, 0.261780701048085, -0.00130433873565217,
0.0672782252391304, 0.123152524898261, 0.122219372418261, 0.108913352556522,
0.00154812281829787, 0.0787473360617021, 0.208401497354894, 0.0494821097730435,
-0.00100385527130435, 0.00179214179166667, -0.00113253543111111,
0.125312734706809, 0.00112138269818182, -0.000824940378297872,
0.133558478718298, 0.06196782193, 0.0853069711554545, 0.227115156824348,
-0.000573424825454545, 0.147897520156364, 0.0948538439485106,
-0.00102526364, 0.136662496333636, 0.0497748200102128, 0.327559354018261,
0.158837548148696, 0.111405826131111, 0.472707361930213, 0.15095515508,
0.108453752477778, -0.000742863369090909, 0.000265612850434783,
0.187649188042222, 0.277873906394783, 0.417418378297273, 0.383188042745714,
0.188476248766667, -0.000679375438095238, -0.000940556842222222,
0.14747278479619, -0.00116911116909091, 0.193063097831111, 0.443910019148889,
0.156860665528889, -0.00140947317111111, 0.194701012465116, 0.179753437755349,
0.0271931040711111, -0.00148793193106383, -0.000247921611304348,
0.0215570688078049, 0.193278326948837, 0.269718930653333, 0.00265989913047619,
0.00111463559674419, 0.0727459556427273, 0.178373261021818, 0.0498605999422222,
0.00285280777363636, 0.28503765544, 0.0225589752241861, 0.105547501674783,
0.00522300738930233, 0.000134479296190476, 0.0587813904836364,
0.02802175016, 0.17572127429, 0.184946720591111, -0.00234486358,
-0.00230577149043478, 0.194051282172558, 0.250351470368182, 0.143461193228889,
-0.00273105342, 0.0943393136254545, 0.184555788855556, 0.0659578766466666,
0.294674815488889, -0.000197858810909091, 0.135317324614545)), row.names = c(NA,
800L), class = "data.frame")
This is the plot i get which is incorrect, I am very confused as I am sure it was working in the past that code, as well as I am sure the syntax is correct! Obviously i must be missing something.
Given your data and script, the plot looks ok on my computer (see below). Which version have you used?
library("plotly")
P1 <- ggplot(data=sd1, aes(x=Period, y=variable, fill=value)) +
geom_tile() +
ggtitle("Stress Portfolio Returns") +
scale_fill_gradientn(colors=colorRampPalette(c("lightgray","royalblue","seagreen","orange","red","brown"))(500),name="Returns") +
labs(x = "Period",y="Size") +
theme_bw()
ggplotly(P1)
I recommend to follow the advice of #Dan Adams. It may also help to check the versions of R and packages. Here a part of my SessionInfo (only R version and attached packages shown).
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)
# ... omitted
other attached packages:
[1] plotly_4.10.0 ggplot2_3.3.5
# ... omitted