My interaction term doesn't cover all groups - r
I am trying to execute linear-mixed models with 3 factors as groups. However running the syntax, I just get 2 terms: time:grup_int2 or time:grup_int3 as you can see in the picture. There should be another term for time:grup_int1. My reference factor group is the level 1, however I don't know how to relevel in the loop, and I intended to post a example with a database provided by the package. The problem is with the database provided by the package works... and I get 3 terms
My code with genes_wide dataframe (plenty of NAs)
#pivot wider to make horizontal data
genes_wide <- genes1 %>% pivot_wider(names_from = gen, values_from = dCt)
genes_wide$time [genes_wide$time == "1"] <- 1
genes_wide$time [genes_wide$time == "3"] <- 3
genes_wide$time<-as.numeric(genes_wide$time)
# if they need to be relevel
#genes_wide$grup_int <- relevel(genes_wide$grup_int, "2")
# names of variables
genes.names <- colnames(genes_wide)[13:67]
no.genes <- length(genes.names)
# create a named list to hold the fitted models
gene.list <- as.list(1:no.genes)
names(gene.list) <- genes.names
# loop over gene names
for(i in genes.names){
# create temporary data matrix and model formula
#1st: adjusting per several variables (edad0 = numeric, grup_int = factor, sexo = factor, time = numeric, id = numeric (need to change?)
#2nd: adjusting just per group
tmp <- genes_wide[, c(i, "grup_int","time", "id", "edad0", "sexo")]
fml <- as.formula( paste( i, "~", paste(c("time", "time:grup_int", "edad0", "sexo"), collapse="+")))
gene.list[[i]] <- lme(fml, random= ~ time|id , method="REML", data=tmp, control = lmeControl(opt = "optim"), na.action = na.omit)
}
# List of data.frames/tibble
result <- lapply(gene.list, function(x) tidy(x))
# Solution 2
result <- dplyr::bind_rows(result, .id = "var")
result2 <- result %>% dplyr::select( var, term, p.value)
**
Example database
**
However running the exact syntax with an example database (+ 5 variables to iterate) from the nlme package, the result is different
#adding variables to df problem
BodyWeight$var1 <- rnorm(176, 47 ,3)
BodyWeight$var2 <- rnorm(176, 50 ,10)
BodyWeight$var3 <- rnorm(176, 68, 7)
BodyWeight$var4 <- rnorm(176, 150 ,10)
BodyWeight$var5 <- rnorm(176, 140, 7)
# names of variables
var.names <- colnames(BodyWeight)[5:9]
no.var <- length(var.names)
# create a named list to hold the fitted models
var.list <- as.list(1:no.var)
names(var.list) <- var.names
# loop over gene names
for(i in var.names){
#2nd: adjusting just per group
tmp <- BodyWeight[, c(i, "Diet","Time", "Rat", "weight")]
fml <- as.formula( paste( i, "~", paste(c("Time:Diet", "weight"), collapse="+")))
var.list[[i]] <- lme(fml, random= ~ Time|Rat , method="REML", data=tmp, control = lmeControl(opt = "optim"), na.action = na.omit)
}
# List of data.frames/tibble
result <- lapply(var.list, function(x) tidy(x))
# Solution
result <- dplyr::bind_rows(result, .id = "var")
result2 <- result %>% dplyr::select( var, term, p.value)
As you can see I get 3 terms:
Time:Diet1
Time:Diet2
Time:Diet3
My original database is made up by more variables, but time is numeric, as well as grouping variables are factor (grup_int and Diet respectively). The original database contains NAs
I was wondering if it is possible to relevel to get the interaction remaining
UPDATE
I don't know how to put in words, what is happening when I get this:
What does it mean? time:grup_int1 is significant but time:grup_int2 not? Is time:grup_int1 the meaning for statistical differences between group 1 ant the others groups (2 and 3) across time ? It doesn't make any sense to be significant for one but not for the others. How can I get an overall p-value comparison between groups?
The df used for the loop
structure(list(weight = c(240, 250, 255, 260, 262, 258, 266,
266, 265, 272, 278, 225, 230, 230, 232, 240, 240, 243, 244, 238,
247, 245, 245, 250, 250, 255, 262, 265, 267, 267, 264, 268, 269,
260, 255, 255, 265, 265, 268, 270, 272, 274, 273, 275, 255, 260,
255, 270, 270, 273, 274, 273, 276, 278, 280, 260, 265, 270, 275,
275, 277, 278, 278, 284, 279, 281, 275, 275, 260, 270, 273, 274,
276, 271, 282, 281, 284, 245, 255, 260, 268, 270, 265, 265, 267,
273, 274, 278, 410, 415, 425, 428, 438, 443, 442, 446, 456, 468,
478, 405, 420, 430, 440, 448, 460, 458, 464, 475, 484, 496, 445,
445, 450, 452, 455, 455, 451, 450, 462, 466, 472, 555, 560, 565,
580, 590, 597, 595, 595, 612, 618, 628, 470, 465, 475, 485, 487,
493, 493, 504, 507, 518, 525, 535, 525, 530, 533, 535, 540, 525,
530, 543, 544, 559, 520, 525, 530, 540, 543, 546, 538, 544, 553,
555, 548, 510, 510, 520, 515, 530, 538, 535, 542, 550, 553, 569
), Time = c(1, 8, 15, 22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15,
22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44,
50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15,
22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44,
50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15,
22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44,
50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15,
22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44,
50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15,
22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44,
50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44, 50, 57, 64), Rat = structure(c(4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L), levels = c("2", "3", "4", "1", "8", "5", "6", "7", "11",
"9", "10", "12", "13", "15", "14", "16"), class = c("ordered",
"factor")), Diet = 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,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 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), levels = c("1", "2", "3"), class = "factor"),
var1 = c(46.804, 45.792, 43.572, 50.587, 52.268, 45.402,
45.996, 48.566, 47.933, 48.336, 43.964, 52.441, 45.077, 45.699,
51.348, 47.888, 50.245, 48.111, 42.373, 44.231, 46.461, 45.639,
44.993, 50.391, 47.997, 48.368, 44.939, 47.805, 49.195, 51.419,
50.242, 45.856, 48.115, 48.053, 44.318, 50.493, 40.48, 47.418,
49.441, 49.414, 49.776, 49.043, 41.456, 48.52, 46.079, 43.058,
43.649, 43.967, 44.799, 48.081, 50.728, 51.207, 50.01, 47.346,
43.816, 52.575, 47.962, 49.061, 45.438, 43.822, 43.022, 48.864,
41.669, 45.58, 42.206, 42.788, 43.988, 46.636, 45.651, 42.502,
42.93, 49.196, 48.314, 46.412, 49.436, 47.193, 48.806, 45.142,
48.4, 48.419, 47.937, 42.605, 46.027, 45.325, 43.811, 45.922,
47.262, 46.566, 47.016, 46.111, 50.573, 47.436, 47.859, 47.839,
48.239, 46.762, 46.581, 55.9, 42.816, 49.636, 47.514, 39.489,
46.513, 49.323, 51.504, 45.558, 42.402, 45.153, 48.632, 47.908,
46.033, 44.963, 49.312, 45.733, 51.149, 45.755, 47.247, 46.674,
49.121, 43.411, 43.735, 44.777, 44.942, 48.207, 50.962, 47.1,
44.167, 50.007, 48.531, 46.749, 47.69, 47.396, 45.074, 49.203,
42.869, 47.713, 46.186, 52.191, 46.809, 47.578, 45.365, 53.228,
45.896, 45.428, 42.702, 47.328, 46.108, 49.857, 45.128, 43.772,
46.479, 51.573, 51.255, 45.33, 45.651, 48.521, 49.006, 51.906,
48.924, 53.693, 48.555, 48.367, 49.659, 47.839, 47.829, 48.212,
45.867, 51.965, 48.275, 49.091, 44.54, 44.978, 50.431, 48.915,
51.569, 46.008), var2 = c(53.273, 35.7, 44.406, 62.644, 42.971,
49.393, 52.585, 30.572, 52.156, 58.566, 44.631, 50.967, 48.583,
60.878, 50.712, 48.741, 47.584, 54.774, 62.288, 49.952, 33.225,
39.251, 53.158, 46.74, 46.796, 40.343, 58.491, 48.12, 41.854,
55.315, 44.553, 55.674, 47.404, 56.085, 53.443, 56.382, 74.641,
44.822, 73.191, 46.409, 66.453, 60.929, 51.394, 56.812, 53.244,
48.993, 54.763, 35.624, 63.346, 38.389, 48.288, 61.422, 45.707,
50.213, 42.629, 64.381, 39.218, 58.923, 61.569, 39.279, 54.748,
63.149, 54.639, 56.494, 66.907, 36.303, 52.297, 46.288, 62.852,
53.063, 43.096, 70.983, 49.175, 43.663, 44.456, 55.17, 51.383,
56.945, 51.471, 49.053, 63.062, 46.386, 48.094, 43.282, 35.509,
48.079, 48.223, 34.605, 42.669, 40.146, 53.089, 40.911, 39.396,
39.507, 41.481, 41.729, 28.367, 62.781, 59.154, 50.788, 84.282,
58.214, 53.404, 46.312, 36.983, 61.712, 53.109, 37.285, 50.834,
32.306, 47.969, 36.232, 46.744, 37.876, 50.369, 59.684, 40.427,
44.2, 46.955, 56.388, 46.667, 46.373, 49.932, 60.3, 27.732,
56.772, 54.569, 45.164, 37.747, 57.827, 49.998, 23.109, 53.339,
51.024, 58.875, 55.315, 40.637, 37.781, 47.209, 57.51, 46.956,
66.715, 51.034, 52.54, 60.342, 34.439, 58.08, 64.298, 56.374,
64.829, 49.392, 48.939, 38.746, 40.048, 46.856, 61.697, 51.253,
54.024, 61.166, 45.834, 43.578, 39.212, 67.632, 35.208, 23.676,
50.577, 46.832, 42.866, 57.808, 53.292, 42.474, 62.779, 34.747,
55.367, 27.928, 57.481), var3 = c(67.483, 67.977, 70.749,
76.851, 77.351, 59.821, 67.203, 72.635, 67.767, 67.627, 64.729,
70.388, 71.889, 61.95, 54.182, 71.049, 56.33, 68.982, 70.615,
68.416, 60.661, 73.205, 68.086, 72.611, 62.082, 74.026, 69.073,
67.488, 79.656, 79.612, 71.016, 61.284, 76.297, 62.713, 79.335,
68.155, 67.184, 73.889, 61.59, 68.273, 57.056, 66.95, 60.504,
77.121, 73.056, 60.739, 75.891, 74.363, 82.829, 86.902, 59.38,
77.63, 67.613, 72.666, 68.903, 71.631, 68.86, 57.653, 65.119,
60.825, 63.301, 64.945, 65.89, 80.092, 79.951, 65.475, 66.491,
72.355, 71.909, 70.339, 83.508, 59.686, 62.102, 67.291, 52.908,
70.508, 63.2, 66.62, 75.138, 72.311, 66.394, 64.812, 74.063,
51.087, 55.095, 56.906, 55.88, 71.916, 81.805, 67.339, 66.459,
70.005, 82.891, 65.456, 86.611, 65.401, 72.647, 59.198, 73.558,
66.536, 69.83, 77.101, 65.494, 62.129, 71.86, 85.406, 70.619,
79.225, 71.806, 62.86, 79.615, 65.049, 63.472, 63.46, 80.215,
74.033, 58.949, 69.519, 66.39, 68.751, 69.356, 75.201, 54.815,
50.592, 76.019, 62.177, 66.677, 69.495, 82.428, 70.743, 59.458,
75.326, 69.615, 67.157, 69.793, 66.205, 75.328, 76.311, 64.36,
62.207, 74.16, 51.799, 66.378, 67.964, 66.937, 59.779, 59.735,
64.992, 66.817, 60.595, 65.388, 50.404, 61.742, 65.789, 77.047,
56.86, 69.756, 66.338, 57.149, 63.407, 73.601, 66.042, 67.421,
51.208, 47.265, 69.465, 63.471, 75.845, 74.938, 69.009, 81.807,
81.195, 64.288, 77.085, 64.698, 65.747), var4 = c(143.048,
139.381, 152.849, 163.726, 155.712, 146.957, 140.231, 148.321,
141.917, 156.873, 165.845, 158.824, 157.305, 144.521, 145.253,
134.892, 153.597, 124.482, 153.096, 134.954, 171.238, 147.43,
150.983, 129.945, 150.652, 142.259, 156.631, 152.215, 152.196,
153.54, 139.712, 168.495, 152.043, 148.538, 164.067, 172.445,
144.175, 144.015, 149.093, 135.921, 156.787, 148.292, 144.958,
137.332, 148.185, 151.678, 160.774, 168.479, 175.839, 148.211,
163.747, 145.416, 131.502, 154.997, 152.35, 125.955, 167.116,
141.734, 135.958, 162.752, 139.482, 149.315, 144.425, 144.654,
160.05, 153.767, 148.532, 143.102, 135.553, 143.305, 153.021,
127.982, 165.047, 144.142, 136.568, 145.793, 143.913, 158.002,
154.043, 167.63, 165.472, 143.681, 166.417, 152.012, 145.843,
144.448, 138.782, 141.475, 150.717, 140.765, 164.657, 133.655,
151.908, 160.084, 138.25, 155.93, 142.756, 170.935, 128.346,
149.986, 154.099, 141.688, 150.616, 149.999, 139.195, 160.921,
148.261, 144.611, 154.183, 157.2, 130.512, 155.004, 125.064,
124.813, 150.074, 145.615, 152.238, 144.241, 149.079, 159.354,
153.398, 146.609, 155.032, 145.522, 143.296, 139.502, 148.944,
143.446, 155.094, 148.208, 154.417, 153.249, 154.544, 140.746,
156.798, 146.069, 134.905, 145.318, 137.198, 142.087, 137.117,
151.782, 147.245, 148.583, 152.535, 151.265, 152.272, 162.014,
150.055, 154.343, 139.717, 148.752, 140.933, 146.761, 145.311,
146.361, 155.264, 154.984, 168.623, 129.31, 147.492, 139.828,
161.22, 145.89, 139.146, 138.104, 148.543, 155.836, 143.594,
159.32, 163.648, 152.058, 160.412, 133, 155.305, 144.094),
var5 = c(135.794, 136.92, 131.548, 132.091, 143.587, 130.393,
133.678, 143.005, 136.036, 128.161, 134.036, 144.237, 125.767,
133.32, 137.047, 140.3, 137.92, 144.883, 140.901, 140.632,
136.315, 138.138, 140.855, 124.721, 143.153, 144.514, 142.092,
144.765, 139.205, 134.905, 141.815, 122.992, 138.249, 125.918,
125.236, 136.503, 137.462, 130.757, 135.141, 135.413, 134.991,
137.143, 153.329, 148.779, 138.249, 133.978, 132.454, 139.955,
122.84, 135.984, 140.941, 134.203, 146.091, 146.766, 136.259,
151.474, 139.683, 132.913, 142.39, 134.625, 124.717, 145.74,
135.647, 135.119, 144.182, 133.961, 130.387, 141.314, 129.26,
127.154, 154.74, 142.327, 142.23, 139.433, 148.245, 132.123,
127.374, 134.518, 133.422, 143.66, 129.306, 138.31, 147.787,
137.415, 138.686, 143.896, 140.973, 128.592, 139.53, 146.29,
145, 129.926, 132.338, 155.193, 132.286, 140.86, 135.343,
137.037, 140.147, 131.872, 137.858, 142.137, 131.587, 121.455,
142.264, 138.533, 138.769, 136.296, 141.055, 133.475, 148.136,
136.175, 143.947, 152.026, 137.649, 142.466, 144.819, 153.745,
138.95, 138.146, 133.404, 147.928, 145.928, 146.421, 145.156,
134.289, 127.955, 148.223, 129.987, 139.295, 141.035, 150.867,
136.412, 139.705, 142.013, 129.377, 143.521, 145.595, 142.936,
137.703, 133.344, 144.46, 143.001, 137.017, 149.985, 144.242,
158.029, 151.835, 133.262, 123.893, 136.338, 143.951, 136.71,
136.007, 147.102, 134.472, 128.058, 145.521, 142.379, 135.869,
144.135, 141.297, 149.133, 134.008, 138.566, 117.773, 151.701,
140.288, 144.17, 140.886, 151.585, 136.438, 150.399, 140.535,
142.601, 136.449)), row.names = c(NA, 176L), class = c("nfnGroupedData",
"nfGroupedData", "groupedData", "data.frame"), outer = ~Diet, formula = weight ~
Time | Rat, labels = list(x = "Time", y = "Body weight"), units = list(
x = "(days)", y = "(g)"), FUN = structure(function (x)
max(x, na.rm = TRUE), source = "function(x) max(x, na.rm = TRUE)"), order.groups = TRUE)
The problem (i.e., difference between what you get in your original model and what you get in the other example you posted) is not because of the levelling of the factors, but because of a difference in model specification. In the first model results you posted you include Time + Time:Diet - the "main effect" of Time and the product regressors with Diet. However in the second set of models - the ones with the dummy data you provided - you do not include the main effect in the model. This is what produces the difference. I show the difference below. First, load the data.
BodyWeight <- structure(list(weight = c(240, 250, 255, 260, 262, 258, 266,
266, 265, 272, 278, 225, 230, 230, 232, 240, 240, 243, 244, 238,
247, 245, 245, 250, 250, 255, 262, 265, 267, 267, 264, 268, 269,
260, 255, 255, 265, 265, 268, 270, 272, 274, 273, 275, 255, 260,
255, 270, 270, 273, 274, 273, 276, 278, 280, 260, 265, 270, 275,
275, 277, 278, 278, 284, 279, 281, 275, 275, 260, 270, 273, 274,
276, 271, 282, 281, 284, 245, 255, 260, 268, 270, 265, 265, 267,
273, 274, 278, 410, 415, 425, 428, 438, 443, 442, 446, 456, 468,
478, 405, 420, 430, 440, 448, 460, 458, 464, 475, 484, 496, 445,
445, 450, 452, 455, 455, 451, 450, 462, 466, 472, 555, 560, 565,
580, 590, 597, 595, 595, 612, 618, 628, 470, 465, 475, 485, 487,
493, 493, 504, 507, 518, 525, 535, 525, 530, 533, 535, 540, 525,
530, 543, 544, 559, 520, 525, 530, 540, 543, 546, 538, 544, 553,
555, 548, 510, 510, 520, 515, 530, 538, 535, 542, 550, 553, 569
), Time = c(1, 8, 15, 22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15,
22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44,
50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15,
22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44,
50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15,
22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44,
50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15,
22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44,
50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15,
22, 29, 36, 43, 44, 50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44,
50, 57, 64, 1, 8, 15, 22, 29, 36, 43, 44, 50, 57, 64), Rat = structure(c(4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L), levels = c("2", "3", "4", "1", "8", "5", "6", "7", "11",
"9", "10", "12", "13", "15", "14", "16"), class = c("ordered",
"factor")), Diet = 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,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 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), levels = c("1", "2", "3"), class = "factor"),
var1 = c(46.804, 45.792, 43.572, 50.587, 52.268, 45.402,
45.996, 48.566, 47.933, 48.336, 43.964, 52.441, 45.077, 45.699,
51.348, 47.888, 50.245, 48.111, 42.373, 44.231, 46.461, 45.639,
44.993, 50.391, 47.997, 48.368, 44.939, 47.805, 49.195, 51.419,
50.242, 45.856, 48.115, 48.053, 44.318, 50.493, 40.48, 47.418,
49.441, 49.414, 49.776, 49.043, 41.456, 48.52, 46.079, 43.058,
43.649, 43.967, 44.799, 48.081, 50.728, 51.207, 50.01, 47.346,
43.816, 52.575, 47.962, 49.061, 45.438, 43.822, 43.022, 48.864,
41.669, 45.58, 42.206, 42.788, 43.988, 46.636, 45.651, 42.502,
42.93, 49.196, 48.314, 46.412, 49.436, 47.193, 48.806, 45.142,
48.4, 48.419, 47.937, 42.605, 46.027, 45.325, 43.811, 45.922,
47.262, 46.566, 47.016, 46.111, 50.573, 47.436, 47.859, 47.839,
48.239, 46.762, 46.581, 55.9, 42.816, 49.636, 47.514, 39.489,
46.513, 49.323, 51.504, 45.558, 42.402, 45.153, 48.632, 47.908,
46.033, 44.963, 49.312, 45.733, 51.149, 45.755, 47.247, 46.674,
49.121, 43.411, 43.735, 44.777, 44.942, 48.207, 50.962, 47.1,
44.167, 50.007, 48.531, 46.749, 47.69, 47.396, 45.074, 49.203,
42.869, 47.713, 46.186, 52.191, 46.809, 47.578, 45.365, 53.228,
45.896, 45.428, 42.702, 47.328, 46.108, 49.857, 45.128, 43.772,
46.479, 51.573, 51.255, 45.33, 45.651, 48.521, 49.006, 51.906,
48.924, 53.693, 48.555, 48.367, 49.659, 47.839, 47.829, 48.212,
45.867, 51.965, 48.275, 49.091, 44.54, 44.978, 50.431, 48.915,
51.569, 46.008), var2 = c(53.273, 35.7, 44.406, 62.644, 42.971,
49.393, 52.585, 30.572, 52.156, 58.566, 44.631, 50.967, 48.583,
60.878, 50.712, 48.741, 47.584, 54.774, 62.288, 49.952, 33.225,
39.251, 53.158, 46.74, 46.796, 40.343, 58.491, 48.12, 41.854,
55.315, 44.553, 55.674, 47.404, 56.085, 53.443, 56.382, 74.641,
44.822, 73.191, 46.409, 66.453, 60.929, 51.394, 56.812, 53.244,
48.993, 54.763, 35.624, 63.346, 38.389, 48.288, 61.422, 45.707,
50.213, 42.629, 64.381, 39.218, 58.923, 61.569, 39.279, 54.748,
63.149, 54.639, 56.494, 66.907, 36.303, 52.297, 46.288, 62.852,
53.063, 43.096, 70.983, 49.175, 43.663, 44.456, 55.17, 51.383,
56.945, 51.471, 49.053, 63.062, 46.386, 48.094, 43.282, 35.509,
48.079, 48.223, 34.605, 42.669, 40.146, 53.089, 40.911, 39.396,
39.507, 41.481, 41.729, 28.367, 62.781, 59.154, 50.788, 84.282,
58.214, 53.404, 46.312, 36.983, 61.712, 53.109, 37.285, 50.834,
32.306, 47.969, 36.232, 46.744, 37.876, 50.369, 59.684, 40.427,
44.2, 46.955, 56.388, 46.667, 46.373, 49.932, 60.3, 27.732,
56.772, 54.569, 45.164, 37.747, 57.827, 49.998, 23.109, 53.339,
51.024, 58.875, 55.315, 40.637, 37.781, 47.209, 57.51, 46.956,
66.715, 51.034, 52.54, 60.342, 34.439, 58.08, 64.298, 56.374,
64.829, 49.392, 48.939, 38.746, 40.048, 46.856, 61.697, 51.253,
54.024, 61.166, 45.834, 43.578, 39.212, 67.632, 35.208, 23.676,
50.577, 46.832, 42.866, 57.808, 53.292, 42.474, 62.779, 34.747,
55.367, 27.928, 57.481), var3 = c(67.483, 67.977, 70.749,
76.851, 77.351, 59.821, 67.203, 72.635, 67.767, 67.627, 64.729,
70.388, 71.889, 61.95, 54.182, 71.049, 56.33, 68.982, 70.615,
68.416, 60.661, 73.205, 68.086, 72.611, 62.082, 74.026, 69.073,
67.488, 79.656, 79.612, 71.016, 61.284, 76.297, 62.713, 79.335,
68.155, 67.184, 73.889, 61.59, 68.273, 57.056, 66.95, 60.504,
77.121, 73.056, 60.739, 75.891, 74.363, 82.829, 86.902, 59.38,
77.63, 67.613, 72.666, 68.903, 71.631, 68.86, 57.653, 65.119,
60.825, 63.301, 64.945, 65.89, 80.092, 79.951, 65.475, 66.491,
72.355, 71.909, 70.339, 83.508, 59.686, 62.102, 67.291, 52.908,
70.508, 63.2, 66.62, 75.138, 72.311, 66.394, 64.812, 74.063,
51.087, 55.095, 56.906, 55.88, 71.916, 81.805, 67.339, 66.459,
70.005, 82.891, 65.456, 86.611, 65.401, 72.647, 59.198, 73.558,
66.536, 69.83, 77.101, 65.494, 62.129, 71.86, 85.406, 70.619,
79.225, 71.806, 62.86, 79.615, 65.049, 63.472, 63.46, 80.215,
74.033, 58.949, 69.519, 66.39, 68.751, 69.356, 75.201, 54.815,
50.592, 76.019, 62.177, 66.677, 69.495, 82.428, 70.743, 59.458,
75.326, 69.615, 67.157, 69.793, 66.205, 75.328, 76.311, 64.36,
62.207, 74.16, 51.799, 66.378, 67.964, 66.937, 59.779, 59.735,
64.992, 66.817, 60.595, 65.388, 50.404, 61.742, 65.789, 77.047,
56.86, 69.756, 66.338, 57.149, 63.407, 73.601, 66.042, 67.421,
51.208, 47.265, 69.465, 63.471, 75.845, 74.938, 69.009, 81.807,
81.195, 64.288, 77.085, 64.698, 65.747), var4 = c(143.048,
139.381, 152.849, 163.726, 155.712, 146.957, 140.231, 148.321,
141.917, 156.873, 165.845, 158.824, 157.305, 144.521, 145.253,
134.892, 153.597, 124.482, 153.096, 134.954, 171.238, 147.43,
150.983, 129.945, 150.652, 142.259, 156.631, 152.215, 152.196,
153.54, 139.712, 168.495, 152.043, 148.538, 164.067, 172.445,
144.175, 144.015, 149.093, 135.921, 156.787, 148.292, 144.958,
137.332, 148.185, 151.678, 160.774, 168.479, 175.839, 148.211,
163.747, 145.416, 131.502, 154.997, 152.35, 125.955, 167.116,
141.734, 135.958, 162.752, 139.482, 149.315, 144.425, 144.654,
160.05, 153.767, 148.532, 143.102, 135.553, 143.305, 153.021,
127.982, 165.047, 144.142, 136.568, 145.793, 143.913, 158.002,
154.043, 167.63, 165.472, 143.681, 166.417, 152.012, 145.843,
144.448, 138.782, 141.475, 150.717, 140.765, 164.657, 133.655,
151.908, 160.084, 138.25, 155.93, 142.756, 170.935, 128.346,
149.986, 154.099, 141.688, 150.616, 149.999, 139.195, 160.921,
148.261, 144.611, 154.183, 157.2, 130.512, 155.004, 125.064,
124.813, 150.074, 145.615, 152.238, 144.241, 149.079, 159.354,
153.398, 146.609, 155.032, 145.522, 143.296, 139.502, 148.944,
143.446, 155.094, 148.208, 154.417, 153.249, 154.544, 140.746,
156.798, 146.069, 134.905, 145.318, 137.198, 142.087, 137.117,
151.782, 147.245, 148.583, 152.535, 151.265, 152.272, 162.014,
150.055, 154.343, 139.717, 148.752, 140.933, 146.761, 145.311,
146.361, 155.264, 154.984, 168.623, 129.31, 147.492, 139.828,
161.22, 145.89, 139.146, 138.104, 148.543, 155.836, 143.594,
159.32, 163.648, 152.058, 160.412, 133, 155.305, 144.094),
var5 = c(135.794, 136.92, 131.548, 132.091, 143.587, 130.393,
133.678, 143.005, 136.036, 128.161, 134.036, 144.237, 125.767,
133.32, 137.047, 140.3, 137.92, 144.883, 140.901, 140.632,
136.315, 138.138, 140.855, 124.721, 143.153, 144.514, 142.092,
144.765, 139.205, 134.905, 141.815, 122.992, 138.249, 125.918,
125.236, 136.503, 137.462, 130.757, 135.141, 135.413, 134.991,
137.143, 153.329, 148.779, 138.249, 133.978, 132.454, 139.955,
122.84, 135.984, 140.941, 134.203, 146.091, 146.766, 136.259,
151.474, 139.683, 132.913, 142.39, 134.625, 124.717, 145.74,
135.647, 135.119, 144.182, 133.961, 130.387, 141.314, 129.26,
127.154, 154.74, 142.327, 142.23, 139.433, 148.245, 132.123,
127.374, 134.518, 133.422, 143.66, 129.306, 138.31, 147.787,
137.415, 138.686, 143.896, 140.973, 128.592, 139.53, 146.29,
145, 129.926, 132.338, 155.193, 132.286, 140.86, 135.343,
137.037, 140.147, 131.872, 137.858, 142.137, 131.587, 121.455,
142.264, 138.533, 138.769, 136.296, 141.055, 133.475, 148.136,
136.175, 143.947, 152.026, 137.649, 142.466, 144.819, 153.745,
138.95, 138.146, 133.404, 147.928, 145.928, 146.421, 145.156,
134.289, 127.955, 148.223, 129.987, 139.295, 141.035, 150.867,
136.412, 139.705, 142.013, 129.377, 143.521, 145.595, 142.936,
137.703, 133.344, 144.46, 143.001, 137.017, 149.985, 144.242,
158.029, 151.835, 133.262, 123.893, 136.338, 143.951, 136.71,
136.007, 147.102, 134.472, 128.058, 145.521, 142.379, 135.869,
144.135, 141.297, 149.133, 134.008, 138.566, 117.773, 151.701,
140.288, 144.17, 140.886, 151.585, 136.438, 150.399, 140.535,
142.601, 136.449)), row.names = c(NA, 176L), class = c("nfnGroupedData",
"nfGroupedData", "groupedData", "data.frame"), outer = ~Diet, formula = weight ~
Time | Rat, labels = list(x = "Time", y = "Body weight"), units = list(
x = "(days)", y = "(g)"), FUN = structure(function (x)
max(x, na.rm = TRUE), source = "function(x) max(x, na.rm = TRUE)"), order.groups = TRUE)
Here is the model without the main effect of Time. You can see, you get the desired result - coefficients for each value of Diet.
library(name)
mod0 <- lme(var1 ~ Time:Diet + weight, random= ~ Time|Rat , method="REML", data=BodyWeight, control = lmeControl(opt = "optim"), na.action = na.omit)
summary(mod0)
#> Linear mixed-effects model fit by REML
#> Data: BodyWeight
#> AIC BIC logLik
#> 905.3992 933.6741 -443.6996
#>
#> Random effects:
#> Formula: ~Time | Rat
#> Structure: General positive-definite, Log-Cholesky parametrization
#> StdDev Corr
#> (Intercept) 0.73280912 (Intr)
#> Time 0.02632007 -0.912
#> Residual 2.73566273
#>
#> Fixed effects: var1 ~ Time:Diet + weight
#> Value Std.Error DF t-value p-value
#> (Intercept) 46.94072 1.3142431 156 35.71692 0.0000
#> weight 0.00016 0.0033845 156 0.04685 0.9627
#> Time:Diet1 -0.00470 0.0160669 156 -0.29282 0.7701
#> Time:Diet2 0.00369 0.0194774 156 0.18956 0.8499
#> Time:Diet3 0.02582 0.0213189 156 1.21119 0.2277
#> Correlation:
#> (Intr) weight Tm:Dt1 Tm:Dt2
#> weight -0.940
#> Time:Diet1 -0.698 0.497
#> Time:Diet2 0.311 -0.533 0.109
#> Time:Diet3 0.422 -0.635 0.026 0.620
#>
#> Standardized Within-Group Residuals:
#> Min Q1 Med Q3 Max
#> -2.75835293 -0.70479591 0.02987446 0.58694850 3.08058470
#>
#> Number of Observations: 176
#> Number of Groups: 16
Now, let's look at the model specified with the Time main effect as well. Here, you can see that you get the Time coefficient and one for all but the reference level of Diet interacted with Time. The omission comes because Time = Time:Diet1 + Time:Diet2 + Time:Diet3, so these four terms are perfectly collinear.
mod1 <- lme(var1 ~ Time + Time:Diet + weight, random= ~ Time|Rat , method="REML", data=BodyWeight, control = lmeControl(opt = "optim"), na.action = na.omit)
summary(mod1)
#> Linear mixed-effects model fit by REML
#> Data: BodyWeight
#> AIC BIC logLik
#> 905.3992 933.6741 -443.6996
#>
#> Random effects:
#> Formula: ~Time | Rat
#> Structure: General positive-definite, Log-Cholesky parametrization
#> StdDev Corr
#> (Intercept) 0.73280912 (Intr)
#> Time 0.02632007 -0.912
#> Residual 2.73566273
#>
#> Fixed effects: var1 ~ Time + Time:Diet + weight
#> Value Std.Error DF t-value p-value
#> (Intercept) 46.94072 1.3142431 156 35.71692 0.0000
#> Time -0.00470 0.0160669 156 -0.29282 0.7701
#> weight 0.00016 0.0033845 156 0.04685 0.9627
#> Time:Diet2 0.00840 0.0238626 156 0.35188 0.7254
#> Time:Diet3 0.03053 0.0263585 156 1.15811 0.2486
#> Correlation:
#> (Intr) Time weight Tm:Dt2
#> Time -0.698
#> weight -0.940 0.497
#> Time:Diet2 0.724 -0.585 -0.770
#> Time:Diet3 0.767 -0.588 -0.816 0.752
#>
#> Standardized Within-Group Residuals:
#> Min Q1 Med Q3 Max
#> -2.75835293 -0.70479591 0.02987446 0.58694850 3.08058470
#>
#> Number of Observations: 176
#> Number of Groups: 16
You could recover all the same information from the second model. The effect of Time for Diet1 is just the coefficient on Time. The effect of Time for Diet2 is the effect of Time plus the effect of Time:Diet2 and its standard error is the square root of the variance of the time effect, plus the variance of the interaction coefficient plus two times the covariance of the two coefficients (this comes from the equation for the calculation of the variance of a linear combination of random variables).
b <- fixef(mod1)
V <- vcov(mod1)
## Time:Diet1
d1 <- c(b[2], sqrt(V[2,2]))
d1 <- c(d1, d1[1]/d1[2])
d1 <- c(d1, 2*pt(abs(d1[3]), 156, lower.tail=FALSE))
## Time:Diet2
d2 <- c(b[2] + b[4], sqrt(V[2,2] + V[4,4] + 2*V[2,4]))
d2 <- c(d2, d2[1]/d2[2])
d2 <- c(d2, 2*pt(abs(d2[3]), 156, lower.tail=FALSE))
## Time:Diet2
d3 <- c(b[2] + b[5], sqrt(V[2,2] + V[5,5] + 2*V[2,5]))
d3 <- c(d3, d3[1]/d3[2])
d3 <- c(d3, 2*pt(abs(d3[3]), 156, lower.tail=FALSE))
d <- rbind(d1, d2, d3)
colnames(d) <- c("Estimate", "SE", "t-stat", "p-value")
rownames(d) <- c("Time:Diet1", "Time:Diet2", "Time:Diet3")
d
#> Estimate SE t-stat p-value
#> Time:Diet1 -0.004704694 0.01606695 -0.2928182 0.7700503
#> Time:Diet2 0.003692093 0.01947738 0.1895580 0.8499019
#> Time:Diet3 0.025821390 0.02131895 1.2111944 0.2276525
Compare this result with the one from the first model above you can see that they are the same:
#> Estimate SE DF t-stat p-value
#> Time:Diet1 -0.00470 0.0160669 156 -0.29282 0.7701
#> Time:Diet2 0.00369 0.0194774 156 0.18956 0.8499
#> Time:Diet3 0.02582 0.0213189 156 1.21119 0.2277
Created on 2023-02-01 by the reprex package (v2.0.1)
So, the answer to how you get a coefficient for the interaction of each level of Diet and Time is to leave out the main effect of Time. That said, I would encourage you to include the main effect of Diet in the model as well - this would allow the intercepts to vary according to Diet.
Related
putting segments on a plot with multiple plots and y variables
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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 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, 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, 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, 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)
R get linear regression equation for boxplots
I didn´t found a sufficient answer in this forum yet, so I decided to raise my own question. I want to get the linear regression equation of a linear fit from a boxplot. I have this data: library(ggplot2) data <- structure(list(x = 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 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, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L ), .Label = c("1", "2", "3", "4", "5", "6"), class = "factor"), y = c(169, 79.5, 78.5, 75, 99.5, 68, 14, 30.5, 107.5, 51, 43, 33, 21.5, 35, 11, 1, 38, 54.5, 26.5, 143, 158, 171, 31.5, 67.5, 1, 57.5, 12, 36.5, 1, 23.5, 22.5, 71, 141, 218, 7.5, 1, 129, 144.5, 76, 46.5, 75.5, 45, 12, 24, 67, 65.5, 44.5, 37.5, 25.5, 19, 15, 1, 17.5, 50, 22.5, 90, 226, 220, 32, 69.5, 1, 79.5, 7, 44, 1, 15.5, 22, 75.5, 178, 153, 4.5, 1, 159, 89, 57, 71, 98.5, 47.5, 18.5, 30, 119, 57.5, 41, 33.5, 30, 31, 10, 1, 12, 43.5, 20.5, 98, 146.5, 145, 34, 64.5, 1, 40.5, 17, 41, 1, 14.5, 16.5, 71, 181, 168, 2, 1, 159, 103, 69, 65.5, 97.5, 37.5, 21, 15.5, 120.5, 46, 27, 29.5, 16.5, 20, 7.5, 1, 15.5, 42.5, 21.5, 111, 102.5, 124, 20.5, 51.5, 1, 22.5, 15, 42, 1, 13, 13.5, 64.5, 138, 155, 4.9, 1, 190, 89.5, 74.5, 79, 78, 59.5, 19.5, 21, 88.5, 44, 18, 19, 10, 13, 4, 1, 9.5, 44, 17, 140.5, 98, 112.5, 29.5, 62.56, 1, 31, 11.5, 49.5, 1, 10, 8.5, 40.5, 121, 141, 2.5, 1, 170, 87.5, 92, 77, 65, 34, 8, 26, 98, 51.5, 26, 19, 9, 8.5, 7.5, 1, 4.5, 0, 15.5, 80, 69, 59, 28, 44.5, 1, 38.5, 10, 51.5, 1, 3, 5, 65, 107, 152, 5, 1)), row.names = c(NA, -216L), class = "data.frame") p <- ggplot(data = data) + aes(x = x, y = y) + geom_boxplot(outlier.shape = NA) + geom_jitter(shape = 1, position = position_jitter(0.1)) + ylim(0, NA) + theme_light() + geom_smooth(method = "lm",se = TRUE, formula = y ~ x, aes(group = 1)) print(p) fit <- lm(y ~ x, data = data) fit which results in this output: How can I extract the regression equation for this dataset? The function fit <- lm(y ~ x, data = data) just gives me one intercept and 5 coefficients, which is not my desired output. I want a simple regression equation in the form of y = a + bx. How can I put this equation into the diagramm? I´ve already looked into ggpmisc::stat_poly_eq(), but this doesn´t seem to work with boxplot linear regression. Can you guys help me out?
ggplot group by fill and show mean
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.
ggplot2 - Survival Swimmer Plot Not Displaying Sorted Values
Using this question I've been assembling a customized Swimmer Plot, but despite my having sorted the patients by Therapy.Length, when I try to plot it the second half of the bars often come out much longer than they should be - putting the graph out of order. I've checked and the symbols are in the correct place even when the bar is too long, so somehow the bar lengths are getting extended. By commenting out scale_y_continuous chunk, I'm able to get the proper length on the bars, but then all of the symbol placement is compressed. ggplot(dat, aes(Patient.ID, Therapy.Length)) + geom_bar(stat="identity", aes(fill=factor(Disease.Stage)), width=0.7) + geom_point(data=dat, aes(Patient.ID, value, colour=variable, shape=variable), size=4) + geom_segment(data=dat %>% filter(Continued == 1), aes(x=Patient.ID, xend=Patient.ID, y=Therapy.Length + 0.1, yend=Therapy.Length + 20), arrow=arrow(type="closed", length=unit(0.13,"in"))) + coord_flip() + scale_fill_manual(values=hcl(seq(15,375,length.out=5)[1:4],100,70)) + scale_colour_manual(values=c(hcl(seq(15,375,length.out=3)[1:2],100,40),c("black","darkgreen"))) + scale_y_continuous(limits=c(-1,max(dat$Therapy.Length)), breaks=seq(0,max(dat$Therapy.Length),30)) + labs(fill="Disease Stage", colour="", shape="", x="Subject Received Study Drug") + theme_bw() + theme(panel.grid.minor=element_blank(),panel.grid.major=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) Here's the rest of the code: [x] ANSWER: dat had originally been melted – changed dat to be unmelted (and created dat.m to be the melted version), and supplied dat.mto geom_point and geom_segment for adding points to bars. data: structure(list(Patient.ID = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L), .Label = c("13", "5", "14", "11", "6", "2", "7", "12", "3", "8", "1", "10", "4", "15", "9"), class = "factor"), Disease.Stage = c(1L, 2L, 4L, 1L, 2L, 3L, 1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 3L, 1L, 2L, 4L, 1L, 2L, 3L, 1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 3L, 1L, 2L, 4L, 1L, 2L, 3L, 1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 3L, 1L, 2L, 4L, 1L, 2L, 3L, 1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 3L), Response.Start = c(15, 40, 51, 26, 36, 26, 32, 43, 67, 80, 93, 105, 160, 34, 94, 15, 40, 51, 26, 36, 26, 32, 43, 67, 80, 93, 105, 160, 34, 94, 15, 40, 51, 26, 36, 26, 32, 43, 67, 80, 93, 105, 160, 34, 94, 15, 40, 51, 26, 36, 26, 32, 43, 67, 80, 93, 105, 160, 34, 94), Therapy.Length = c(74, 84, 84, 108, 128, 185, 194, 198, 257, 274, 293, 311, 325, 336, 357, 74, 84, 84, 108, 128, 185, 194, 198, 257, 274, 293, 311, 325, 336, 357, 74, 84, 84, 108, 128, 185, 194, 198, 257, 274, 293, 311, 325, 336, 357, 74, 84, 84, 108, 128, 185, 194, 198, 257, 274, 293, 311, 325, 336, 357), Continued = c(1, 1, 1, NA, 1, NA, 1, NA, 1, NA, 1, NA, 1, NA, 1, 1, 1, 1, NA, 1, NA, 1, NA, 1, NA, 1, NA, 1, NA, 1, 1, 1, 1, NA, 1, NA, 1, NA, 1, NA, 1, NA, 1, NA, 1, 1, 1, 1, NA, 1, NA, 1, NA, 1, NA, 1, NA, 1, NA, 1), variable = structure(c(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, 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), .Label = c("Response.End", "Durable", "Complete", "Partial"), class = "factor"), value = c(40, 78, 78, 106, 89, 177, 108, 101, 138, 209, 279, 244, 311, 167, 271, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -1, NA, NA, NA, NA, NA, 40, NA, NA, NA, NA, 32, 43, NA, NA, 93, NA, 160, NA, 94, 15, NA, 51, 26, 36, 26, NA, NA, 67, 80, NA, 105, NA, 34, NA)), row.names = c(NA, -60L), .Names = c("Patient.ID", "Disease.Stage", "Response.Start", "Therapy.Length", "Continued", "variable", "value"), class = "data.frame")
How to make beanplot and boxplot in the same chart?
seasons <- structure(list(values = c(204, 339, 304, 434, 334, 212, 361, 102, 298, 369, 149, 227, 278, 199, 360, 211, 219, 209, 177, 299, 262, 285, 237, 227, 216, 229, 317, 321, 327, 123, 84, 321, 442, 263, 225, 290, 259, 219, 244, 325, 257, 672, 762, 381, 698, 578, 576, 386, 834, 790, 815, 736, 517, 556, 685, 781, 703, 1071, 537, 784, 753, 790, 489, 878, 433, 742, 638, 731, 1017, 850, 804, 612, 923, 1000, 855, 750, 921, 676, 621, 781, 703, 1054, 156, 312, 267, 152, 352, 155, 215, 184, 186, 221, 352, 183, 307, 353, 507, 255, 159, 109, 343, 377, 209, 260, 193, 231, 111, 167, 233, 360, 488, 347, 208, 178, 371, 276, 263, 166, 486, 119, 153, 315, 226, 158, 142, 78, 75, 156, 53, 103, 141, 94, 94, 55, 84, 35, 82, 65, 150, 30, 201, 184, 94, 119, 150, 70, 63, 50, 74, 160, 49, 52, 135, 105, 129, 75, 83, 85, 84, 85, 77, 147, 100, 46), ind = 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 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("spring", "summer", "autumn", "winter"), class = "factor", scores = structure(c(3, 1, 2, 4), .Dim = 4L, .Dimnames = list(c("autumn", "spring", "summer", "winter"))))), .Names = c("values", "ind"), row.names = c(NA, -164L), class = "data.frame") I made a boxplot and beanplot below. boxplot(seasons$values~seasons$ind, ylim= c(0,1200)) beanplot(seasons$values~seasons$ind, ylim= c(0,1200), col = c("#CAB2D6", "#33A02C", "#B2DF8A"), border = "#CAB2D6", side="second") I want to make a chart containing these boxplot and beanplot at the same time. This would make comparison easy. Thai is why I made same ylim on both plots. Is there any way I can do?
With beanplot package, use add=TRUE: boxplot(seasons$values~seasons$ind, ylim= c(0,1200)) beanplot(seasons$values~seasons$ind, ylim= c(0,1200), col = c("#CAB2D6", "#33A02C", "#B2DF8A"), border = "#CAB2D6", side="second", add=T)
Try with ggplot: ggplot(seasons, aes(x=ind, y=values))+geom_boxplot()+geom_violin(fill='lightblue', alpha=0.5)+geom_jitter(position = position_jitter(width = .1))