我有一个嵌套for
循环的代码,运行完美,并在页面中提供4X4图。我需要在每个情节中插入标题。以下是我的代码。
我想要做的是创建一个向量并在其中分配我的标题,如代码所示,然后在循环内读取它。为此,我需要将索引i转换为数字并使用第二个向量的位置。
这是我的方法,可能不是那么好,所以你可以使用我的或给出你自己的想法。您可以使用任何随机数据集并使用简单的绘图/直方图。矢量分别代表星期几和一天中的时间。
#set dimension
par(mfcol=c(4,4))
#vector definition
days<-c(1,2,3,4)
hours<-c(8,14,18,22)
#Title vector
D1<-c("Monday (7-8 am)","Monday (1-2 pm)","Monday (5-6 pm)",
"Monday (9-10 pm)")
D2<-c("Wednesday (7-8 am)","Wednesday (1-2 pm)","Wednesday (5-6 pm)",
"Wednesday (9-10 pm)")
D3<-c("Friday (7-8 am)","Friday (1-2 pm)","Friday (5-6 pm)",
"Friday (9-10 pm)")
D4<-c("Saturday (7-8 am)","Saturday (1-2 pm)","Saturday (5-6 pm)",
"Saturday (9-10 pm)")
#Loop
for (i in days)
{
for (j in hours)
{
# set positioning of the histogram
par("plt" = c(0.2,0.95,0.35,0.84))
# plot the histogram
hist(path$TT[path$days==i & path$hours==j], breaks = seq(0,60,by=3), xlab="Travel Time",
ylab="Number of paths",col="blue", **main=D??**, mgp=c(2.5,1,0))
}
}
structure(list(days = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L,
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
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13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L), TT = c(34.82720833,
34.13870083, 30.59218805, 35.1616205, 34.87982204, 30.74262596,
35.19981237, 35.14235172, 31.6716496, 29.84148401, 31.32268062,
30.58250275, 35.26514263, 33.55230269, 34.97001136, 31.09735713,
29.90509108, 33.78335499, 33.08419061, 33.9702478, 32.68267307,
32.88848951, 30.16693345, 32.85994732, 30.83277565, 34.62568305,
34.13923292, 33.50498645, 31.31095608, 34.31001321, 33.99902318,
33.7909643, 34.33340843, 32.30046602, 34.74999297, 29.87097318,
32.91255436, 30.37869556, 35.22453148, 33.91415576, 30.87027627,
34.32036758, 34.14405484, 32.52770687, 30.63412371, 30.69590367,
34.10350198, 33.51383263, 31.19792969, 35.26664132, 33.79975778,
30.9254123, 33.58382797, 32.47180323, 35.07275967, 30.97518331,
34.09754282, 31.30283331, 35.03617718, 35.0447385, 34.48088429,
34.93546837, 30.97837093, 31.14469741, 30.92743268, 34.10879646,
30.4886625, 35.00307314, 31.41065689, 31.82113768, 30.38511722,
30.39628127, 31.89778508, 31.5036342, 30.78847263, 30.63294595,
34.40494811, 32.57036077, 31.96399169, 33.90064885, 31.64029012,
34.1366935, 35.24047602, 30.50038163, 35.26178882, 30.67850437,
31.28041078, 31.13586861, 34.03564851, 30.45301463, 31.46075363,
32.79463877, 34.37256141, 31.14590299, 32.98806056, 34.61871373,
34.50000295, 33.64822723, 31.79305995, 32.95337037, 31.97535842,
33.01756184, 30.27499142, 31.52636985, 33.88390737, 29.86033691,
33.10717421, 31.13912362, 34.03308637, 29.82060846, 30.29160216,
30.68720702, 32.21043532, 32.38637581, 29.87286573, 31.91229798,
33.07799897, 30.41662694, 32.24261367, 35.3258724, 29.81198078,
29.87369792, 29.5469277, 31.07479327, 29.93749303, 31.32897414,
32.11042476, 31.74139691, 29.35309499, 31.91510643, 28.43111183,
30.64316778, 28.82045246, 31.2966231, 32.88217249, 28.85142648,
32.61772627, 28.89998879, 29.09439029, 31.17275104, 30.14374991,
32.54361297, 30.50674627, 32.01595442, 30.50549694, 30.92120556,
28.56600115, 32.6272292, 32.01189691, 32.48467475, 32.63696512,
30.92335971, 31.05045202, 30.7754939, 31.40027579, 29.12356583,
31.77973836, 28.78119827, 31.44082345, 30.73383322, 32.04126499,
30.09865077, 32.23577216, 29.08265343, 30.49423226, 31.46262176,
29.84828538, 30.18785884, 29.51834908, 29.37202672, 31.50806652,
32.40830835, 30.48030326, 31.25898945, 28.36670284, 31.28059981,
29.34232677, 30.09806882, 32.11127774, 29.59171523, 30.61713837,
29.76958526, 31.85824615, 32.16215903, 29.84655136, 31.07721122,
28.65494456, 30.9843114, 32.54863022, 31.46634971, 31.89779842,
32.82481805, 32.14782935, 32.08964421, 31.60785849, 32.91857557,
31.71183437, 31.81246841, 32.98599723, 28.95747656, 28.84662181,
31.71611474, 31.62086303, 32.53920721, 30.42499004, 28.99300588,
29.61203445, 32.4920689, 29.36255767, 32.6194317, 31.04202451,
28.75123245, 30.13704325, 30.92045914, 32.57753631, 30.83279548,
28.8546849, 30.74245368, 29.03716971, 28.37275181, 30.86814322,
30.61960665, 30.42719574, 30.27684903, 32.91275304, 29.80632759,
29.50108563, 32.6131215, 30.03530353, 30.24898855, 29.97890411,
29.91508311, 30.66431902, 29.44062756, 30.78040092, 30.42641885,
32.52252736, 32.02849124, 28.44168133, 28.77193919, 32.3661733,
32.50081923, 30.78754405, 29.31429942, 29.25319403, 29.41670938,
34.79250707, 28.45292865, 33.30658009, 36.95793072, 31.1241599,
29.47446652, 37.93368226, 29.99169743, 34.53286071, 33.30080173,
32.07298455, 34.59538339, 33.19895485, 32.39419483, 31.37985584,
33.10293436, 29.39098815, 29.6792889, 35.03296983, 37.90584009,
30.95003357, 33.20300797, 37.19244019, 35.17202829, 33.36301054,
35.45811104, 32.30603702, 35.90719466, 32.53788221, 32.98462237,
34.40384647, 34.60599035, 36.12782575, 34.22463048, 29.98624712,
35.806683, 36.85504472, 35.98104837, 35.97362738, 35.43026929,
29.52289309, 29.0544412, 28.38438112, 29.31043103, 34.55714132,
31.35110246, 35.45463173, 32.52063466, 29.64833452, 31.74827447,
31.19599864, 35.86874035, 31.36035725, 30.90048731, 36.67327499,
30.0504123, 37.41148645, 33.68205359, 29.2592527, 28.82514246,
30.62364715, 37.55578321, 32.25899523, 34.31735337, 37.1286007,
30.09667053, 37.77301539, 37.28325032, 33.82381014, 33.64911154,
32.23733708, 35.36476734, 31.19880018, 29.1404291, 30.72636631,
34.77003685, 37.31098961, 31.55246022, 28.51524079, 35.97250119,
35.08409392, 36.5458489, 37.35540297, 30.23406879, 29.17387163,
33.74088357, 29.40765925, 29.98726349, 29.58959745, 31.96605073,
31.94788415, 33.60347166, 28.43148601, 29.65454367, 36.06816061,
29.96597865, 31.90935292, 28.59771444, 32.44428733, 31.50734498,
30.23029062, 32.7213003, 33.17963215, 30.84546259, 35.61594726,
31.1375163, 33.58903731, 36.3755896, 30.15521544, 32.64832733,
29.75419547, 32.87727257, 32.86349263, 30.87051665, 34.99052692,
29.32459293, 29.75063939, 29.31336196, 30.26155711, 37.78471798,
29.29637466, 33.63983534, 29.0707227, 37.23740461, 30.46483145,
32.5191104, 32.38759822, 35.67256593, 31.96392716, 33.3250217,
35.46341363, 28.75439972, 33.2611733, 30.02014914, 35.78496489,
32.96781502, 31.43534921, 35.07596123, 34.52762462, 30.26655854,
35.32014083, 37.55183466, 34.14971103, 36.29105196, 32.40044715,
36.0587327, 31.83769864, 33.92873059, 34.70263617, 30.80816039,
30.68630199, 31.01802064, 30.80777532, 35.05333618, 27.06058834,
27.79241831, 27.33752079, 27.77903509, 26.947812, 27.8862964,
27.39365377, 27.9236377, 26.78983708, 27.98767273, 27.93024624,
27.84690108, 27.32830243, 26.81574528, 27.11055277, 27.39296015,
28.00610613, 27.71688355, 27.62271524, 27.69926561, 26.77071774,
26.75407601, 27.54772857, 26.85613667, 27.43762662, 27.45478206,
27.70204762, 27.66985159, 27.46593956, 28.00153523, 27.85391116,
26.78324156, 27.51476443, 27.54375831, 27.45536832, 27.25299275,
27.42563343, 27.35861323, 27.89703515, 27.94359525, 27.02701474,
28.01213784, 27.05632904, 27.219231, 28.00160216, 27.06621867,
26.83356071, 26.85138171, 26.9857268, 26.84488214, 27.04212578,
27.90226659, 26.88270484, 27.36445874, 27.98903653, 26.74879158,
27.91409337, 27.04442553, 27.76393403, 26.97261286, 26.82558533,
27.40286709, 26.90959192, 27.61358064, 27.67649126, 27.98923329,
27.27538051, 27.93429854, 27.24070111, 27.79609001, 27.51659686,
27.60029289, 26.85518925, 27.31821322, 27.1642527, 27.27570585,
27.67152235, 26.96014272, 27.89962397, 27.84824436)), .Names = c("days",
"hours", "TT"), class = "data.frame", row.names = c(NA, -480L
))
答案 0 :(得分:0)
看起来你有4 * 4 = 16个图,在4个向量中有16个标题。 在函数中尝试这个参数,
main=get(paste0("D",i))[which(hours==j)]
get()
函数可以获取具有给定对象名称的对象。
我使用了一些模拟数据,只是为了检查标题。看起来不错,
示例代码:
x<-rnorm(50)#my simulated data
for (i in days)
{
for (j in hours)
{
hist(x,xlab="Travel Time",
ylab="Number of paths",col="blue",
main=get(paste0("D",i))[which(hours==j)], mgp=c(2.5,1,0))
}
}
答案 1 :(得分:0)
这是经过修改以匹配提供的数据。
这里的主要思想是制作标题矩阵,并在每次打印时简单地访问矩阵。问题中的代码循环了几个小时和几天。因为我想知道索引,所以我将其更改为循环索引1:4。这意味着在原始代码使用循环变量(小时或天)的情况下,我使用索引从小时或天中选择元素。
我假设我们已经有data.frame和OP的列表D1,D2,D3和D4。
LabelMat = matrix(c(D1, D2, D3, D4), nrow=4)
for (i in 1:4) {
for (j in 1:4) {
# set positioning of the histogram
par("plt" = c(0.2,0.95,0.35,0.84))
# plot the histogram
hist(path$TT[path$days==days[i] & path$hours==hours[j]], breaks = seq(0,60,by=3),
xlab="Travel Time", ylab="Number of paths",
col="blue", main = LabelMat[i,j], mgp=c(2.5,1,0))
}
}