所以我在循环中使用ggplot2绘制了一系列lmer模型,但是我无法确定如何在循环内指定每个绘图的标记。
示例数据
dput(data)
structure(list(Reaction = c(249.56, 258.7047, 250.8006, 321.4398,
356.8519, 414.6901, 382.2038, 290.1486, 430.5853, 466.3535, 222.7339,
205.2658, 202.9778, 204.707, 207.7161, 215.9618, 213.6303, 217.7272,
224.2957, 237.3142, 199.0539, 194.3322, 234.32, 232.8416, 229.3074,
220.4579, 235.4208, 255.7511, 261.0125, 247.5153, 321.5426, 300.4002,
283.8565, 285.133, 285.7973, 297.5855, 280.2396, 318.2613, 305.3495,
354.0487, 287.6079, 285, 301.8206, 320.1153, 316.2773, 293.3187,
290.075, 334.8177, 293.7469, 371.5811, 234.8606, 242.8118, 272.9613,
309.7688, 317.4629, 309.9976, 454.1619, 346.8311, 330.3003, 253.8644,
283.8424, 289.555, 276.7693, 299.8097, 297.171, 338.1665, 332.0265,
348.8399, 333.36, 362.0428, 265.4731, 276.2012, 243.3647, 254.6723,
279.0244, 284.1912, 305.5248, 331.5229, 335.7469, 377.299, 241.6083,
273.9472, 254.4907, 270.8021, 251.4519, 254.6362, 245.4523, 235.311,
235.7541, 237.2466, 312.3666, 313.8058, 291.6112, 346.1222, 365.7324,
391.8385, 404.2601, 416.6923, 455.8643, 458.9167, 236.1032, 230.3167,
238.9256, 254.922, 250.7103, 269.7744, 281.5648, 308.102, 336.2806,
351.6451, 256.2968, 243.4543, 256.2046, 255.5271, 268.9165, 329.7247,
379.4445, 362.9184, 394.4872, 389.0527, 250.5265, 300.0576, 269.8939,
280.5891, 271.8274, 304.6336, 287.7466, 266.5955, 321.5418, 347.5655,
221.6771, 298.1939, 326.8785, 346.8555, 348.7402, 352.8287, 354.4266,
360.4326, 375.6406, 388.5417, 271.9235, 268.4369, 257.2424, 277.6566,
314.8222, 317.2135, 298.1353, 348.1229, 340.28, 366.5131, 225.264,
234.5235, 238.9008, 240.473, 267.5373, 344.1937, 281.1481, 347.5855,
365.163, 372.2288, 269.8804, 272.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, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L), .Label = c("308", "309",
"310", "330", "331", "332", "333", "334", "335", "337", "349",
"350", "351", "352", "369", "370", "371", "372"), class = "factor"),
sim_1 = c(299.964231384066, 267.491912906856, 285.889371053274,
322.537140999718, 335.214513295517, 335.311117763686, 308.660876991118,
311.0711492498, 385.816499225727, 361.591295830996, 262.303849138308,
271.188629868801, 257.959634776352, 318.428881916069, 311.451855350732,
326.573871201961, 369.379064149954, 380.765791679964, 409.604000740783,
376.033147049433, 277.662434340415, 306.670416712317, 252.692807794961,
269.141206895719, 272.257744019907, 273.927595597971, 323.368971221048,
348.089297856304, 372.158037637232, 378.210770078511, 261.676017761342,
345.286580451847, 291.54724942077, 321.942103677611, 319.066139879949,
337.371893169806, 355.949879319467, 364.813844470173, 371.752756525177,
385.791319294913, 286.981188251501, 297.548819999732, 278.990403969054,
322.764626817582, 330.477884620956, 423.792129268372, 330.835096397743,
380.255055589314, 349.566135502178, 361.207887030423, 285.627000846447,
281.03281902119, 330.594965535968, 343.784671472299, 363.142607676188,
351.135379536966, 395.710023288258, 455.750684591618, 489.78595951969,
486.748875254035, 218.378824348612, 255.34710725818, 271.691952019533,
214.999400250621, 248.307220353828, 290.291274141785, 255.754620764028,
274.666171070366, 282.038424819988, 269.757854934444, 236.854376904564,
261.509988141163, 265.700768149116, 318.814882346351, 292.040718676616,
307.293504538655, 350.241953862745, 319.402475763064, 359.424202182025,
334.787787382359, 274.515399215196, 232.841242106336, 223.070243408808,
198.553051161577, 225.242455437881, 200.15934774696, 202.185907018875,
243.991815904352, 201.744763840107, 174.00685400798, 197.546187164272,
200.940899761501, 242.17816137642, 280.591530238188, 235.38305463556,
289.277794412561, 296.669771123147, 334.270833817783, 322.325851945278,
366.471501788248, 199.453916922683, 287.524404484846, 266.594336497439,
240.779481084656, 207.730403302928, 261.443903988526, 257.528967414075,
245.175522072299, 249.389220911585, 230.739795282281, 256.755247522168,
272.16601094918, 274.786974364596, 265.045463565352, 334.48407992422,
348.042000869878, 329.764955573542, 327.667610641449, 391.029738567312,
394.261505821705, 302.93866528393, 272.253573810679, 289.718108032336,
350.947547474787, 342.389504481718, 355.961027207619, 396.302313689479,
362.039669280728, 316.431055554823, 385.07600885413, 275.263431756927,
298.295801500904, 210.653418763036, 216.738680762738, 228.429270690433,
219.818677222045, 230.56948159876, 242.65243462655, 216.956496314321,
300.129125686067, 265.104630616233, 267.228554484115, 289.099394936063,
284.407665021742, 287.104324077865, 335.312924116153, 298.999608450575,
278.738648110863, 328.49285654228, 316.661616126373, 248.882255449474,
249.595660835004, 302.588853363055, 301.867862247138, 334.213292087507,
329.713698224951, 333.554345760387, 397.097583612986, 372.841943906807,
418.751797808512, 245.269772583493, 271.570586959615, 284.085047994806,
288.45161939073, 340.451891682993, 312.832334130186, 327.144002264074,
304.255710148906, 317.189614150529, 368.872900187749, 295.620053981986,
296.292604172199, 229.917595386271, 318.631515062137, 293.111611658494,
267.322931990642, 286.931973273292, 244.984551195061, 242.710125999761,
272.936737219166), sim_2 = c(202.078541676681, 242.867370798436,
276.469302444478, 222.054156237672, 234.869034276772, 260.212942075645,
238.233364792791, 259.720776916912, 297.119317769345, 304.850551220275,
203.817220435292, 156.159979465895, 211.71354296982, 249.895663564398,
253.008086289517, 236.864373168548, 182.202370343393, 262.409490939542,
285.083357348811, 281.122971255142, 260.332340447118, 275.854194002271,
263.167008634324, 315.345943688578, 302.365227428633, 267.030600839975,
342.695600561878, 346.668651655545, 345.439693770349, 290.306222040088,
254.865070174519, 292.377664683554, 283.911695274238, 298.37809470775,
359.096352518545, 281.886104585946, 255.428372079613, 323.192418903055,
305.270265508814, 340.961176508894, 198.076210060879, 287.77446686883,
240.295426358548, 243.542742208211, 263.801029327284, 302.427864780702,
295.141758776719, 263.34549162231, 317.127061122913, 348.998631047882,
233.10756634957, 269.239970246639, 264.998059074354, 285.583538408867,
317.728112153489, 349.113053456325, 310.122717985737, 359.481657101835,
376.841033375421, 377.553347175506, 214.67393613075, 239.00602108651,
284.167599447162, 272.498197321074, 277.068471951937, 315.33982364314,
293.510465791086, 365.928565791748, 340.763605433466, 372.007958674474,
260.366013674734, 265.205220234226, 317.667463034761, 256.391158522428,
312.978180214686, 290.622236182207, 274.590497621983, 300.35585505553,
327.377596137579, 303.527265151692, 269.117805970784, 307.649307389382,
343.902788998713, 331.585871218395, 312.832277889413, 329.751600482837,
358.390181829012, 379.428163095485, 407.232463533516, 439.803088185855,
242.417024076997, 237.118229881838, 293.246155499907, 296.843427812765,
331.497370804934, 270.90578882668, 328.282957731861, 372.768066251911,
368.75077335001, 340.996084486903, 286.305710170197, 256.539723139968,
278.545589782502, 331.834955161219, 305.313390283143, 345.237676875134,
327.875569943271, 306.281578676792, 336.195760618778, 341.356235690252,
167.833861223408, 182.319981414247, 197.701148676341, 206.099776739211,
271.448862166232, 198.252819043966, 233.805986308183, 264.054783627868,
307.495008402868, 278.784708334438, 274.38417575711, 211.936881684792,
302.39092308816, 259.05533008827, 281.00563272027, 271.039952028308,
314.209939405435, 367.826036907867, 395.475514682222, 317.2819338493,
277.677316565777, 319.489325265463, 297.660649176939, 315.797704013651,
332.294702773488, 374.107480515366, 413.283943075662, 413.831955616233,
423.14204062319, 425.935778813079, 246.863338117012, 237.596973789292,
262.064872337404, 314.824256170761, 315.625334947357, 328.032393644866,
332.873816425954, 380.79303561329, 408.679982157159, 369.821832479469,
260.811774282669, 277.188196240769, 277.672033596095, 283.454393323388,
325.492392986034, 358.871114562068, 396.431173848642, 326.318131814505,
373.740840050953, 400.288199653737, 230.737339267534, 269.095850756178,
283.412337343826, 299.817125800636, 277.295559566833, 353.776397459479,
347.145389280739, 417.232392829486, 413.693035834264, 371.127431316133,
229.117266461985, 220.871947052106, 268.268830066501, 256.36274595031,
250.21780741902, 285.160906604297, 287.315526323414, 288.665619616054,
354.039628988072, 298.267488566935), gender = 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, 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), .Label = c("0",
"1"), class = "factor")), .Names = c("Reaction", "Days",
"Subject", "sim_1", "sim_2", "gender"), row.names = 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"), class = "data.frame")
使用此数据,我使用以下函数构建了一个lmer模型列表。
library(lme4)
library(effects)
library(ggplot2)
fitlmer <- function(data, respnames){
lapply(respnames, function(resp){
lmer(as.formula(sprintf("%s ~ Days + gender + (Days | Subject)", resp)),
data = data, REML = FALSE)
})
}
output <- fitlmer(data, colnames(data)[c(1,4,5)])
然后我编写了以下函数来为每个模型生成一系列图。 (这使用效果包首先从lmer模型中提取预测值和置信区间)
plotmer <- function(x){
lapply(x, function(y){
df <- as.data.frame(Effect(c("gender", "Days"), y))
ggplot(data = df, aes(x=Days, y=fit)) + geom_line(aes(colour=gender)) +
geom_ribbon(aes(ymin=lower, ymax=upper, fill=gender), alpha=0.2)
})
}
plots <- plotmer(output)
然后我可以使用ggtittle分别为每个图添加一个小的
plots[[1]] + ggtitle("Reaction")
plots[[2]] + ggtitle("sim_1")
plots[[3]] + ggtitle("sim_2")
然而,当我尝试在循环中命名每个情节时,我无法使其正常工作。我最接近的是为列表中的每个模型指定一个名称,然后将这些名称与plotmer函数一起使用。然而。这将使用第一个型号名称命名每个图。
names(output) <- colnames(data)[c(1,4,5)]
plotmer <- function(x){
lapply(x, function(y){
name <- names(x)
df <- as.data.frame(Effect(c("gender", "Days"), y))
ggplot(data = df, aes(x=Days, y=fit)) + geom_line(aes(colour=gender)) +
geom_ribbon(aes(ymin=lower, ymax=upper, fill=gender), alpha=0.2) +
labs(title=name)
})
}
plots <- plotmer(output)
plots[[1]]
plots[[2]]
如何解决此问题?
由于
答案 0 :(得分:1)
你的名字&lt; - names(x)但是x永远不会改变,所以它总是返回所有名字的向量,我猜想labs()只是从向量中取出第一个元素。您不能在lapply中同时迭代名称和值。考虑迭代名称并提取循环中的元素,或使用mapply同时迭代两者。
尝试
plotmer <- function(x){
mapply(function(y, name){
df <- as.data.frame(Effect(c("gender", "Days"), y))
ggplot(data = df, aes(x=Days, y=fit)) + geom_line(aes(colour=gender)) +
geom_ribbon(aes(ymin=lower, ymax=upper, fill=gender), alpha=0.2) +
labs(title=name)
}, x, names(x))
}