I have 9 plant species for which I want to plot a relationship. I have been plotting these the long way because I need to add text to the graphs individually. I have the 9 plots formatted to fit on one page in a 3x3 grid.
But is there a way to do this with a loop instead? And get the 9 plots formatted into 3x3? If this is possible, can you still add text to each plot?
I have code like this for all 9 but here are the first 2 species.
First I subset by species:
Acru<-carbon2[Species=="Acru",]
Arte<-carbon2[Species=="Arte",]
...
par(mfrow=c(3,3), cex=.3)
plot(LogRecBio~LogPreBiomass,data=Acru,font.lab=2,font.main=2, font.sub=3, mgp=c(2.5,1,0), cex.lab=2, cex.main=1.5, axes=F,lwd=1.5, cex=2.5, ann=F)
box()
axis(1,at=c(-2,-1,0,1,2,3,4),font=2,cex.axis=3)
axis(2,at=c(-5,-4,-3,-2,-1,0,1,2),font=2,cex.axis=3)
text(2,-0.27, sprintf("a) Acer rubrum"),font=3,cex=2.5)
text(2.35,-0.6,sprintf("R²= 0.18"),cex=2.5)
fit.bio.acru1 <- lm(LogRecBio ~LogPreBiomass, data=Acru, subset=c(Site=="7"))
abline(fit.bio.acru1, lwd=2, col='red')
fit.bio.acru2 <- lm(LogRecBio ~LogPreBiomass, data=Acru,subset=c(Site=="8"))
abline(fit.bio.acru2, lwd=2, col='blue')
fit.bio.acru
plot(LogRecBio~LogPreBiomass,data=Arte,font.lab=2,font.main=2, font.sub=3, mgp=c(2.5,1,0), cex.lab=2, cex.main=1.5, cex.axis=1.2,axes=F,lwd=1.5, cex=2.2, ann=F)
box()
axis(1,at=c(-3,-2,-1,0,1.0,2,3),font=2,cex.axis=3)
axis(2,at=c(-4,-3,-2,-1,0,1,2),font=2,cex.axis=3)
text(2.05,0.97, sprintf("b) Arundinaria tecta"),font=3,cex= 2.5)
text(2.56,0.59,sprintf("R² = 0.04"),cex= 2.5)
fit.bio.arte <- lm(LogRecBio ~LogPreBiomass, data=Arte)
abline(fit.bio.arte, lwd=2, col='red')
fit.bio.arte
Part of data frame carbon2:
structure(list(Treatment = c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), Species = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L), .Label = c("Acru", "Arte", "Clal", "Euro", "Gafr", "Ilgl",
"Lylu", "Oxar", "Pepu"), class = "factor"), Tag = c(64L, 248L,
249L, 250L, 251L, 252L, 253L, 315L, 316L, 318L, 931L, 932L, 933L,
934L, 935L, 936L, 3L, 4L, 5L, 6L, 917L, 918L, 919L, 920L, 921L,
923L, 924L, 995L, 996L, 997L, 208L, 209L, 210L, 211L, 212L, 213L,
214L, 215L, 323L, 324L, 925L, 926L, 927L, 928L, 929L, 930L, 987L,
988L, 989L, 990L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 51L,
52L, 912L, 913L, 914L, 915L, 916L, 59L, 60L, 62L, 63L, 240L,
901L, 902L, 903L, 904L, 905L, 907L, 908L, 909L, 22L, 23L, 937L,
938L, 939L, 976L, 977L, 979L, 980L, 981L, 985L, 986L), site = c(12L,
7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 23L, 23L, 23L, 23L, 23L,
23L, 16L, 16L, 16L, 16L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 16L,
16L, 16L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 23L, 23L, 23L,
23L, 23L, 23L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 12L, 12L, 23L, 23L, 23L, 23L, 23L, 12L, 12L, 12L, 12L,
7L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 12L, 12L, 23L, 23L,
23L, 16L, 16L, 16L, 16L, 16L, 16L, 16L), stem = 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), heightPre = c(58, 50, 38, 33, 28,
18, 20, 28, 47, 118, 109, 103, 99, 79, 69, 73, 54, 33, 32.5,
74, 67, 57, 77, 49, 45, 58, 89, 49, 33, 37, 69, 53, 64, 49, 70,
36, 59, 66.5, 21, 36.5, 59, 37, 59, 55, 33, 27, 35, 41, 65, 56,
37, 53, 73, 59, 16, 51, 37, 77, 79, 45, 68, 75, 110, 56, 115,
42, 90, 107, 86.5, 37, 113, 211, 130, 175, 135, 175, 122, 66,
115, 167, 84, 63.5, 144, 16, 51, 45, 54, 32, 70, 53), height1mo = c(0,
0, 0, 0, 0, 0, 0, 6, 10, 15, 25.5, 25, 26.5, 20, 19.5, 18, 19,
8.5, 20, 12, 7.5, 4, 7, 0, 4, 11, 3, 9, 3.5, 12.5, 10, 12.5,
19, 17.5, 11.5, 7, 12, 14, 18, 18, 3, 3.5, 6, 1, 9, 0.5, 7.5,
9, 17, 15, 1.5, 7.5, 14, 18, 5, 4.5, 6, 15.5, 15, 2, 0.5, 0,
12, 0, 5, 0, 0, 0, 0, 0, 37, 31, 14, 0, 20, 35, 0, 39, 0, 0,
21, 7, 12, 11, 23.5, 15, 26, 7, 5, 17), height2mo = c(2.5, 7.5,
9, 10, 12, 0, 8, 23, 34, 26.5, 57, 49, 57.5, 41, 43, 28, 29,
11, 29.5, 20, 21, 20, 10, 7, 11, 22.5, 13, 21, 17.5, 29.5, 24.5,
38, 53, 49, 44, 19, 38, 49, 37, 40, 10, 0, 18, 8.5, 19.5, 0.5,
9, 23, 29, 22, 6.5, 26, 36.5, 39, 15, 12.5, 13.5, 30, 45, 16.5,
2, 0, 14, 20, 43, 0.5, 1, 1, 0, 0, 86, 106, 59, 0, 67, 83, 32,
83, 3, 4.5, 49, 9.5, 43, 20, 35, 16, 27, 7, 7, 26.5), height4mo = c(7,
11.5, 10, 8.5, 17, 6.5, 8.5, 26, 34, 33, 62.5, 51.5, 61, 42.5,
46.5, 29, 30, 11.5, 26.5, 20, 23, 29, 11.5, 21, 12.5, 25, 15.5,
24, 28.5, 37, 51, 46, 60.5, 55, 53, 25, 49, 56, 40, 43, 23, 8,
16, 8.5, 19.5, 2.5, 9.5, 22.5, 21, 20, 6.5, 31, 42, 45, 15, 17.5,
13.5, 36.5, 47, 20, 13, 18, 20, 27.5, 70, 31, 21, 25, 42, 12,
89, 128, 64, 45, 69, 84.5, 32, 86, 31, 31, 50, 10, 49.5, 20,
42.5, 15, 38, 7.5, 13, 33), PreBiomass = c(3.895575649, 2.834165975,
1.573928396, 1.163246736, 0.817978445, 0.317316605, 0.39770427,
0.817978445, 2.48217785, 17.85049586, 12.66281683, 11.31037967,
10.45107234, 6.662611474, 5.086139353, 5.691293433, 3.119036919,
1.167754804, 1.13272457, 5.847881474, 5.365833116, 3.999508626,
6.910020728, 3.038057965, 2.602293243, 4.127993434, 8.991590789,
3.038057965, 1.480678454, 1.823048057, 2.062949335, 1.18423519,
1.760986422, 1.004008739, 2.126354774, 0.524854338, 1.483984987,
1.908835132, 0.168869468, 0.540308647, 5.375747945, 2.053683314,
5.375747945, 4.65118718, 1.622061536, 1.072373896, 1.831323212,
2.537878044, 6.564139518, 4.827265975, 2.156129781, 5.153936858,
11.20247906, 6.684694751, 0.282366443, 4.694933633, 2.156129781,
12.74952183, 13.56745754, 3.465912243, 7.799750512, 9.849322745,
24.51741906, 4.912423703, 27.25487445, 2.476234093, 15.20382114,
22.9550974, 13.83353724, 1.831101565, 33.25213131, 139.5296871,
45.87772104, 90.79892694, 50.03156249, 90.79892694, 39.65094302,
9.671283982, 34.61929294, 81.54741169, 26.00946401, 14.01849263,
85.55945731, 0.667045516, 8.637292691, 6.550744926, 9.799815468,
3.084543759, 17.38618089, 9.403377144), Biomass1mo = c(0, 0,
0, 0, 0, 0, 0, 0.030124963, 0.090031144, 0.214679983, 0.698192549,
0.67114866, 0.75387857, 0.430024245, 0.408844585, 0.348506855,
0.388198419, 0.078012828, 0.430024245, 0.155212563, 0.296240906,
0.112200441, 0.26629734, 0, 0.112200441, 0.535234264, 0.071949318,
0.39259111, 0.091290587, 0.652064296, 0.094858153, 0.142288516,
0.304507388, 0.262240101, 0.122283739, 0.04961378, 0.132115845,
0.174825154, 0.276013506, 0.276013506, 0.022723782, 0.030242664,
0.082163804, 0.002963157, 0.174263551, 0.000819511, 0.124274142,
0.174263551, 0.566729877, 0.449345809, 0.01811283, 0.21782346,
0.57145776, 0.842645845, 0.116410145, 0.09891947, 0.154294203,
0.668793517, 0.635749905, 0.028252321, 0.005400266, 0, 0.308521938,
0, 0.101231381, 0, 0, 0, 0, 0, 3.291544691, 2.308284799, 0.468693323,
0, 0.958429423, 2.944402977, 0, 3.658082992, 0, 0, 1.524728852,
0.19037959, 0.528356631, 0.448087868, 1.886696465, 0.806223299,
2.284810808, 0.19037959, 0.10066607, 1.021875261), Biomass2mo = c(0.00461379,
0.048598634, 0.071833216, 0.090031144, 0.133074246, 0, 0.055807842,
0.536596536, 1.24010478, 0.726931224, 3.474265098, 2.569451142,
3.535326978, 1.800572943, 1.980042107, 0.841402428, 0.902414303,
0.130479556, 0.933718887, 0.430024245, 1.453050159, 1.347577447,
0.461969077, 0.26629734, 0.535234264, 1.616437082, 0.6927849,
1.453050159, 1.09644076, 2.456139386, 0.483312867, 1.0729993,
1.964067221, 1.703060052, 1.400530346, 0.304507388, 1.0729993,
1.703060052, 1.0222427, 1.177817154, 0.211862783, 0, 0.630095648,
0.156738681, 0.730913281, 0.000819511, 0.174263551, 0.992673892,
1.525735605, 0.914132292, 0.174609348, 1.487410339, 2.512370265,
2.7832013, 0.635749905, 0.479653397, 0.540228204, 1.855529897,
3.472016487, 0.736631727, 0.031533874, 0, 0.375407219, 0.591122274,
1.566167617, 0.005400266, 0.013049571, 0.013049571, 0, 0, 17.86670522,
27.17489168, 8.39141448, 0, 10.82904112, 16.63862372, 2.460045699,
16.63862372, 0.021336774, 0.048116873, 7.586947075, 0.339458495,
5.924289588, 1.390158514, 4.011712305, 0.911037264, 2.454089599,
0.19037959, 0.19037959, 2.368736511), Biomass4mo = c(0.041918611,
0.121473174, 0.090031144, 0.063551149, 0.280731338, 0.035762566,
0.063551149, 0.697852512, 1.24010478, 1.163246736, 4.175122771,
2.837608296, 3.977614516, 1.934377716, 2.314568827, 0.902414303,
0.96555584, 0.142578593, 0.75387857, 0.430024245, 1.672251324,
2.392140111, 0.573271986, 1.453050159, 0.652064296, 1.902090818,
0.909034181, 1.785867231, 2.328738868, 3.484981432, 1.831472627,
1.518350095, 2.498057072, 2.100814509, 1.964067221, 0.501385136,
1.703060052, 2.170736601, 1.177817154, 1.343229421, 0.992673892,
0.140073002, 0.506470737, 0.156738681, 0.730913281, 0.016205216,
0.192640481, 0.953030318, 0.838582725, 0.766045551, 0.174609348,
1.951972553, 3.12089825, 3.472016487, 0.635749905, 0.806750379,
0.540228204, 2.512370265, 3.713345005, 0.991640221, 0.341613273,
0.516931045, 0.591122274, 0.886590001, 2.912202284, 1.032643803,
0.628997882, 0.785297256, 1.519953371, 0.308521938, 19.13863569,
39.66748749, 9.878452915, 4.874133229, 11.48709241, 17.24718389,
2.460045699, 17.86670522, 2.308284799, 2.308284799, 7.882847273,
0.374087949, 7.734229305, 1.390158514, 5.794509429, 0.806223299,
4.687787249, 0.21695254, 0.614836496, 3.588685645), PreCarbon = c(0.156967627,
0.122843082, 0.075813652, 0.059169491, 0.044340523, 0.020426006,
0.024570672, 0.043002708, 0.106999734, 0.545053086, 0.320308244,
0.290501628, 0.271291986, 0.183819859, 0.145631674, 0.160452129,
0.095815131, 0.041269875, 0.0402085, 0.164611233, 0.206240523,
0.161958253, 0.253981948, 0.129206528, 0.113781088, 0.166223479,
0.31553078, 0.129335339, 0.071711875, 0.085049069, 0.09317797,
0.056638382, 0.080836829, 0.04885422, 0.09574618, 0.027353778,
0.069332271, 0.086903123, 0.009475442, 0.026773874, 0.160707434,
0.070160311, 0.160707434, 0.141844012, 0.057277905, 0.04013973,
0.063665515, 0.084295725, 0.191213272, 0.146673529, 0.04757003,
0.100344991, 0.195222186, 0.125396417, 0.008354372, 0.092637319,
0.04757003, 0.218128582, 0.230401206, 0.07162901, 0.131269005,
0.157389096, 0.320282156, 0.091653711, 0.347792395, 0.054313153,
0.221509506, 0.305003004, 0.205854943, 0.043576888, 1.197871953,
3.518128126, 1.526275089, 2.548979546, 1.629080226, 2.548979546,
1.367623475, 0.472267765, 1.236694558, 2.352499024, 0.638415034,
0.362069714, 1.904909524, 0.022514492, 0.232778297, 0.18076034,
0.261297274, 0.090855905, 0.441867498, 0.25160457), X1moCarbon = c(0,
0, 0, 0, 0, 0, 0, 0.002996291, 0.00733127, 0.014924131, 0.027262412,
0.026357968, 0.02910929, 0.018027068, 0.017267076, 0.015069346,
0.016555346, 0.004223662, 0.018064917, 0.007582482, 0.025240181,
0.011434171, 0.023138557, 0, 0.011434171, 0.040903752, 0.007959618,
0.031793903, 0.009674612, 0.048110681, 0.007167162, 0.009865614,
0.017984257, 0.015981995, 0.00875496, 0.004302588, 0.009305238,
0.011605185, 0.015676514, 0.015676514, 0.001435067, 0.001828411,
0.004266066, 0.000255474, 0.008072101, 8.6e-05, 0.006067683,
0.008083206, 0.022002661, 0.018064652, 0.000900408, 0.009931103,
0.025212867, 0.036698014, 0.005423393, 0.004634604, 0.007118678,
0.029351971, 0.028106107, 0.001391831, 0.000170474, 0, 0.00861976,
0, 0.002921775, 0, 0, 0, 0, 0, 0.145554027, 0.112095861, 0.034715829,
0, 0.058726823, 0.134088229, 0, 0.157321152, 0, 0, 0.046764496,
0.007082034, 0.017870499, 0.015406694, 0.056813876, 0.026251236,
0.067611463, 0.007089935, 0.003979541, 0.032552937), X2moCarbon = c(0.000649991,
0.004515538, 0.006213783, 0.007472947, 0.010284713, 0, 0.005055737,
0.031163252, 0.061950899, 0.03996902, 0.104905954, 0.080952912,
0.106489463, 0.0596727, 0.064736059, 0.031121663, 0.033108443,
0.006368256, 0.034087686, 0.017584511, 0.090428862, 0.085023956,
0.035457668, 0.022619457, 0.039985244, 0.098665234, 0.04936287,
0.090524095, 0.071905993, 0.139112044, 0.01893176, 0.035575351,
0.057471358, 0.051317148, 0.043939693, 0.013147169, 0.035575351,
0.051317148, 0.032779136, 0.036681952, 0.010956757, 0, 0.027658897,
0.008484002, 0.031380578, 9.89e-05, 0.009296871, 0.040783331,
0.058816936, 0.038019773, 0.008653254, 0.068600624, 0.113960148,
0.125843837, 0.030152569, 0.022965412, 0.025762347, 0.084974223,
0.156538314, 0.034969776, 0.000617961, 0, 0.006829998, 0.010617342,
0.027416033, 0.000112755, 0.000265127, 0.000265127, 0, 0, 0.893643875,
1.221422984, 0.50961417, 0, 0.615827082, 0.847505481, 0.205500657,
0.847505481, 0.006343264, 0.011521572, 0.304122678, 0.017974685,
0.242561906, 0.064815574, 0.170213577, 0.04413614, 0.108738758,
0.010654473, 0.010654473, 0.105287901), X4moCarbon = c(0.003939148,
0.009545784, 0.007472947, 0.005621946, 0.018936017, 0.003514856,
0.005621946, 0.038653227, 0.061950899, 0.058780944, 0.122873324,
0.088156055, 0.117853953, 0.063453978, 0.074010607, 0.03304018,
0.035078824, 0.006867286, 0.028392571, 0.017584511, 0.101443992,
0.135990762, 0.042290614, 0.090428862, 0.046980199, 0.112718863,
0.061628519, 0.10716323, 0.133173424, 0.185313121, 0.054366886,
0.046847602, 0.069583598, 0.060629528, 0.057471358, 0.019488753,
0.051317148, 0.062228442, 0.036681952, 0.040720029, 0.040718675,
0.007712115, 0.022971487, 0.008484002, 0.031380578, 0.001238836,
0.010122608, 0.039392597, 0.035328505, 0.032710879, 0.008653254,
0.089246941, 0.140615722, 0.155927128, 0.030152569, 0.037960536,
0.025762347, 0.113960148, 0.167057162, 0.046603193, 0.006232079,
0.009319662, 0.010617342, 0.01574909, 0.050249507, 0.018386847,
0.011359619, 0.014091865, 0.026775581, 0.005739173, 0.940599218,
1.618014419, 0.57521217, 0.340713733, 0.643414594, 0.870471728,
0.205500657, 0.893643875, 0.198091533, 0.198091533, 0.314956518,
0.01963135, 0.309519324, 0.064815574, 0.23812921, 0.039496022,
0.196215504, 0.011995076, 0.030876347, 0.153759364), TotalCarbon = c(2.501938424,
2.332218951, 1.641013423, 1.392483377, 1.696000406, 0.411835774,
0.764726634, 3.251928154, 6.361282467, 11.96169249, 13.62052751,
11.04045665, 12.83366357, 7.61659474, 7.576922118, 5.024502369,
4.227801803, 1.174993138, 3.259824259, 3.901744082, 10.5848291,
10.50668723, 7.021121066, 5.668839356, 5.08697083, 10.92840985,
8.922540693, 9.705419805, 8.451769698, 13.81683185, 3.988112769,
4.003878786, 6.156035357, 5.100700715, 5.268944019, 1.651130691,
4.319967871, 5.653749527, 2.952799027, 3.509044554, 4.146751849,
1.311194265, 4.40839747, 2.767792413, 3.333793425, 0.645000544,
1.768035403, 4.402711785, 6.904856277, 5.163288885, 1.376050648,
7.418577716, 12.65320411, 12.77220297, 2.468109142, 3.631338446,
2.75250667, 10.95485279, 15.93354872, 4.067048349, 2.1863628,
2.640426289, 5.559398908, 2.325058743, 8.001919262, 1.371376664,
3.675361853, 5.009731732, 3.891091568, 0.825828495, 88.5833406,
157.9578266, 63.60386651, 48.45610517, 72.33176226, 104.4979151,
35.92690142, 74.39089664, 24.77861122, 41.74870207, 33.41190899,
6.935077567, 49.04256584, 5.429985864, 19.14736986, 6.276180599,
15.71334028, 2.308491154, 8.093447265, 13.61309907), OLDestAssim = c(0.16273292,
0.531283865, 0.546784515, 0.548005831, 1.117252104, 0.114703735,
0.430112005, 2.958994249, 5.413171027, 4.46316534, 10.53485645,
8.109549494, 10.50791092, 5.898234837, 6.492409411, 3.283621134,
3.499740655, 0.718250337, 3.370987696, 1.789036071, 8.664887392,
9.086176264, 3.919440166, 4.123497491, 3.919792348, 9.950535196,
4.756772076, 9.060767572, 8.278498547, 14.54720467, 4.724107286,
5.766516948, 9.027313298, 7.989111013, 6.951312225, 2.28863022,
5.981065361, 7.901084227, 5.4728181, 6.020818486, 1.853280059,
0.311044291, 2.18794981, 0.68034361, 2.765418329, 0.047128668,
0.980573348, 3.453418537, 4.673671847, 3.476060731, 0.646316335,
5.778390351, 9.633196576, 10.92370219, 2.319186273, 2.209960947,
2.058639203, 7.773645484, 12.22756086, 2.852689634, 0.386565881,
0.496860166, 1.407968874, 1.680611096, 4.916640727, 0.977215919,
0.620253347, 0.763829673, 1.409081851, 0.302135427, 44.7470637,
63.92221417, 24.86052444, 6.076027823, 29.59811425, 41.96954834,
9.196665562, 43.0963223, 3.680815508, 3.819136463, 19.30141334,
1.332232454, 16.07431445, 4.286639367, 13.30975772, 3.365326795,
10.62150582, 0.898527189, 1.227305975, 8.317751517), CARBONmulti = c(2.621415393,
2.398836005, 1.641013423, 1.392483377, 1.696000406, 0.411835774,
0.809806973, 3.251928154, 6.361282467, 11.96169249, 13.62052751,
11.04045665, 12.83366357, 7.61659474, 7.576922118, 5.024502369,
4.227801803, 1.174993138, 4.510076237, 3.901744082, 18.5717312,
10.50668723, 7.021121066, 8.059558949, 5.08697083, 10.92840985,
11.70126006, 16.07362835, 9.527447818, 21.36601039, 4.377265297,
4.003878786, 6.156035357, 6.514095673, 5.268944019, 2.169858768,
4.319967871, 5.653749527, 2.952799027, 4.588995837, 4.705122057,
1.542557708, 6.195249968, 2.767792413, 3.480946773, 0.645000544,
1.768035403, 4.961955587, 6.904856277, 5.163288885, 1.765904086,
7.418577716, 12.65320411, 12.77220297, 2.468109142, 3.631338446,
3.441469031, 15.95980398, 15.93354872, 4.067048349, 2.1863628,
2.640426289, 6.723791976, 2.325058743, 8.001919262, 1.857730159,
3.675361853, 5.330578088, 3.891091568, 0.825828495, 88.5833406,
157.9578266, 63.60386651, 48.45610517, 82.73192555, 106.3419007,
35.92690142, 74.66251592, 24.77861122, 41.74870207, 35.0178608,
6.935077567, 49.04256584, 5.429985864, 19.14736986, 6.276180599,
23.40428597, 2.308491154, 8.093447265, 13.61309907), RecBiomass = c(0.010760569,
0.042860289, 0.05720155, 0.054632562, 0.343201388, 0.112703103,
0.159794989, 0.853142911, 0.499603516, 0.065166074, 0.329715167,
0.250885327, 0.380593913, 0.290333261, 0.455073813, 0.158560495,
0.309568583, 0.122096345, 0.665544467, 0.073535048, 0.311648031,
0.598108502, 0.082962412, 0.478282566, 0.25057295, 0.460778547,
0.10109826, 0.587831849, 1.572751236, 1.911623458, 0.887793314,
1.282135599, 1.418555556, 2.092426518, 0.923678046, 0.955284351,
1.1476262, 1.137204866, 6.974719413, 2.48604095, 0.184657819,
0.068205746, 0.094214004, 0.03369864, 0.450607616, 0.015111536,
0.105191962, 0.375522504, 0.127752118, 0.158691391, 0.080982764,
0.378734278, 0.278589965, 0.519397911, 2.25150658, 0.171834246,
0.250554586, 0.197056039, 0.273694979, 0.286112328, 0.043797974,
0.052483918, 0.024110298, 0.180479139, 0.106850695, 0.417021883,
0.041371039, 0.034210147, 0.109874528, 0.168489801, 0.575561173,
0.284294248, 0.215321352, 0.053680516, 0.229596915, 0.189949204,
0.062042552, 1.847397435, 0.066676255, 0.028306046, 0.303076114,
0.026685319, 0.09039596, 2.084053457, 0.670871028, 0.123073529,
0.478354645, 0.070335374, 0.035363517, 0.381637957), RecovCarbonMULTI = c(0.672921188,
0.846399267, 1.042622667, 1.197066224, 2.073404766, 1.297870225,
2.03620387, 3.975567052, 2.562782706, 0.670104214, 1.075631725,
0.976134929, 1.227975767, 1.143184586, 1.489719725, 0.882840154,
1.355483091, 1.006198505, 3.981617736, 0.667206423, 3.461108611,
2.626994517, 1.016078148, 2.65286543, 1.954803074, 2.647390319,
1.301355937, 5.290757627, 6.434515065, 11.7199381, 2.121848182,
3.380982781, 3.495788088, 6.488086629, 2.477923291, 4.134211359,
2.911059012, 2.961884676, 17.4856892, 8.493285951, 0.875249752,
0.751117613, 1.15244428, 0.595072248, 2.146001675, 0.601469829,
0.965441486, 1.955159193, 1.051905777, 1.069609363, 0.819015674,
1.439400194, 1.129500358, 1.910663604, 8.740801894, 0.773458952,
1.596132599, 1.251796278, 1.174394589, 1.173442391, 0.280311889,
0.268082015, 0.274245505, 0.473301752, 0.293595895, 0.750223965,
0.241739351, 0.23221762, 0.281279582, 0.451000923, 2.663989859,
1.132073252, 1.386378073, 0.533663853, 1.65359468, 1.171180148,
0.906079369, 7.72002105, 0.715745734, 0.511956188, 1.346350728,
0.494709221, 0.573198655, 8.14035284, 2.216825404, 0.958086549,
2.388237416, 0.748406032, 0.465510357, 1.447681919), LogRespBiomass = c(-3.172025374,
-2.108061831, -2.407599624, -2.755910201, -1.27035716, -3.330853575,
-2.755910201, -0.359747499, 0.215195876, 0.151215006, 1.429143764,
1.042961548, 1.380682272, 0.659785681, 0.839223418, -0.102681548,
-0.035051343, -1.947861901, -0.282523972, -0.843913688, 0.514170817,
0.872188409, -0.556395005, 0.373664905, -0.427612108, 0.642953712,
-0.095372583, 0.579904141, 0.845326863, 1.248462716, 0.605120357,
0.417624281, 0.915513259, 0.742325131, 0.675017436, -0.690380739,
0.532426664, 0.775066557, 0.163662856, 0.29507673, -0.007353076,
-1.965591549, -0.680288732, -1.853175313, -0.313460457, -4.122422113,
-1.64692962, -0.048108563, -0.176042044, -0.266513645, -1.745204097,
0.668840427, 1.138120861, 1.244735545, -0.452950024, -0.214740978,
-0.615763629, 0.921226636, 1.311933089, -0.008394918, -1.074075962,
-0.659845789, -0.52573239, -0.120372635, 1.068909594, 0.032122313,
-0.46362739, -0.241692963, 0.418679657, -1.175962326, 2.951709103,
3.680531897, 2.290355912, 1.583942289, 2.441224006, 2.847648877,
0.900179927, 2.882938937, 0.836504737, 0.836504737, 2.064689168,
-0.983264351, 2.045655842, 0.329417779, 1.756910819, -0.215394529,
1.544960669, -1.528076659, -0.486398907, 1.27778602), LogPreBiomass = c(1.35984146,
1.041747705, 0.453574657, 0.151215006, -0.200919294, -1.14785525,
-0.92204659, -0.200919294, 0.90913634, 2.882031287, 2.53866989,
2.425720859, 2.346704589, 1.896511521, 1.626519066, 1.738937539,
1.137524274, 0.155082934, 0.124625855, 1.766079454, 1.680051651,
1.38617151, 1.932972637, 1.111218484, 0.956393073, 1.417791438,
2.196289784, 1.111218484, 0.392500398, 0.600509857, 0.724136675,
0.169097157, 0.565874119, 0.004000725, 0.75440914, -0.644634506,
0.394731028, 0.646493177, -1.778629241, -0.615614734, 1.681897717,
0.719634919, 1.681897717, 1.537122495, 0.483697893, 0.069874785,
0.605038772, 0.931328316, 1.881621428, 1.574280257, 0.768314847,
1.639760861, 2.416135098, 1.899820548, -1.264549608, 1.546483977,
0.768314847, 2.545493767, 2.607674097, 1.242975872, 2.054091748,
2.287402696, 3.199383847, 1.591767446, 3.305232384, 0.906738895,
2.721546787, 3.133540021, 2.627095879, 0.604917734, 3.504118865,
4.938277389, 3.825979619, 4.508647468, 3.912654056, 4.508647468,
3.680114731, 2.269161081, 3.544411126, 4.40118459, 3.258460472,
2.64037736, 4.449211541, -0.404896996, 2.156089188, 1.879578772,
2.282363556, 1.126403756, 2.855675689, 2.241068895), LogRecBio = c(-4.531866845,
-3.149809546, -2.861174283, -2.9071252, -1.069437867, -2.182998325,
-1.833863604, -0.158828206, -0.693940463, -2.730816283, -1.109526128,
-1.382759309, -0.966022317, -1.23672584, -0.787295647, -1.841619087,
-1.172575619, -2.102944833, -0.407149826, -2.609993143, -1.165880834,
-0.5139831, -2.489367641, -0.737553579, -1.384005183, -0.774837727,
-2.291662364, -0.531314343, 0.452826465, 0.647952859, -0.119016318,
0.248527124, 0.34963914, 0.738324406, -0.079391703, -0.045746233,
0.137695635, 0.12857338, 1.942292099, 0.910691465, -1.689250794,
-2.685226465, -2.362186446, -3.390297799, -0.797158349, -4.192296853,
-2.251968388, -0.979436879, -2.057663471, -1.8407939, -2.513518937,
-0.970920433, -1.278014238, -0.655085002, 0.811599583, -1.761224953,
-1.384078474, -1.624267129, -1.295741008, -1.25137079, -3.128167718,
-2.94724848, -3.725116227, -1.712140081, -2.236322793, -0.874616581,
-3.185174184, -3.375232983, -2.208416219, -1.780880059, -0.552409761,
-1.257745493, -1.535623706, -2.924705174, -1.471430051, -1.66099859,
-2.779934807, 0.613777857, -2.707906386, -3.564679858, -1.193771304,
-3.623641715, -2.403555703, 0.734314775, -0.399178369, -2.094973305,
-0.737402886, -2.65448042, -3.342074583, -0.963282876), LogCarbAss = c(0.963714398,
0.874983622, 0.495313992, 0.331088755, 0.528272777, -0.887130616,
-0.210959365, 1.179248099, 1.850230003, 2.481709251, 2.611578031,
2.401566403, 2.552071685, 2.030329385, 2.025107064, 1.614326418,
1.44168219, 0.161262308, 1.506314057, 1.361423654, 2.921640597,
2.352011933, 1.948922901, 2.086858834, 1.626682532, 2.391365807,
2.459696533, 2.777179938, 2.254176877, 3.06180136, 1.476424168,
1.387263588, 1.817432959, 1.873968394, 1.661829967, 0.774662082,
1.463247965, 1.732318958, 1.082753543, 1.523661228, 1.548651715,
0.433441888, 1.823782864, 1.01805004, 1.247304318, -0.438504119,
0.569868988, 1.601799935, 1.932224972, 1.641573757, 0.568662789,
2.003987356, 2.537910472, 2.547271167, 0.903452328, 1.289601298,
1.235898424, 2.77007331, 2.768426869, 1.402917515, 0.782239341,
0.970940377, 1.905652278, 0.843745305, 2.079681421, 0.619355398,
1.301651591, 1.673459692, 1.358689727, -0.19136816, 4.483943811,
5.062328077, 4.152674263, 3.88065834, 4.415605568, 4.666659382,
3.581486358, 4.312978171, 3.20998083, 3.731668363, 3.55585824,
1.936592239, 3.892688612, 1.691936531, 2.952165362, 1.836761611,
3.152919167, 0.836594131, 2.091054755, 2.611032496), LogRecCarb = c(-0.396127062,
-0.166764084, 0.041739334, 0.17987375, 0.729192071, 0.260724633,
0.711087226, 1.380167393, 0.941093663, -0.400322035, 0.07290814,
-0.024154455, 0.205367096, 0.133817864, 0.398587998, -0.124611121,
0.304157916, 0.006179373, 1.381688203, -0.404655801, 1.241588946,
0.965840424, 0.015950264, 0.97564035, 0.670289459, 0.973574369,
0.263406749, 1.665961454, 1.861676479, 2.461291503, 0.752287493,
1.218166431, 1.25155884, 1.869967669, 0.907420827, 1.419296587,
1.068516937, 1.085825781, 2.861382786, 2.139275963, -0.133246002,
-0.286193031, 0.141885148, -0.519072456, 0.763606425, -0.508378904,
-0.035169784, 0.670471619, 0.050603545, 0.067293501, -0.199652057,
0.364226495, 0.121775374, 0.647450618, 2.168001935, -0.256882678,
0.467583578, 0.224579542, 0.160752771, 0.159941643, -1.271852406,
-1.316462319, -1.29373157, -0.74802214, -1.225550964, -0.287383497,
-1.419895195, -1.46008033, -1.268406151, -0.796285893, 0.979824946,
0.124050688, 0.326694644, -0.627989127, 0.502951512, 0.158011914,
-0.098628373, 2.043817091, -0.334430295, -0.669516228, 0.297397768,
-0.703785121, -0.55652293, 2.096833526, 0.796076174, -0.042817162,
0.870555611, -0.289809625, -0.764620933, 0.369963601)), .Names = c("Treatment",
"Species", "Tag", "site", "stem", "heightPre", "height1mo", "height2mo",
"height4mo", "PreBiomass", "Biomass1mo", "Biomass2mo", "Biomass4mo",
"PreCarbon", "X1moCarbon", "X2moCarbon", "X4moCarbon", "TotalCarbon",
"OLDestAssim", "CARBONmulti", "RecBiomass", "RecovCarbonMULTI",
"LogRespBiomass", "LogPreBiomass", "LogRecBio", "LogCarbAss",
"LogRecCarb"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 46L,
47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 105L, 106L, 107L,
108L, 109L, 110L, 111L, 112L, 113L, 114L, 123L, 124L, 125L, 126L,
127L, 128L, 129L, 130L, 131L, 132L, 172L, 173L, 174L, 175L, 176L,
177L, 178L, 179L, 180L, 181L, 227L, 228L, 229L, 230L, 231L, 232L,
233L, 234L, 235L, 236L, 255L, 256L, 257L, 258L, 259L, 260L, 261L,
262L, 263L, 264L, 290L, 291L, 292L, 293L, 294L, 295L, 296L, 297L,
298L, 299L), class = "data.frame")
This should work with the simple graphics package. You need to add the real scientific names, in the same order as they are listed in species. If you want a different order, you can manually set the species too.
species = unique(carbon2$Species) # c("Acru", "Arte", ...)
bin.name = c("Acer rubrum", "Arundinaria tecta", "Clethra alnifolia",
"Eupatorium rotundifolium", "Gaylussacia frondosa",
"Ilex glabra", "Lyonia lucida", "Oxydendrum arboreum",
"Persea palustris")
sites = c(7,8) # sites to use
color = c("blue", "green") # colors for each site
fit.bio = list() # list to save all models
par(mfrow=c(3,3), cex=.3)
for(isp in seq_along(species)) {
sp.data = carbon2[carbon2$Species==species[isp], ]
fit.bio[[isp]] <- lm(LogRecBio ~ LogPreBiomass, data=sp.data)
sp.label = paste(letters[isp], ") ", bin.name[isp], sep="")
sp.r.squared = paste("R²", "=", round(summary(fit.bio[[isp]])$adj.r.squared, 3))
plot(LogRecBio ~ LogPreBiomass, data=sp.data,
font.lab=2,font.main=2, font.sub=3, ylim=c(-5,4),
mgp=c(2.5,1,0), cex.lab=2, cex.main=1.5,
axes=FALSE, lwd=1.5, cex=2.5, ann=FALSE)
for(isite in seq_along(sites)) {
# loop for each selected site
site.data = sp.data[sp.data$site==sites[isite], ]
site.model = lm(LogRecBio ~ LogPreBiomass, data=site.data)
abline(site.model, lwd=2, col=color[isite])
}
abline(fit.bio[[isp]], lwd=2, col='red') # all data
axis(1,at=c(-2,-1,0,1,2,3,4),font=2,cex.axis=3)
axis(2,at=c(-5,-4,-3,-2,-1,0,1,2),font=2,cex.axis=3)
box()
mtext(sp.label, 3, line=-2.5, adj=0.05, font=3, cex=0.75)
mtext(sp.r.squared, 3, line=-2.5, adj=0.95, cex=0.75)
}
In reply to the comments, this is how your code would look like if you used ggplot2.
DF<-read.csv("test.csv")
require(ggplot2)
qplot(data=DF,x=LogPreBiomass,y=LogRecBio,facets=~Species)
I saved your sample as a .csv file named test.csv.Does this work for you?
Related
For my current projects I'm repeatedly specifying regression models with differing amounts of predictors/covariates on different outcomes. Right now I'm just writing out each model in full, but I'm sure there is a (very much) faster way requiring less code to do what I'm doing.
My example data is repeated measurements dataset of 24 stroke patients in which I assess the effect of three different types of rehabilitation (Group) on functional recovery scores (Outcome 1 to Outcome 4). Each patients functional ability was measured weekly (Time_num) for 8 weeks:
library(tidyverse)
library(magrittr)
library(nlme)
mydata <- structure(list(Subject = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L), Age = c(60, 52.5, 57.1, 63, 65.1, 39, 59.3, 65.3,
61.4, 56.3, 46.4, 58.2, 58, 57.7, 56.6, 42.3, 52.5, 51.8, 43.2,
50.9, 56.7, 67.5, 65, 56.5, 65.5, 45.6, 56.7, 47.9, 65.5, 46.6,
68.6, 52.1, 43.1, 62.1, 62.9, 58.3, 49.6, 42.1, 59.7, 62.9, 56.2,
71.7, 60.5, 59.8, 54.3, 76.1, 56.2, 74.3, 48.7, 69.9, 59.6, 58.4,
55.9, 56.5, 33, 57.1, 63, 53.1, 51.3, 46.9, 57.2, 47, 58, 63.7,
69.8, 57.9, 62.7, 44.8, 51.5, 57, 58.1, 53.3, 57.2, 54.2, 50.2,
60.4, 61.1, 81.3, 59.6, 68.8, 49.2, 51, 53.5, 55.9, 66.7, 60.3,
59.8, 61.6, 63.8, 59.8, 55.5, 57.7, 66.3, 54.7, 56.3, 56.7, 57.7,
63.8, 53.5, 56.1, 49, 44.5, 36, 58.2, 50.8, 56.8, 47.9, 51.1,
53.2, 53.4, 59.3, 42.8, 63.6, 51.2, 49, 62.6, 44.8, 59.9, 44.7,
56, 54.3, 58.7, 62.2, 76.7, 31.4, 65.2, 52.8, 56.7, 52.4, 60.6,
54.8, 43.2, 77.6, 58.1, 49.8, 55.2, 53.6, 54.1, 72.9, 58.7, 51.9,
64.9, 56.6, 61, 71.3, 63.1, 57.4, 56.9, 53.8, 73, 58.9, 60.7,
63.8, 54.6, 74.5, 46.7, 44.2, 56.3, 66.8, 56.5, 43.6, 62.8, 55.3,
53.7, 54.9, 46.6, 51.8, 60.7, 62.9, 61.5, 61.6, 43.6, 66.8, 50.1,
51.6, 69.9, 52.2, 58.1, 62.1, 69.2, 59.1, 55.2, 47.2, 64.5, 54.2,
75.9, 52.9, 62.5, 58, 64.5, 70.7, 60.5), Sex = structure(c(1L,
2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L,
1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L,
1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2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3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A",
"B", "C"), class = "factor"), Time_num = c(1, 2, 3, 4, 5, 6,
7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3,
4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5,
6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2,
3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7,
8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4,
5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1,
2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6,
7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8), First_outcome = c(45L,
45L, 45L, 45L, 80L, 80L, 80L, 90L, 20L, 25L, 25L, 25L, 30L, 35L,
30L, 50L, 50L, 50L, 55L, 70L, 70L, 75L, 90L, 90L, 25L, 25L, 35L,
40L, 60L, 60L, 70L, 80L, 100L, 100L, 100L, 100L, 100L, 100L,
100L, 100L, 20L, 20L, 30L, 50L, 50L, 60L, 85L, 95L, 30L, 35L,
35L, 40L, 50L, 60L, 75L, 85L, 30L, 35L, 45L, 50L, 55L, 65L, 65L,
70L, 40L, 55L, 60L, 70L, 80L, 85L, 90L, 90L, 65L, 65L, 70L, 70L,
80L, 80L, 80L, 80L, 30L, 30L, 40L, 45L, 65L, 85L, 85L, 85L, 25L,
35L, 35L, 35L, 40L, 45L, 45L, 45L, 45L, 45L, 80L, 80L, 80L, 80L,
80L, 80L, 15L, 15L, 10L, 10L, 10L, 20L, 20L, 20L, 35L, 35L, 35L,
45L, 45L, 45L, 50L, 50L, 40L, 40L, 40L, 55L, 55L, 55L, 60L, 65L,
20L, 20L, 30L, 30L, 30L, 30L, 30L, 30L, 35L, 35L, 35L, 40L, 40L,
40L, 40L, 40L, 35L, 35L, 35L, 40L, 40L, 40L, 45L, 45L, 45L, 65L,
65L, 65L, 80L, 85L, 95L, 100L, 45L, 65L, 70L, 90L, 90L, 95L,
95L, 100L, 25L, 30L, 30L, 35L, 40L, 40L, 40L, 40L, 25L, 25L,
30L, 30L, 30L, 30L, 35L, 40L, 15L, 35L, 35L, 35L, 40L, 50L, 65L,
65L), Second_outcome = c(3, 50, 7, 43, -23, 32, 48, 46, 32, 46,
23, 34, 46, -2, 46, 49, 45, 44, 53, 1, 61, 23, 41, 52, 25, 54,
26, -1, 22, 50, 21, 20, 70, 62, 67, 18, 55, 25, 5, 16, 43, 35,
59, 5, -5, 50, 35, 32, 25, 25, 13, 57, 42, 21, 35, 34, 38, 52,
63, 52, 44, 36, 32, 30, 26, 42, 44, 53, 39, 29, 13, 37, 41, 31,
18, 41, 40, 29, 28, 22, 6, -15, 16, 26, 0, 41, 35, 28, 35, 32,
41, 49, 16, 43, 56, 63, 14, 46, 43, 46, 36, -3, 49, 33, 49, 20,
20, 31, 27, 23, 34, 36, 39, 20, 29, 58, 45, 60, 40, 17, 77, 45,
13, 62, 43, 74, 47, 56, 13, 12, 36, 2, 40, 57, 35, 31, 28, 82,
49, 6, 10, 46, 49, 17, 55, 16, 12, -17, -7, 22, 20, -14, 21,
17, 41, 47, 25, 34, 72, 59, 26, 24, 46, 16, 35, 34, 51, 40, 25,
53, 24, 14, 66, 18, 18, 34, 29, 81, 12, 50, 55, 33, 62, 38, 24,
25, 29, 60, 71, -6, 60, 49), Third_outcome = c(87, 78, 94, 93,
78, 84, 72, 81, 82, 81, 86, 72, 80, 82, 77, 82, 79, 71, 82, 79,
86, 86, 76, 73, 80, 74, 81, 73, 81, 80, 65, 84, 73, 85, 87, 78,
77, 70, 85, 80, 77, 73, 75, 85, 67, 87, 90, 84, 71, 73, 81, 72,
74, 74, 85, 90, 75, 70, 81, 69, 81, 73, 79, 74, 76, 77, 82, 80,
87, 87, 82, 81, 76, 80, 79, 71, 81, 77, 74, 78, 73, 79, 77, 78,
94, 78, 71, 82, 81, 80, 79, 70, 68, 82, 78, 68, 66, 82, 80, 71,
73, 79, 83, 71, 80, 78, 82, 73, 86, 76, 75, 81, 84, 84, 85, 80,
83, 79, 75, 77, 82, 89, 78, 74, 79, 82, 73, 86, 77, 81, 84, 84,
73, 80, 82, 81, 81, 83, 81, 79, 84, 82, 75, 75, 80, 67, 81, 82,
82, 80, 80, 80, 76, 81, 82, 85, 86, 81, 89, 78, 84, 79, 80, 77,
85, 88, 78, 81, 82, 81, 82, 77, 74, 86, 81, 73, 80, 77, 81, 76,
83, 76, 81, 79, 76, 83, 77, 79, 71, 77, 82, 87), Fourth_outcome = c(59,
36, 53, 51, 59, 50, 56, 57, 52, 42, 60, 44, 46, 52, 54, 68, 63,
37, 51, 46, 67, 42, 63, 47, 41, 48, 51, 48, 51, 34, 35, 46, 52,
52, 44, 67, 47, 58, 57, 55, 50, 56, 36, 42, 51, 51, 42, 49, 59,
55, 44, 53, 42, 64, 75, 64, 41, 44, 39, 64, 40, 48, 51, 54, 42,
52, 35, 55, 53, 66, 34, 50, 56, 35, 32, 63, 52, 35, 63, 38, 57,
67, 35, 41, 47, 31, 55, 60, 52, 60, 44, 52, 63, 53, 48, 69, 43,
44, 40, 45, 63, 39, 48, 56, 44, 57, 56, 62, 54, 49, 47, 62, 41,
41, 59, 32, 62, 39, 64, 46, 44, 78, 68, 38, 51, 27, 57, 55, 67,
51, 44, 61, 24, 49, 62, 61, 43, 41, 54, 47, 41, 28, 40, 31, 57,
58, 36, 48, 58, 61, 67, 50, 47, 56, 56, 69, 43, 43, 58, 55, 48,
52, 46, 51, 38, 58, 44, 43, 49, 59, 31, 37, 46, 55, 45, 50, 45,
67, 48, 37, 51, 47, 66, 42, 52, 46, 61, 47, 34, 49, 58, 38)), row.names = c(NA,
-192L), class = c("tbl_df", "tbl", "data.frame"))
Which looks as follows:
head(mydata)
# A tibble: 6 x 9
Subject Age Sex Group Time_num First_outcome Second_outcome Third_outcome Fourth_outcome
<int> <dbl> <fct> <fct> <dbl> <int> <dbl> <dbl> <dbl>
1 1 60 Male A 1 45 3 87 59
2 1 52.5 Female A 2 45 50 78 36
3 1 57.1 Female A 3 45 7 94 53
4 1 63 Male A 4 45 43 93 51
5 1 65.1 Male A 5 80 -23 78 59
6 1 39 Female A 6 80 32 84 50
The models that I run now are 2 linear mixed effects models per outcome (using nlme::lme): one just containing Group and one additionally containing Age and Sex. How I do this now is:
# Outcome 1
outcome1_modelA <-
lme(fixed=First_outcome ~ 1 + Time_num*Group,
random= ~1 + Time_num|Subject,
data=mydata,
na.action="na.omit",
method="ML")
outcome1_modelB <-
lme(fixed=First_outcome ~ 1 + Time_num*Group + Time_num*Age + Time_num*Sex,
random= ~1 + Time_num|Subject,
data=mydata,
na.action="na.omit",
method="ML")
# Outcome 2, 3, and finally...
# Outcome 4
outcome4_modelA <-
lme(fixed=Fourth_outcome ~ 1 + Time_num*Group,
random= ~1 + Time_num|Subject,
data=mydata,
na.action="na.omit",
method="ML")
outcome4_modelB <-
lme(fixed=Fourth_outcome ~ 1 + Time_num*Group + Time_num*Age + Time_num*Sex,
random= ~1 + Time_num|Subject,
data=mydata,
na.action="na.omit",
method="ML")
But seeing as I've got even more outcomes and also more models, I'd like to learn a way to make my code more efficient. I've read about for-loops but can't seem to find examples that work for me. Solutions not involving for-loops would also be greatly appreciated!
Create a function to do it with parameters for the parts you want to change - you'll need the as.formula() function
my_model_function <- function(x, y){
fixed_effects <- as.formula(paste(y, "~ 1 +",
paste("Time_num", x, sep="*", collapse=" + ")))
lme(fixed=fixed_effects,
random= ~1 + Time_num|Subject,
data=mydata,
na.action="na.omit",
method="ML")
}
outcome1_modelA <- my_model_function(x = "Group",
y = "First_outcome")
outcome1_modelB <- my_model_function(x = c("Group", "Age", "Sex"),
y = "First_outcome")
To make it more automated then you can created nested loops in lapply()which will return nested lists of your model outputs.
x_values <- list("Group", c("Group", "Age", "Sex"))
y_values <- list("First_outcome", "Second_outcome")
lapply(x_values, function(x_value){
lapply(y_values, function(y_value){
my_model_function(x_value, y_value)
})
})
You could also replace the user defined function here too - really no need to make a function as this allows it to be called repeatedly from one piece of code (I've left it in because I am writing on a phone, it's too much editing, but something akin to)
lapply(list(1,2,3), function(i){
i^2
})
I am working to generate a heatmap of the distribution of biological functional classes by tissue type for an analysis that I'm working on. I've successfully generated the heatmap using geom_tile, but would like to maintain the grid within the white space that is generated in the heatmap.
This white space is generated because there are no data in those comparisons (not NAs or zeros, but completely absent). Is it possible to either 1) edit the graphics to include the grid over the white space, or 2) edit the data frame to include NA's or zeros where those data are currently absent?
Here are my data:
structure(list(Tissue = 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, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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,
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), .Label = c("FB",
"SOG", "MG", "HG", "MT"), class = "factor"), Transcript_Count = c(64,
36, 35, 42, 66, 122, 62, 40, 34, 46, 40, 36, 41, 37, 36, 37,
40, 35, 38, 40, 53, 37, 36, 36, 68, 40, 40, 116, 84, 149, 45,
72, 42, 65, 78, 37, 62, 35, 35, 43, 38, 152, 37, 60, 36, 66,
40, 60, 45, 35, 36, 35, 129, 193, 153, 420, 247, 357, 237, 343,
199, 484, 112, 464, 244, 150, 127, 151, 247, 152, 238, 246, 127,
127, 120, 182, 245, 128, 388, 279, 246, 139, 120, 120, 120, 146,
119, 143, 144, 133, 126, 133, 143, 143, 218, 131, 121, 120, 119,
124, 127, 119, 124, 124, 119, 224, 306, 387, 102, 108, 122, 136,
186, 373, 85, 151, 156, 83, 161, 127, 286, 135, 82, 180, 150,
158, 157, 76, 142, 95, 79, 81, 78, 79, 77, 183, 88, 99, 189,
356, 162, 150, 125, 110, 96, 98, 88, 91, 100, 93, 101, 150, 90,
88, 193, 96, 100, 336, 275, 410, 108, 225, 103, 187, 237, 90,
163, 131, 100, 92, 427, 90, 171, 88, 190, 102, 175, 109, 107,
80, 97, 87, 72, 256, 185, 144, 266, 233, 150, 83, 106, 133, 133,
133, 69, 217, 70, 134, 131, 101, 121, 58, 67, 65, 61, 58, 64,
64, 64, 65, 58, 57), GO.ID = structure(c(1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 19L, 9L, 10L, 6L, 37L,
35L, 8L, 29L, 39L, 42L, 53L, 30L, 34L, 31L, 22L, 49L, 25L, 21L,
1L, 46L, 43L, 36L, 12L, 48L, 5L, 41L, 28L, 32L, 7L, 40L, 23L,
15L, 18L, 33L, 38L, 20L, 47L, 26L, 54L, 11L, 27L, 17L, 44L, 13L,
14L, 51L, 3L, 24L, 16L, 52L, 2L, 45L, 50L, 29L, 6L, 42L, 9L,
39L, 8L, 37L, 35L, 30L, 10L, 1L, 34L, 49L, 25L, 21L, 28L, 7L,
31L, 32L, 48L, 46L, 5L, 27L, 44L, 4L, 47L, 40L, 17L, 33L, 20L,
1L, 2L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L, 14L, 16L, 17L,
19L, 20L, 21L, 22L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L,
33L, 34L, 35L, 36L, 37L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L,
47L, 48L, 49L, 37L, 9L, 8L, 39L, 10L, 30L, 29L, 35L, 42L, 6L,
32L, 21L, 7L, 5L, 25L, 34L, 31L, 28L, 49L, 46L, 1L, 48L, 44L,
11L, 40L, 47L, 55L, 26L, 27L, 17L, 20L, 33L, 13L, 16L), .Label = c("GO:0006139",
"GO:0006351", "GO:0006355", "GO:0006508", "GO:0006725", "GO:0006807",
"GO:0006810", "GO:0007154", "GO:0007165", "GO:0009058", "GO:0009059",
"GO:0009889", "GO:0010467", "GO:0010468", "GO:0010556", "GO:0016070",
"GO:0018130", "GO:0019219", "GO:0019222", "GO:0019438", "GO:0019538",
"GO:0031323", "GO:0031326", "GO:0032774", "GO:0034641", "GO:0034645",
"GO:0034654", "GO:0043170", "GO:0044237", "GO:0044238", "GO:0044249",
"GO:0044260", "GO:0044271", "GO:0046483", "GO:0050794", "GO:0051171",
"GO:0051234", "GO:0051252", "GO:0051716", "GO:0055085", "GO:0060255",
"GO:0071704", "GO:0080090", "GO:0090304", "GO:0097659", "GO:1901360",
"GO:1901362", "GO:1901564", "GO:1901576", "GO:1903506", "GO:2000112",
"GO:2001141", "GO:0003008", "GO:0006811", "GO:0006259"), class = "factor")), row.names = c(NA,
-212L), class = "data.frame")
And my code to generate the heatmap:
(ggplot(All_Tissues_BP_Head, aes(Tissue, GO.ID)) +
Alex_Theme +
geom_tile(aes(fill = Transcript_Count), color = "black") +
scale_fill_gradient2(low = "white", mid = "blue", high= "black",
midpoint = mean(All_Tissues_BP$Transcript_Count)) +
scale_x_discrete(expand = c(0,0)) +
ggtitle(expression(atop(bold("Biological Processes")))) +
theme(legend.title = element_text(size=12),
legend.text = element_text(size=12)) +
theme(axis.text = element_text(size=12),
axis.title.y = element_blank(),
axis.title.x = element_text(size = 12)) +
labs(fill = "Transcript \n count"))
Use the complete function from tidyr to fill in missing factor combinations in your data.frame with NA.
Then you can use the na.value parameter in the color gradient to set the color.
library(ggplot2)
library(dplyr)
library(tidyr)
# z <- complete(All_Tissues_BP_Head, Tissue, GO.ID)
ggplot(complete(All_Tissues_BP_Head, Tissue, GO.ID), aes(Tissue, GO.ID)) +
geom_tile(aes(fill = Transcript_Count), color = "black") +
scale_fill_gradient2(low = "white", mid = "blue", high= "black",
midpoint = mean(All_Tissues_BP_Head$Transcript_Count), na.value="black") +
scale_x_discrete(expand = c(0,0)) +
ggtitle(expression(atop(bold("Biological Processes")))) +
theme(legend.title = element_text(size=12),
legend.text = element_text(size=12)) +
theme(axis.text = element_text(size=12),
axis.title.y = element_blank(),
axis.title.x = element_text(size = 12)) +
labs(fill = "Transcript \n count")
I'm working on a heatmap and following along this tutorial:
https://www.r-graph-gallery.com/283-the-hourly-heatmap/
To save a click, here's the code block to reproduce:
library(ggplot2)
library(dplyr) # easier data wrangling
library(viridis) # colour blind friendly palette, works in B&W also
library(Interpol.T) # will generate a large dataset on initial load
library(lubridate) # for easy date manipulation
library(ggExtra) # because remembering ggplot theme options is beyond me
library(tidyr)
data<- data(Trentino_hourly_T,package = "Interpol.T")
names(h_d_t)[1:5]<- c("stationid","date","hour","temp","flag")
df<- tbl_df(h_d_t) %>%
filter(stationid =="T0001")
df<- df %>% mutate(year = year(date),
month = month(date, label=TRUE),
day = day(date))
df$date<-ymd(df$date) # not necessary for plot but
#useful if you want to do further work with the data
#cleanup
rm(list=c("h_d_t","mo_bias","Tn","Tx",
"Th_int_list","calibration_l",
"calibration_shape","Tm_list"))
#create plotting df
df <-df %>% select(stationid,day,hour,month,year,temp)
Then a heatmap is made:
p <-ggplot(df,aes(day,hour,fill=temp))+
geom_tile(color= "white",size=0.1) +
scale_fill_viridis(name="Hrly Temps C",option ="C")
p <-p + facet_grid(year~month)
p <-p + scale_y_continuous(trans = "reverse", breaks = unique(df$hour))
So far so good, I can recreate this. However my own dataset is website visit data at the visit level, so many visits in a given day and hour. In addition to visits I also have a timeOnPage metric.
Sample of data below with dput.
I wouldlike to heatmap the average hourly visits or timeOnPage. Here's what I tried.
Sample of my data:
> dput(sam)
structure(list(Day = structure(c(4L, 4L, 4L, 5L, 3L, 2L, 3L,
6L, 2L, 2L, 4L, 2L, 3L, 3L, 6L, 1L, 4L, 2L, 3L, 5L, 2L, 5L, 4L,
2L, 5L, 2L, 7L, 5L, 6L, 2L, 2L, 6L, 4L, 6L, 2L, 2L, 2L, 5L, 5L,
2L, 6L, 5L, 3L, 5L, 3L, 2L, 6L, 4L, 2L, 5L, 2L, 5L, 4L, 2L, 6L,
2L, 7L, 2L, 2L, 2L, 5L, 6L, 3L, 2L, 3L, 4L, 4L, 3L, 6L, 2L, 5L,
3L, 4L, 4L, 3L, 2L, 5L, 5L, 5L, 3L, 5L, 2L, 4L, 5L, 5L, 2L, 3L,
6L, 2L, 2L, 5L, 4L, 6L, 7L, 3L, 3L, 4L, 4L, 2L, 6L), .Label = c("Sun",
"Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"), class = c("ordered",
"factor")), Hour = c(18L, 7L, 3L, 22L, 11L, 11L, 9L, 16L, 16L,
13L, 18L, 18L, 10L, 19L, 7L, 13L, 18L, 14L, 10L, 20L, 17L, 6L,
21L, 15L, 18L, 7L, 12L, 10L, 16L, 14L, 18L, 13L, 17L, 10L, 19L,
20L, 14L, 16L, 10L, 9L, 16L, 9L, 8L, 13L, 17L, 17L, 11L, 15L,
22L, 17L, 18L, 17L, 7L, 19L, 12L, 2L, 12L, 15L, 7L, 17L, 17L,
18L, 13L, 10L, 19L, 9L, 13L, 13L, 17L, 21L, 23L, 4L, 17L, 12L,
12L, 9L, 17L, 19L, 7L, 4L, 5L, 17L, 6L, 23L, 3L, 14L, 19L, 13L,
7L, 11L, 9L, 13L, 9L, 19L, 11L, 5L, 20L, 20L, 19L, 11L), sessionID = c("1508980591045.l027p6mt",
"1510155616668.57i2wj1", "1510140439620.qu19kyo", "1510296404412.xasqfwqd10v1qdtl6jemi",
"1510082622485.szj2ja1e", "1511204933263.mq9bvi0d", "1511285142249.vp2fyfd9",
"1510965282725.x04h1dko", "1508801295434.e056cpef", "1508790369346.ly63bjgr",
"1509585154520.3usd036k", "1511834881064.e6f5evp", "1509471114265.2u807dwo",
"1507688054076.9dls0jk", "1509721031589.ho125mpb", "1510521845178.99j1ibkr",
"1510194555297.ioepfjgr", "1508793469455.hkc3xwa8", "1511288175700.62n5oc5",
"1510287319653.7ye9sjc", "1511227016523.yyn1of99", "1511448209341.1u5vir5p",
"1510205972493.qvu4ev7o", "1510615247987.swxhwct", "1508463701266.p52sdjzp",
"1510588449881.d6ffruv9", "1507404213416.rovwmmge", "1510857718956.2z57w2vr",
"1510360661780.19hznp3m78pvi", "1511820500742.48cyvo2a", "1508809029952.up0wqq5h",
"1508533120441.gdvhacjr7jswiquwuyp66r", "1509583258224.j8krac0sz5kx8pxohl4n29",
"1511549442901.5vm7na1l", "1508811367845.7b36epqk", "1509421407861.om0ydylt",
"1508794534361.p3gcoa0e", "1510877729807.viad220f", "1511460355269.omwvd00l",
"1508775703610.usuk2akm", "1510964376869.7e2crw9d", "1510247098808.np9ia23",
"1508860753512.3z4182b", "1510868797935.3nmpvkri", "1510105270807.4evhpys",
"1511831565084.27izf13f", "1510340973580.l9qj5drou5wmi", "1508364715184.14l4ikj",
"1509426566404.9qnp0m3", "1510275972333.hhqu0exc", "1510625679744.jk3vvt1v",
"1510881839700.c34skful", "1511365134270.57thqyir", "1509416741055.1f2cnmrp",
"1509738404263.8ajwpij", "1510570338116.h9a5j88", "1511640706961.qw8q1eh",
"1510011913201.eqd54kw", "1508769010911.wrpb329", "1508803518777.56b2ej2l",
"1509670743316.yhncp17j", "1511576965410.y47g0wgj", "1508876390209.wem8i3lh",
"1508779846415.hyx8qar", "1511322782502.s835px9", "1509554323957.osxgi0em",
"1510176829762.jncm9xwb", "1509482328620.sqdbob0u", "1508545652936.a5hqcmp1fw29",
"1508817816447.6mbdldxb", "1510297785623.33i6yhko", "1508843299131.3m26sqf5",
"1510191633431.cl5fh9ik", "1509565114633.bd5yrkf5", "1510690660714.818yxn5o",
"1507567660773.ybpbfgn", "1509667501973.1a9f9pyp", "1509674601865.yqvmcclv",
"1511450423709.s149r25q", "1511267096892.n5u1d0nv", "1509624499459.u57lgtt8",
"1510019204298.ka4w9kfh", "1511362131909.t26h6ig", "1510904968660.eowoea2q",
"1510225256391.4dk073ej", "1510006654569.reo2eili", "1509501692686.ng48bwnz",
"1509741958143.bxbf325r", "1508770633217.33ymrfgc", "1511810438817.zcgpr6vj",
"1510852180447.wywsj7f", "1510176833767.nev0iaec", "1509727547082.53van2sr",
"1507430914148.niu297m", "1508868705810.akd7r18h", "1510060231388.mz9ojf6g",
"1509592760232.qtrlxye8", "1509592651211.1r82ucw4", "1508812928318.f3st4004",
"1509734102140.leol1dnw"), uniquePageviews = c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), timeOnPage = c(359, 149, 69, 146, 147, 119, 168, 69, 29, 0,
1542, 148, 242, 49, 457, 175, 175, 97, 79, 12, 0, 1141, 150,
236, 74, 128, 23, 147, 172, 223, 225, 88, 69, 156, 0, 49, 110,
150, 70, 123, 30, 145, 1629, 1, 119, 169, 48, 136, 529, 130,
149, 124, 281, 2483, 0, 60, 149, 50, 29, 124, 149, 0, 92, 149,
915, 47, 50, 89, 143, 84, 129, 147, 138, 80, 33, 226, 70, 146,
177, 98, 150, 32, 148, 149, 12, 338, 146, 204, 149, 148, 26,
149, 1110, 148, 23, 151, 0, 100, 0, 28)), row.names = c(20219L,
42612L, 42149L, 46707L, 40122L, 57449L, 60878L, 56707L, 11725L,
10102L, 29911L, 71743L, 25952L, 1492L, 35570L, 48411L, 43917L,
10530L, 61004L, 46446L, 58846L, 65695L, 44287L, 49341L, 2999L,
48502L, 627L, 54118L, 48148L, 70166L, 13346L, 4770L, 29745L,
67979L, 13832L, 24814L, 10692L, 54744L, 65995L, 8216L, 56683L,
44920L, 18121L, 54499L, 41155L, 71353L, 47606L, 1900L, 25023L,
45811L, 49937L, 54904L, 63607L, 24571L, 36060L, 48479L, 69086L,
37708L, 7353L, 12117L, 33912L, 68752L, 19081L, 8768L, 62647L,
28317L, 43172L, 26286L, 6359L, 14907L, 46733L, 16418L, 43797L,
28637L, 51671L, 1273L, 33677L, 34226L, 65759L, 60247L, 31739L,
38171L, 63497L, 55589L, 44462L, 37454L, 27141L, 36178L, 7543L,
69636L, 54030L, 43173L, 35743L, 852L, 18784L, 39283L, 30672L,
30663L, 14142L, 35933L), class = "data.frame", .Names = c("Day",
"Hour", "sessionID", "uniquePageviews", "timeOnPage"))
It looks like this:
> head(sam)
Day Hour sessionID uniquePageviews timeOnPage
20219 Wed 18 1508980591045.l027p6mt 1 359
42612 Wed 7 1510155616668.57i2wj1 1 149
42149 Wed 3 1510140439620.qu19kyo 1 69
46707 Thurs 22 1510296404412.xasqfwqd10v1qdtl6jemi 1 146
40122 Tues 11 1510082622485.szj2ja1e 1 147
57449 Mon 11 1511204933263.mq9bvi0d 1 119
> glimpse(sam)
Observations: 100
Variables: 5
$ Day <ord> Wed, Wed, Wed, Thurs, Tues, Mon, Tues, Fri, Mon, Mon, Wed, Mon, Tues, Tues, Fri, Sun, Wed, M...
$ Hour <int> 18, 7, 3, 22, 11, 11, 9, 16, 16, 13, 18, 18, 10, 19, 7, 13, 18, 14, 10, 20, 17, 6, 21, 15, 1...
$ sessionID <chr> "1508980591045.l027p6mt", "1510155616668.57i2wj1", "1510140439620.qu19kyo", "1510296404412.x...
$ uniquePageviews <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
$ timeOnPage <dbl> 359, 149, 69, 146, 147, 119, 168, 69, 29, 0, 1542, 148, 242, 49, 457, 175, 175, 97, 79, 12, ...
Metric uniquePageviews will always be 1 or o and in a heatmap it doesn't look great. Since it's session level data there are multiple entries for each day / hour. For timeOnPage I wouldlike to heatmap the mean time on page for a given hour and day of week combination.
So, as far as I can tell ggplot is summing everything whereas I want mean().
My initial code block:
# creates the initial heatmap
p <- ggplot(sam, aes(x = Day, y = Hour, fill = uniquePageviews)) +
geom_tile(color = "white", size = 0.1) +
scale_fill_viridis(name = "TimeOnPage", option ="C")
# order by hour of day going top to bottom asc
p <-p + scale_y_continuous(trans = "reverse", breaks = unique(df$hour))
I tried changing it to this but the results look the exact same:
# gets the initial heatmap
p <- ggplot(sam, aes(x = Day, y = Hour, fill = uniquePageviews),
stat = "summary", fun.y = "mean") +
geom_tile(color = "white", size = 0.1) +
scale_fill_viridis(name = "Mean TimeOnPage", option ="C")
# order by hour of day going top to bottom asc
p <-p + scale_y_continuous(trans = "reverse", breaks = unique(df$hour))
I could do some dplyr group by transformations on the dataframe sam but I was not sure if ggplot::geom_tile() takes care of that or not?
How can I create a heatmap with ggplot where the fill is based on mean? Also, can someone clarify what exactly it's showing now? Total sum?
Not sure if I get your problem but you can try following:
library(tidyverse)
library(viridis)
d %>%
group_by(Day, Hour) %>%
summarise(Mean=mean(timeOnPage)) %>%
ggplot(aes(x = Day, y = Hour, fill = Mean)) +
geom_tile(color = "white", size = 0.1) +
scale_fill_viridis(name = "TimeOnPage", option ="C")
this will caclulate the mean timeOnPage per Day and Hour and plot it as a heatmap.
I am trying to display percentages in ggplot2 using geom_line and geom_point.
My code is:
print(ggplot(data=dfComb, aes(x=hour_adj, y=(..count..)/sum(..count..), group=word)) +
geom_line(aes(colour=dfComb$word)) +
geom_point(aes(colour=dfComb$word))
+ ggtitle(paste("Hourly Frequencies of Tweets")) +
xlab("Hour of Day") +
ylab("Count") +
scale_colour_discrete("Word", breaks=c("A","B"), labels=c("yid", "abbo")) +
scale_y_continuous(labels = scales::percent)
)
This errors:
Error in FUN(X[[i]], ...) : object 'count' not found
because the ..count.. variable is only created by geom_histogram (I think!) and not geom_line.
Is there an easy way to use percentages with geom_line?
FYI: EDIT, my data is:
dput(dfComb)
structure(list(hour_adj = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L), count = c(44,
24, 22, 36, 26, 18, 39, 35, 50, 46, 46, 41, 57, 49, 34, 56, 54,
54, 49, 45, 36, 49, 43, 47, 35, 20, 18, 10, 10, 25, 25, 26, 32,
25, 29, 39, 37, 45, 52, 43, 46, 67, 38, 69, 108, 80, 73, 48),
word = 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, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("A", "B"), class = "factor")), .Names = c("hour_adj",
"count", "word"), row.names = c(NA, -48L), class = "data.frame")
You can calculate percentage in the dataframe first.
Also, as per Roman Lustrik's comment, it's better to call variable by name from within aes().
library(dplyr)
# sample data
set.seed(1)
dfComb <- data.frame(hour_adj = rep(0:4, 2),
count = sample(10:50, 10, replace = T),
word = c(rep("A", 5), rep("B", 5)))
ggplot(dfComb %>%
group_by(word) %>%
mutate(perc = count/sum(count)) %>%
ungroup(),
aes(x=hour_adj, y=perc, group=word, colour = word)) +
geom_line() +
geom_point() +
ggtitle(paste("Hourly Frequencies of Tweets")) +
xlab("Hour of Day") +
ylab("Count") +
scale_colour_discrete("Word", breaks=c("A","B"), labels=c("yid", "abbo")) +
scale_y_continuous(labels = scales::percent)
I have a data set that look like this. There are 5 plots that are spatialy distributed. I want to draw a distribution map that will show the variation of a variable over the experiment. I use geom_tile to do that.
ggplot(aes(x = x, y = y), data = check) + geom_tile(aes(fill = HJD_6))
Is there any way to generate the borders around each plot. I used manualy geom_vline and geom_hline but there are problems whan I have bigger design that is not as regular as in the example.
check <- structure(list(Yta = 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, 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,
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, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 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, 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8L, 8L), x = c(1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20), y = c(31, 31, 31, 31, 31, 31,
31, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33,
33, 33, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 34, 34, 34,
34, 34, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36,
36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39,
39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 31, 31,
31, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34,
34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37,
37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39,
39, 39, 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40,
40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 42, 42, 42, 42,
42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45,
45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 47, 47,
47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 50, 50, 50, 50,
50, 50, 50, 50, 50, 50, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43,
43, 43, 43, 43, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 45,
45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46,
46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 53, 53,
53, 53, 53, 53, 53, 53, 53, 53, 54, 54, 54, 54, 54, 54, 54, 54,
54, 54, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 59,
59, 59, 59, 59, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60), RAD = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2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L, 62L, 52L, 95L,
190L, 154L, 55L, NA, NA, 180L, 130L, 90L, NA, NA, NA, NA, NA,
NA, NA, 148L, NA, NA, NA, 244L, 158L, NA, 164L, NA, NA, 224L,
NA, NA, 139L, 140L, NA, NA, 155L, 135L, 76L, 80L, 130L, NA, NA,
145L, NA, 75L, NA, 105L, 70L, 95L, NA, 95L, 115L, 140L, NA, NA,
NA, 135L, NA, 75L, 98L, 132L, 100L, 105L, 112L, NA, NA, 125L,
105L, 87L, 79L, NA, NA, NA, 165L, NA, 110L, 110L, 133L, 75L,
52L, 117L, 70L, 155L, 130L, 180L, 187L, 110L, 90L, 60L, 120L,
195L, 90L, 100L, 88L, NA, 90L, NA, 112L, 130L, 155L, 152L, 130L,
73L, 122L, 142L, 130L, 150L, 108L, NA, 86L, 125L, 90L, 119L,
125L, 206L, 100L, 95L, 40L, 160L, 222L, NA, 100L, 112L, NA, NA,
NA, 105L, 150L, 185L, NA, NA, 163L, 135L, 115L, NA, 155L, 183L,
NA, 126L, 122L, 150L, 140L, 134L, 80L, 213L, 152L, 63L, 75L,
70L, NA, 115L, 98L, 106L, 130L, NA, 123L, NA, 114L, 65L, 144L,
115L, 60L, NA, 100L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 174L,
50L, 102L, 153L, NA, NA, 85L, 132L, 85L, NA, 72L, 177L, 115L,
141L, 157L, 77L, 70L, NA, 115L, 90L, NA, NA, NA, 40L, NA, 115L,
145L, 100L, 70L, 80L, 151L, 120L, NA, 55L, 200L, NA, 120L, 170L,
185L, NA, NA, NA, 120L, 60L, NA, NA, 95L, 172L, 60L, 155L, NA,
191L, 85L, NA, 65L, 115L, 115L, 175L, 30L, 66L, 195L, 161L, 132L,
NA, 80L, 75L, 115L, NA, NA, 115L, 95L, 151L, 140L, 114L, 140L,
165L, 124L, 168L, 90L, 50L, 160L, NA, 81L, 142L, 135L, 42L, 160L,
NA, 130L, 50L, 172L, 94L, 120L, NA, 140L, NA, 145L, 120L, NA,
NA, 170L, 187L, NA, 141L, 200L, 102L, NA, NA, 136L, NA, NA, 121L,
NA, 60L, 175L, 140L, 175L, 195L, NA, 216L, 77L, 231L, 175L, 210L,
180L, 175L, 260L, NA, 160L, 172L, NA, 135L, 122L, 193L, 115L,
175L, 60L, 85L, 202L, 164L, 159L, 95L, 169L, 190L, 80L, 80L,
120L, NA, 115L, 130L, 172L, 155L, 75L, 72L, 170L, NA, NA, 65L,
75L, NA, NA, 190L, NA, NA, NA, NA, NA, NA, NA, 75L, NA, NA, 90L,
NA, 190L, NA, NA, NA, NA, 52L, NA, NA, NA, 90L, NA, NA, NA, NA,
NA, NA, 93L, 130L, 109L, NA, NA, NA, 100L, NA, NA, NA, NA, NA,
NA, 150L, NA, 202L, 161L, NA, NA, 120L, 50L, NA, 164L, NA, NA,
120L, NA, 138L, NA, NA, 154L, 60L, 57L, 195L, 130L, 75L, NA,
NA, NA, 54L, 95L, 59L, 65L, 52L, 72L, NA, NA, NA, NA, NA, NA,
67L, NA, NA, NA, 168L, NA, NA, NA, 100L, 120L, NA, 195L, 40L,
NA, NA, NA, NA, NA)), row.names = c(NA, -500L), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("Yta", "x", "y", "RAD", "PLANTA",
"HJD_6"))
One idea is to find the convex hull and plot the geom_polygon. Drawing on How to draw neat polygons around scatterplot regions in ggplot2:
library(plyr)
find_hull <- function(df) df[chull(df$x, df$y), ]
hulls <- ddply(check, "Yta", find_hull)
ggplot(aes(x = x, y = y), data = d) +
geom_tile(aes(fill = HJD_6), colour = "white") +
geom_polygon(data = hulls, aes(x = x, y = y, group = Yta),
colour = "red", alpha = 0)
You can modify hulls to make the border prettier, but this solution is quite specific to this example:
hulls <- ddply(hulls, "Yta", function(df) {
df$x <- df$x + ifelse(df$x < mean(df$x), -.5, .5)
df$y <- df$y + ifelse(df$y < mean(df$y), -.5, .5)
df
})