Add a constant line to all plots in facet_wrap() - r

I have the following code:
p1 <- ggplot(df_test, aes(x=AA_Number,y=Energy_Profile,col='red')) + geom_line() + facet_wrap(~Model, ncol=3) + geom_hline(yintercept=-0.03, colour='blue') + geom_line(data=df_templates, colour="green")
print(p1)
It produces this output:
I am having trouble merging the data in green into one plot and then plotting it over the other three plots in red.
Essentially the plot in green is my constant and I want to see how my data in red varies from the constant, by overlaying the data in green on top of each of the plots in red.
Anybody have any ideas?
Data:
df_test:
structure(list(Model = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1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L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 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("102",
"103", "104", "105", "107", "108", "109", "118", "141", "143",
"144", "145", "14x", "161", "162", "163", "164", "165", "167",
"168", "169", "2", "21", "22", "25", "26", "27", "3", "31", "310",
"32", "36", "37", "39", "51", "510", "52", "53", "54", "57",
"61", "62", "64", "65", "66", "67", "68", "81", "84", "88", "910",
"93", "95", "97"), class = "factor"), AA_Number = 1:614, AA = structure(c(1L,
15L, 1L, 10L, 11L, 4L, 18L, 18L, 1L, 9L, 12L, 7L, 19L, 11L, 14L,
16L, 4L, 4L, 10L, 11L, 16L, 9L, 11L, 16L, 1L, 18L, 2L, 2L, 10L,
1L, 2L, 11L, 18L, 8L, 14L, 4L, 19L, 15L, 11L, 6L, 14L, 4L, 1L,
6L, 1L, 2L, 13L, 11L, 6L, 19L, 1L, 6L, 1L, 17L, 6L, 19L, 10L,
15L, 4L, 6L, 5L, 2L, 2L, 1L, 6L, 18L, 4L, 5L, 12L, 17L, 5L, 8L,
2L, 3L, 4L, 14L, 5L, 13L, 3L, 2L, 5L, 13L, 8L, 8L, 7L, 4L, 7L,
5L, 15L, 13L, 3L, 1L, 6L, 4L, 13L, 4L, 4L, 9L, 13L, 15L, 15L,
9L, 13L, 7L, 5L, 8L, 1L, 2L, 5L, 15L, 2L, 5L, 2L, 14L, 1L, 16L,
2L, 7L, 8L, 4L, 9L, 6L, 9L, 6L, 19L, 1L, 19L, 13L, 10L, 6L, 6L,
16L, 10L, 16L, 6L, 8L, 11L, 2L, 16L, 9L, 15L, 18L, 3L, 10L, 14L,
19L, 18L, 3L, 1L, 13L, 7L, 12L, 9L, 6L, 5L, 6L, 9L, 14L, 11L,
16L, 10L, 3L, 19L, 11L, 9L, 14L, 1L, 7L, 19L, 7L, 3L, 7L, 5L,
2L, 9L, 2L, 10L, 11L, 7L, 5L, 7L, 16L, 14L, 7L, 6L, 3L, 7L, 7L,
14L, 3L, 7L, 4L, 10L, 17L, 10L, 19L, 9L, 8L, 9L, 1L, 14L, 8L,
14L, 10L, 13L, 4L, 9L, 19L, 7L, 5L, 4L, 10L, 6L, 9L, 19L, 19L,
14L, 19L, 15L, 14L, 17L, 6L, 1L, 1L, 10L, 7L, 1L, 11L, 19L, 16L,
19L, 14L, 16L, 10L, 11L, 6L, 15L, 9L, 10L, 10L, 16L, 9L, 14L,
14L, 7L, 15L, 5L, 1L, 7L, 5L, 2L, 10L, 2L, 19L, 11L, 7L, 11L,
7L, 10L, 19L, 15L, 11L, 11L, 5L, 16L, 7L, 4L, 10L, 18L, 1L, 19L,
10L, 11L, 9L, 19L, 12L, 14L, 14L, 11L, 14L, 4L, 6L, 3L, 16L,
1L, 1L, 10L, 17L, 5L, 5L, 10L, 1L, 4L, 1L, 5L, 15L, 15L, 13L,
4L, 14L, 2L, 11L, 4L, 17L, 7L, 11L, 1L, 1L, 15L, 1L, 10L, 11L,
4L, 18L, 18L, 1L, 9L, 12L, 7L, 19L, 11L, 14L, 16L, 4L, 4L, 10L,
11L, 16L, 9L, 11L, 16L, 1L, 18L, 2L, 2L, 10L, 1L, 2L, 11L, 18L,
8L, 14L, 4L, 19L, 15L, 11L, 6L, 14L, 4L, 1L, 6L, 1L, 2L, 13L,
11L, 6L, 19L, 1L, 6L, 1L, 17L, 6L, 19L, 10L, 15L, 4L, 6L, 5L,
2L, 2L, 1L, 6L, 18L, 4L, 5L, 12L, 17L, 5L, 8L, 2L, 3L, 4L, 14L,
5L, 13L, 3L, 2L, 5L, 13L, 8L, 8L, 7L, 4L, 7L, 5L, 15L, 13L, 3L,
1L, 6L, 4L, 13L, 4L, 4L, 9L, 13L, 15L, 15L, 9L, 13L, 7L, 5L,
8L, 1L, 2L, 5L, 15L, 2L, 5L, 2L, 14L, 1L, 16L, 2L, 7L, 8L, 4L,
9L, 6L, 9L, 6L, 19L, 1L, 19L, 13L, 10L, 6L, 6L, 16L, 10L, 16L,
6L, 8L, 11L, 2L, 16L, 9L, 15L, 18L, 3L, 10L, 14L, 19L, 18L, 3L,
1L, 13L, 7L, 12L, 9L, 6L, 5L, 6L, 9L, 14L, 11L, 16L, 10L, 3L,
19L, 11L, 9L, 14L, 1L, 7L, 19L, 7L, 3L, 7L, 5L, 2L, 9L, 2L, 10L,
11L, 7L, 5L, 7L, 16L, 14L, 7L, 6L, 3L, 7L, 7L, 14L, 3L, 7L, 4L,
10L, 17L, 10L, 19L, 9L, 8L, 9L, 1L, 14L, 8L, 14L, 10L, 13L, 4L,
9L, 19L, 7L, 5L, 4L, 10L, 6L, 9L, 19L, 19L, 14L, 19L, 15L, 14L,
17L, 6L, 1L, 1L, 10L, 7L, 1L, 11L, 19L, 16L, 19L, 14L, 16L, 10L,
11L, 6L, 15L, 9L, 10L, 10L, 16L, 9L, 14L, 14L, 7L, 15L, 5L, 1L,
7L, 5L, 2L, 10L, 2L, 19L, 11L, 7L, 11L, 7L, 10L, 19L, 15L, 11L,
11L, 5L, 16L, 7L, 4L, 10L, 18L, 1L, 19L, 10L, 11L, 9L, 19L, 12L,
14L, 14L, 11L, 14L, 4L, 6L, 3L, 16L, 1L, 1L, 10L, 17L, 5L, 5L,
10L, 1L, 4L, 1L, 5L, 15L, 15L, 13L, 4L, 14L, 2L, 11L, 4L, 17L,
7L, 11L, 1L), .Label = c("ALA", "ARG", "ASN", "ASP", "GLN", "GLU",
"GLY", "HIS", "ILE", "LEU", "LYS", "MET", "PHE", "PRO", "SER",
"THR", "TRP", "TYR", "VAL"), class = "factor"), Energy_Profile = c(-0.017,
-0.018, -0.02, -0.021, -0.022, -0.024, -0.026, -0.027, -0.028,
-0.028, -0.028, -0.026, -0.025, -0.024, -0.022, -0.021, -0.02,
-0.02, -0.021, -0.022, -0.023, -0.024, -0.024, -0.026, -0.026,
-0.025, -0.024, -0.023, -0.023, -0.022, -0.021, -0.02, -0.02,
-0.02, -0.019, -0.018, -0.019, -0.019, -0.02, -0.02, -0.022,
-0.023, -0.024, -0.025, -0.026, -0.027, -0.027, -0.026, -0.026,
-0.027, -0.027, -0.027, -0.027, -0.026, -0.026, -0.025, -0.024,
-0.022, -0.021, -0.021, -0.02, -0.02, -0.021, -0.023, -0.024,
-0.025, -0.026, -0.027, -0.028, -0.028, -0.028, -0.027, -0.027,
-0.027, -0.027, -0.027, -0.026, -0.026, -0.026, -0.025, -0.025,
-0.024, -0.024, -0.024, -0.024, -0.024, -0.025, -0.026, -0.027,
-0.028, -0.028, -0.028, -0.028, -0.028, -0.027, -0.026, -0.025,
-0.026, -0.026, -0.025, -0.026, -0.027, -0.027, -0.027, -0.025,
-0.024, -0.023, -0.022, -0.02, -0.019, -0.018, -0.019, -0.019,
-0.02, -0.021, -0.022, -0.023, -0.024, -0.025, -0.027, -0.029,
-0.031, -0.034, -0.035, -0.035, -0.035, -0.034, -0.032, -0.03,
-0.026, -0.024, -0.023, -0.022, -0.022, -0.023, -0.024, -0.027,
-0.029, -0.032, -0.035, -0.036, -0.037, -0.037, -0.037, -0.036,
-0.035, -0.033, -0.032, -0.032, -0.031, -0.031, -0.032, -0.033,
-0.034, -0.036, -0.038, -0.04, -0.041, -0.042, -0.044, -0.045,
-0.044, -0.043, -0.041, -0.04, -0.037, -0.035, -0.032, -0.031,
-0.029, -0.029, -0.029, -0.029, -0.03, -0.031, -0.031, -0.031,
-0.031, -0.029, -0.027, -0.025, -0.023, -0.021, -0.019, -0.018,
-0.017, -0.018, -0.019, -0.023, -0.026, -0.031, -0.036, -0.04,
-0.043, -0.045, -0.044, -0.043, -0.039, -0.037, -0.033, -0.03,
-0.028, -0.027, -0.027, -0.026, -0.025, -0.025, -0.025, -0.024,
-0.023, -0.023, -0.024, -0.025, -0.026, -0.028, -0.029, -0.029,
-0.029, -0.029, -0.029, -0.029, -0.028, -0.029, -0.031, -0.033,
-0.034, -0.036, -0.037, -0.039, -0.039, -0.039, -0.038, -0.037,
-0.036, -0.035, -0.033, -0.033, -0.032, -0.031, -0.03, -0.029,
-0.028, -0.027, -0.025, -0.023, -0.022, -0.021, -0.02, -0.019,
-0.019, -0.018, -0.019, -0.018, -0.018, -0.017, -0.017, -0.016,
-0.015, -0.014, -0.013, -0.012, -0.011, -0.011, -0.013, -0.014,
-0.017, -0.02, -0.024, -0.027, -0.03, -0.031, -0.032, -0.03,
-0.029, -0.027, -0.025, -0.023, -0.021, -0.021, -0.022, -0.023,
-0.025, -0.026, -0.028, -0.031, -0.033, -0.035, -0.035, -0.035,
-0.036, -0.036, -0.034, -0.033, -0.031, -0.028, -0.025, -0.023,
-0.021, -0.019, -0.017, -0.015, -0.014, -0.013, -0.012, -0.011,
-0.011, -0.011, -0.012, -0.013, -0.016, -0.018, -0.02, -0.022,
-0.024, -0.026, -0.027, -0.027, -0.026, -0.025, -0.024, -0.023,
-0.022, -0.021, -0.02, -0.021, -0.022, -0.023, -0.024, -0.024,
-0.024, -0.025, -0.025, -0.023, -0.022, -0.021, -0.021, -0.02,
-0.018, -0.018, -0.018, -0.017, -0.016, -0.016, -0.016, -0.016,
-0.016, -0.017, -0.019, -0.02, -0.021, -0.023, -0.024, -0.026,
-0.027, -0.027, -0.027, -0.028, -0.028, -0.028, -0.028, -0.027,
-0.026, -0.025, -0.023, -0.022, -0.02, -0.02, -0.02, -0.02, -0.021,
-0.022, -0.023, -0.025, -0.025, -0.026, -0.026, -0.026, -0.026,
-0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025,
-0.025, -0.025, -0.024, -0.024, -0.024, -0.024, -0.025, -0.025,
-0.026, -0.027, -0.028, -0.028, -0.028, -0.028, -0.027, -0.027,
-0.025, -0.025, -0.026, -0.026, -0.025, -0.026, -0.027, -0.027,
-0.027, -0.025, -0.024, -0.023, -0.022, -0.02, -0.019, -0.018,
-0.019, -0.019, -0.02, -0.021, -0.022, -0.023, -0.024, -0.025,
-0.027, -0.029, -0.031, -0.034, -0.035, -0.036, -0.035, -0.034,
-0.032, -0.03, -0.027, -0.025, -0.024, -0.023, -0.023, -0.024,
-0.025, -0.027, -0.03, -0.032, -0.035, -0.036, -0.037, -0.037,
-0.037, -0.036, -0.035, -0.033, -0.032, -0.032, -0.031, -0.031,
-0.032, -0.033, -0.034, -0.036, -0.038, -0.04, -0.041, -0.042,
-0.044, -0.044, -0.044, -0.043, -0.041, -0.039, -0.037, -0.034,
-0.031, -0.03, -0.029, -0.028, -0.029, -0.029, -0.03, -0.031,
-0.031, -0.032, -0.031, -0.029, -0.027, -0.025, -0.023, -0.021,
-0.019, -0.018, -0.017, -0.018, -0.019, -0.023, -0.026, -0.031,
-0.036, -0.04, -0.043, -0.045, -0.044, -0.043, -0.039, -0.037,
-0.033, -0.03, -0.028, -0.027, -0.026, -0.026, -0.025, -0.025,
-0.024, -0.023, -0.023, -0.024, -0.026, -0.027, -0.029, -0.03,
-0.031, -0.031, -0.031, -0.03, -0.03, -0.029, -0.028, -0.029,
-0.029, -0.031, -0.033, -0.035, -0.036, -0.039, -0.039, -0.04,
-0.039, -0.039, -0.038, -0.037, -0.037, -0.036, -0.036, -0.035,
-0.034, -0.032, -0.031, -0.028, -0.026, -0.024, -0.023, -0.022,
-0.022, -0.021, -0.022, -0.022, -0.022, -0.022, -0.022, -0.021,
-0.021, -0.02, -0.019, -0.018, -0.017, -0.015, -0.014, -0.014,
-0.015, -0.017, -0.02, -0.023, -0.026, -0.029, -0.032, -0.033,
-0.033, -0.031, -0.03, -0.029, -0.027, -0.025, -0.023, -0.023,
-0.024, -0.025, -0.027, -0.029, -0.032, -0.034, -0.037, -0.039,
-0.04, -0.041, -0.041, -0.04, -0.039, -0.036, -0.033, -0.03,
-0.026, -0.023, -0.021, -0.019, -0.017, -0.016, -0.015, -0.015,
-0.014, -0.014, -0.013, -0.013)), .Names = c("Model", "AA_Number",
"AA", "Energy_Profile"), row.names = c(NA, 614L), class = "data.frame")
df_templates:
structure(list(Model = 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,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2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L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("2kqx_renumberedA",
"2kqx_renumberedB", "3lz8_renumbered"), class = "factor"), AA_Number = c(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, 53L, 54L, 55L, 56L,
57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L,
70L, 71L, 72L, 73L, 310L, 311L, 312L, 313L, 314L, 315L, 316L,
317L, 318L, 319L, 320L, 321L, 322L, 323L, 324L, 325L, 326L, 327L,
328L, 329L, 330L, 331L, 332L, 333L, 334L, 335L, 336L, 337L, 338L,
339L, 340L, 341L, 342L, 343L, 344L, 345L, 346L, 347L, 348L, 349L,
350L, 351L, 352L, 353L, 354L, 355L, 356L, 357L, 358L, 359L, 360L,
361L, 362L, 363L, 364L, 365L, 366L, 367L, 368L, 369L, 370L, 371L,
372L, 373L, 374L, 375L, 376L, 377L, 378L, 379L, 380L, 115L, 116L,
117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L,
128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L,
139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L,
150L, 148L, 150L, 151L, 153L, 154L, 155L, 156L, 157L, 158L, 159L,
160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L,
171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 178L, 180L,
181L, 182L, 183L, 187L, 188L, 189L, 190L, 191L, 192L, 193L, 194L,
195L, 196L, 197L, 198L, 199L, 200L, 201L, 202L, 203L, 204L, 205L,
206L, 207L, 208L, 209L, 210L, 211L, 212L, 213L, 214L, 215L, 216L,
217L, 218L, 219L, 220L, 221L, 219L, 221L, 222L, 223L, 224L, 225L,
226L, 227L, 228L, 229L, 230L, 231L, 232L, 233L, 234L, 235L, 236L,
237L, 238L, 239L, 240L, 241L, 242L, 243L, 244L, 245L, 246L, 247L,
248L, 249L, 250L, 251L, 252L, 253L, 254L, 255L, 256L, 257L, 258L,
259L, 260L, 261L, 262L, 263L, 264L, 265L, 266L, 267L, 268L, 269L,
270L, 271L, 272L, 273L, 274L, 275L, 276L, 277L, 278L, 279L, 278L,
280L, 281L, 282L, 283L, 284L, 285L, 286L, 287L, 288L, 289L, 290L,
291L, 292L, 293L, 294L, 295L, 296L, 297L, 298L, 299L, 300L, 301L,
302L, 303L, 301L, 303L, 304L, 422L, 423L, 424L, 425L, 426L, 427L,
428L, 429L, 430L, 431L, 432L, 433L, 434L, 435L, 436L, 437L, 438L,
439L, 440L, 441L, 442L, 443L, 444L, 445L, 446L, 447L, 448L, 449L,
450L, 451L, 452L, 453L, 454L, 455L, 456L, 457L, 458L, 459L, 460L,
151L, 461L, 462L, 463L, 464L, 465L, 466L, 467L, 158L, 467L, 468L,
469L, 470L, 471L, 472L, 473L, 474L, 475L, 476L, 477L, 478L, 479L,
480L, 481L, 482L, 483L, 484L, 485L, 176L, 485L, 486L, 487L, 488L,
489L, 490L, 491L, 492L, 493L, 495L, 496L, 497L, 498L, 499L, 500L,
501L, 502L, 503L, 504L, 505L, 506L, 507L, 508L, 509L, 510L, 511L,
512L, 513L, 204L, 513L, 514L, 515L, 516L, 517L, 518L, 519L, 520L,
521L, 522L, 523L, 524L, 525L, 526L, 527L, 528L, 529L, 530L, 531L,
532L, 533L, 534L, 535L, 536L, 537L, 538L, 539L, 230L, 539L, 540L,
541L, 542L, 543L, 544L, 545L, 546L, 547L, 548L, 549L, 550L, 551L,
552L, 553L, 554L, 555L, 556L, 557L, 558L, 559L, 560L, 561L, 562L,
563L, 564L, 565L, 566L, 567L, 568L, 571L, 572L, 573L, 574L, 575L,
576L, 577L, 578L, 579L, 580L, 581L, 582L, 583L, 584L, 585L, 586L,
587L, 588L, 589L, 590L, 591L, 592L, 593L, 594L, 595L, 596L, 597L,
598L, 599L, 600L, 601L, 602L, 603L, 604L, 605L, 606L, 607L, 608L,
609L, 610L, 611L), AA = structure(c(6L, 10L, 11L, 4L, 18L, 18L,
1L, 9L, 12L, 7L, 19L, 11L, 14L, 16L, 4L, 4L, 10L, 11L, 16L, 9L,
11L, 16L, 1L, 18L, 2L, 2L, 10L, 1L, 2L, 11L, 18L, 8L, 14L, 4L,
19L, 15L, 11L, 6L, 14L, 4L, 1L, 6L, 1L, 2L, 13L, 11L, 6L, 19L,
1L, 6L, 1L, 17L, 6L, 19L, 10L, 15L, 4L, 6L, 5L, 2L, 2L, 1L, 6L,
18L, 4L, 5L, 12L, 17L, 5L, 8L, 2L, 6L, 10L, 11L, 4L, 18L, 18L,
1L, 9L, 12L, 7L, 19L, 11L, 14L, 16L, 4L, 4L, 10L, 11L, 16L, 9L,
11L, 16L, 1L, 18L, 2L, 2L, 10L, 1L, 2L, 11L, 18L, 8L, 14L, 4L,
19L, 15L, 11L, 6L, 14L, 4L, 1L, 6L, 1L, 2L, 13L, 11L, 6L, 19L,
1L, 6L, 1L, 17L, 6L, 19L, 10L, 15L, 4L, 6L, 5L, 2L, 2L, 1L, 6L,
18L, 4L, 5L, 12L, 17L, 5L, 8L, 2L, 1L, 1L, 2L, 7L, 8L, 4L, 10L,
6L, 9L, 6L, 19L, 1L, 19L, 13L, 10L, 6L, 6L, 16L, 10L, 1L, 6L,
5L, 16L, 2L, 16L, 9L, 15L, 18L, 3L, 10L, 14L, 19L, 18L, 3L, 19L,
13L, 13L, 13L, 7L, 9L, 6L, 15L, 6L, 16L, 14L, 11L, 16L, 10L,
3L, 19L, 11L, 9L, 14L, 1L, 7L, 19L, 19L, 4L, 7L, 5L, 2L, 9L,
2L, 10L, 11L, 7L, 5L, 5L, 7L, 16L, 14L, 7L, 7L, 14L, 3L, 7L,
4L, 10L, 17L, 10L, 19L, 9L, 8L, 9L, 1L, 14L, 8L, 14L, 10L, 13L,
4L, 9L, 19L, 7L, 8L, 3L, 10L, 6L, 9L, 19L, 10L, 14L, 10L, 1L,
14L, 17L, 17L, 17L, 6L, 1L, 1L, 10L, 7L, 1L, 11L, 19L, 16L, 19L,
14L, 16L, 10L, 11L, 6L, 15L, 9L, 10L, 10L, 16L, 19L, 14L, 14L,
7L, 15L, 5L, 1L, 7L, 5L, 2L, 10L, 2L, 9L, 11L, 7L, 11L, 7L, 10L,
19L, 15L, 11L, 16L, 8L, 16L, 7L, 4L, 10L, 13L, 1L, 19L, 9L, 11L,
9L, 19L, 12L, 14L, 16L, 11L, 14L, 14L, 4L, 6L, 11L, 1L, 2L, 6L,
10L, 17L, 5L, 5L, 10L, 1L, 1L, 1L, 6L, 1L, 15L, 13L, 4L, 14L,
2L, 11L, 16L, 16L, 16L, 17L, 1L, 1L, 2L, 7L, 8L, 4L, 10L, 6L,
9L, 6L, 19L, 1L, 19L, 13L, 10L, 6L, 6L, 16L, 10L, 1L, 6L, 5L,
16L, 2L, 16L, 9L, 15L, 18L, 3L, 10L, 14L, 19L, 18L, 3L, 19L,
13L, 7L, 12L, 9L, 9L, 6L, 15L, 6L, 16L, 14L, 11L, 16L, 16L, 16L,
10L, 3L, 19L, 11L, 9L, 14L, 1L, 7L, 19L, 19L, 4L, 7L, 5L, 2L,
9L, 2L, 10L, 11L, 11L, 11L, 7L, 5L, 7L, 16L, 14L, 7L, 6L, 3L,
7L, 14L, 3L, 7L, 4L, 10L, 17L, 10L, 19L, 9L, 8L, 9L, 1L, 14L,
8L, 14L, 10L, 13L, 4L, 4L, 4L, 9L, 19L, 7L, 8L, 3L, 10L, 6L,
9L, 19L, 10L, 14L, 10L, 1L, 14L, 17L, 6L, 1L, 1L, 10L, 7L, 1L,
11L, 19L, 16L, 19L, 14L, 14L, 14L, 16L, 10L, 11L, 6L, 15L, 9L,
10L, 10L, 16L, 19L, 14L, 14L, 7L, 15L, 5L, 1L, 7L, 5L, 2L, 10L,
2L, 9L, 11L, 7L, 11L, 7L, 10L, 19L, 15L, 8L, 16L, 7L, 4L, 10L,
13L, 1L, 19L, 9L, 11L, 9L, 19L, 12L, 14L, 16L, 11L, 14L, 4L,
6L, 11L, 1L, 2L, 6L, 10L, 17L, 5L, 5L, 10L, 1L, 1L, 1L, 6L, 1L,
15L, 13L, 4L, 14L, 2L, 11L, 16L, 17L), .Label = c("ALA", "ARG",
"ASN", "ASP", "GLN", "GLU", "GLY", "HIS", "ILE", "LEU", "LYS",
"MET", "PHE", "PRO", "SER", "THR", "TRP", "TYR", "VAL"), class = "factor"),
Energy_Profile = c(-0.018, -0.019, -0.019, -0.02, -0.022,
-0.023, -0.024, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025,
-0.026, -0.025, -0.026, -0.028, -0.029, -0.029, -0.027, -0.026,
-0.026, -0.026, -0.025, -0.023, -0.023, -0.024, -0.025, -0.025,
-0.024, -0.025, -0.025, -0.026, -0.025, -0.024, -0.024, -0.024,
-0.023, -0.024, -0.025, -0.025, -0.027, -0.028, -0.029, -0.029,
-0.029, -0.028, -0.027, -0.025, -0.023, -0.022, -0.022, -0.022,
-0.023, -0.024, -0.025, -0.027, -0.028, -0.029, -0.03, -0.03,
-0.032, -0.032, -0.033, -0.033, -0.033, -0.033, -0.033, -0.032,
-0.031, -0.029, -0.018, -0.019, -0.019, -0.02, -0.022, -0.023,
-0.024, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.026,
-0.025, -0.026, -0.028, -0.029, -0.029, -0.027, -0.026, -0.026,
-0.026, -0.025, -0.023, -0.023, -0.024, -0.025, -0.025, -0.024,
-0.025, -0.025, -0.026, -0.025, -0.024, -0.024, -0.024, -0.023,
-0.024, -0.025, -0.025, -0.027, -0.028, -0.029, -0.029, -0.029,
-0.028, -0.027, -0.025, -0.023, -0.022, -0.022, -0.022, -0.023,
-0.024, -0.025, -0.027, -0.028, -0.029, -0.03, -0.03, -0.032,
-0.032, -0.033, -0.033, -0.033, -0.033, -0.033, -0.032, -0.031,
-0.029, -0.015, -0.019, -0.023, -0.026, -0.029, -0.032, -0.037,
-0.04, -0.044, -0.046, -0.047, -0.046, -0.046, -0.044, -0.042,
-0.039, -0.038, -0.037, -0.037, -0.038, -0.038, -0.039, -0.041,
-0.042, -0.043, -0.044, -0.044, -0.045, -0.045, -0.043, -0.035,
-0.024, -0.01, 0.0021, 0.014, 0.027, 0.037, 0.035, 0.026,
0.015, 0.0039, -0.008, -0.021, -0.032, -0.039, -0.042, -0.045,
-0.048, -0.049, -0.05, -0.049, -0.048, -0.046, -0.043, -0.04,
-0.039, -0.037, -0.036, -0.037, -0.037, -0.037, -0.035, -0.032,
-0.028, -0.024, -0.019, -0.012, -0.0097, -0.011, -0.013,
-0.014, -0.017, -0.023, -0.03, -0.035, -0.04, -0.044, -0.049,
-0.053, -0.055, -0.055, -0.053, -0.05, -0.048, -0.045, -0.043,
-0.041, -0.04, -0.041, -0.041, -0.041, -0.041, -0.042, -0.042,
-0.043, -0.044, -0.046, -0.047, -0.048, -0.049, -0.047, -0.042,
-0.038, -0.032, -0.027, -0.021, -0.015, -0.012, -0.011, -0.012,
-0.013, -0.015, -0.018, -0.024, -0.029, -0.034, -0.037, -0.04,
-0.043, -0.044, -0.045, -0.044, -0.045, -0.045, -0.045, -0.045,
-0.045, -0.043, -0.041, -0.039, -0.036, -0.034, -0.033, -0.033,
-0.034, -0.035, -0.036, -0.038, -0.039, -0.039, -0.04, -0.039,
-0.039, -0.039, -0.039, -0.04, -0.04, -0.039, -0.04, -0.041,
-0.042, -0.043, -0.044, -0.046, -0.048, -0.049, -0.05, -0.05,
-0.049, -0.046, -0.043, -0.037, -0.031, -0.025, -0.021, -0.017,
-0.015, -0.016, -0.021, -0.028, -0.034, -0.039, -0.044, -0.049,
-0.051, -0.049, -0.047, -0.045, -0.043, -0.04, -0.038, -0.036,
-0.035, -0.035, -0.033, -0.03, -0.026, -0.022, -0.017, -0.012,
-0.0058, -0.002, -0.0018, -0.0043, -0.0073, -0.012, -0.017,
-0.023, -0.029, -0.034, -0.038, -0.041, -0.044, -0.046, -0.047,
-0.046, -0.045, -0.043, -0.041, -0.038, -0.036, -0.034, -0.034,
-0.035, -0.035, -0.037, -0.039, -0.041, -0.043, -0.045, -0.046,
-0.047, -0.048, -0.05, -0.052, -0.054, -0.054, -0.051, -0.044,
-0.037, -0.027, -0.017, -0.0058, 0.0041, 0.0065, 0.0052,
0.0053, 0.0048, 0.0059, 0.0058, 0.0057, 0.0074, 0.007, 0.0014,
-0.0049, -0.015, -0.023, -0.031, -0.037, -0.04, -0.039, -0.039,
-0.037, -0.036, -0.034, -0.029, -0.023, -0.017, -0.009, -0.0017,
0.007, 0.011, 0.01, 0.0045, -0.0012, -0.0089, -0.017, -0.025,
-0.031, -0.033, -0.034, -0.035, -0.036, -0.038, -0.04, -0.042,
-0.046, -0.049, -0.052, -0.052, -0.051, -0.049, -0.046, -0.042,
-0.035, -0.027, -0.019, -0.013, -0.0065, -6.1e-05, 0.0045,
0.003, -0.0013, -0.0071, -0.014, -0.021, -0.029, -0.036,
-0.041, -0.044, -0.046, -0.046, -0.045, -0.044, -0.043, -0.041,
-0.039, -0.038, -0.038, -0.038, -0.039, -0.039, -0.038, -0.034,
-0.03, -0.025, -0.02, -0.013, -0.0082, -0.008, -0.011, -0.016,
-0.021, -0.028, -0.034, -0.04, -0.042, -0.043, -0.041, -0.04,
-0.038, -0.035, -0.033, -0.032, -0.032, -0.033, -0.034, -0.035,
-0.037, -0.038, -0.038, -0.039, -0.038, -0.038, -0.037, -0.037,
-0.036, -0.037, -0.036, -0.037, -0.039, -0.041, -0.043, -0.045,
-0.046, -0.048, -0.048, -0.047, -0.045, -0.043, -0.04, -0.038,
-0.037, -0.036, -0.037, -0.039, -0.041, -0.043, -0.046, -0.047,
-0.048, -0.048, -0.048, -0.046, -0.042, -0.04, -0.038, -0.036,
-0.033, -0.032, -0.031, -0.032, -0.032, -0.033, -0.034, -0.035,
-0.036, -0.037, -0.038, -0.039)), .Names = c("Model", "AA_Number",
"AA", "Energy_Profile"), class = "data.frame", row.names = c(NA,
-532L))
In my df_test data I provided here, I could only put one plot as I reach the character limit.

Your data frame df_templates is also faceted because it has the same column Model as given in facet_wrap(). If you rename this column, for example, to Model2
colnames(df_templates)<-c("AA_Number","AA","Energy_Profile","Model2")
Then this data frame is not faceted.
ggplot(df_test, aes(x=AA_Number,y=Energy_Profile,col='red')) + geom_line() +
geom_hline(yintercept=-0.03, colour='blue') +
geom_line(data=df_templates,colour="green")+
facet_wrap(~Model,ncol=3)

Related

Setting legend with Plotly

I am working in R with Plotly. Below you can see my bar chart and data.
t_df3<-data.frame(structure(list(deciles = c(0.001, 0.002, 0.003, 0.004, 0.005,
0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004,
0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003,
0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002,
0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001,
0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01,
0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009,
0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008,
0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007,
0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006,
0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005,
0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004,
0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003,
0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01), variable.x = 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, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("Food",
"Alcoholic Beverages", "Clothing", "Housing Water", "Furnishings",
"Health", "Transport", "Communication", "Recreation", "Education",
"Restaurants", "Miscellaneous Goods"), class = "factor"), value.x = c(958.823102803738,
1292.9, 1575.2, 1807, 1911.8,
2041.2, 2376.83, 2723, 3161.9,
4130.448, 120.0, 304, 246.4,
249.8, 285, 382.1, 494.5,
691.6, 787, 948.8, 11.9,
22.9085309734513, 62, 77.3, 201.184778761062,
239.961132743363, 330, 588.19178761062, 766.9,
2117.2, 46, 91.0239292035398, 166.0,
329.5, 525, 772.3, 1060.3112920354,
1297.9, 1680, 3963.0, 106.8,
175.2, 228, 295.961379310345, 300.2,
404.8, 447, 496.528551724138, 731.2,
1916.6995862069, 68, 71.9, 111.1,
154.9, 201, 253.49, 248.205798165138,
247.0, 421, 850.106642201835, 19.2,
14.9, 40, 96.0269734513274, 200.4,
354.0, 457, 745.2, 929.6,
2654.9, 4.9, 12.6, 50.6,
77.0251034482759, 168., 259.1, 364.2,
571.067586206897, 828.672, 1452.7, 6.75,
4.5, 5.41241379310345, 25.2, 46.1,
68.2, 125.2, 104.550620689655, 258.9,
951.36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15.5, 14.1,
44.5, 79.2, 119.3, 216.31,
316.8, 577.3, 901.7, 1897.1,
70.5037168141593, 97.10, 136.4, 189.5,
250.2, 333.9, 439.5, 583.7,
725.7, 1283.5), color.x = 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, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("blue",
"cyan", "darkgreen", "red", "brown", "chartreuse", "green", "purple",
"gold", "tomato", "darkturquoise", "forestgreen"), class = "factor"),
deciles = c(0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007,
0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006,
0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005,
0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004,
0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003,
0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002,
0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001,
0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01,
0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009,
0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008,
0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007,
0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006,
0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005,
0.006, 0.007, 0.008, 0.009, 0.01), variable = 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, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L), .Label = c("Food", "Alcoholic Beverages",
"Clothing", "Housing Water", "Furnishings", "Health", "Transport",
"Communication", "Recreation", "Education", "Restaurants",
"Miscellaneous Goods"), class = "factor"), value = c(145.111841584158,
195.676277227723, 238.405544554455, 273.52, 289.35,
308.935841584158, 359.719128712871, 412.1, 478.5,
625.117306930693, 96.9022702702703, 245.5, 198.9,
201.624648648649, 230.688432432432, 308.3, 399.0,
558.103135135135, 635.512216216216, 765.6, 11.9,
22.9085309734513, 62.3668672566372, 77.3, 201.1,
239.961132743363, 330.529486725664, 588.1, 766.9,
2117.21543362832, 46.402407079646, 91.0, 166.0,
329.568637168142, 525.05182300885, 772.307681415929, 1060.3112920354,
1297.90619469027, 1680.52311504425, 3963.01847787611, 106.808275862069,
175.229793103448, 228.774620689655, 295.961379310345, 300.238344827586,
404.881655172414, 447.85324137931, 496.528551724138, 731.241931034483,
1916.9, 68.8187889908257, 71.9220550458716, 111.1,
154.5, 201.2, 253.4, 248.2,
247.1, 421.0, 850.106642201835, 19.2,
14.94, 40.8, 96.0269734513274, 200.4,
354.09, 457.8, 745.21614159292, 929.6,
2654.1, 4.97, 12.6554482758621, 50.6,
77.0, 168.119172413793, 259.120551724138, 364.27,
571.07, 828.672, 1452.78786206897, 6.75310344827586,
4.55834482758621, 5.41241379310345, 25.2513103448276, 46.1428965517241,
68.2262068965517, 125.256827586207, 104.550620689655, 258.918620689655,
951.36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15.5453793103448, 14.1020689655172,
44.5, 79.2, 119.3, 216.3,
316.8, 577.3, 901.7, 1897.1,
70.5, 97.10, 136.421309734513, 189.527575221239,
250.2, 333.986336283186, 439.591433628319, 583.765805309735,
725.7, 1283.5), color = 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, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L), .Label = c("blue", "cyan", "darkgreen",
"red", "brown", "chartreuse", "green", "purple", "gold",
"tomato", "darkturquoise", "forestgreen"), class = "factor")), class = "data.frame", row.names = c(NA,
-120L)))
Above is artificial data and below is code for bar chart
library(plotly)
plt <- plot_ly(t_df3) %>%
add_trace(x = ~deciles, y = ~value.x, type = 'bar',name = 'Left-scale',marker = list(color = ~color.x), name = ~variable.x) %>%
add_trace(x = ~deciles, y = ~value, type = 'bar', name = 'Right-scale',marker = list(color = ~color), name = ~variable) %>%
layout(
xaxis = list(title = '',font = t_8),
yaxis = list(title = ''),
legend = list(x = 0.01, y = 0.99,font = t_10),
barmode = 'bar'
)
plt
On the left side, you can see a legend, but I am not satisfied with this legend and I want to change this legend, with a legend with the structure of the data (e.g. Food, Alcoholic Beverages, etc.). The structure is same for left and also right bars.So can anybody help with this ?
Would this be suitable?
If so, then this is how you can make this plot.
First, I melted the data.
t_df4 <- pivot_longer(t_df3, cols = c(value, value.x),
names_to = "group") %>%
mutate(group = ifelse(group == "value", "right_side",
"left_side"))
Then I plotted.
plot_ly(t_df4, x = ~list(deciles, group), y = ~value,
color = ~variable, colors = ~as.character(color), type = "bar") %>%
layout(barmode = "stack",
xaxis = list(title = ''),
yaxis = list(title = ''),
legend = list(x = 0.01, y = 0.99))

Why does strip text color from facet_wrap not correspond to element_text color?

Please, find my data sample p below.
Question: why does the strip text color from facet_wrap() not change as specified in element_text(colour)?
I have produced this plot
I would like the strip text color (UICC Stage I, II, III and IV) to match the color of the geom_point as specified in cols. It currently loads #E1B930 on all text items.
What is wrong with the following script?
cols = c("#E1B930", "#2C77BF","#E38072","#6DBCC3")
ggplot(p, aes(x=n.fjernet,y=os.neck)) + geom_point(aes(color=uiccc),shape=20, size=5,alpha=0.7) +
geom_quantile(quantiles = 0.5,col="black", size=1,linetype=2) + facet_wrap(.~factor(uiccc)) +
scale_fill_manual(values=cols) +
scale_colour_manual(values=cols) +
scale_x_continuous(breaks = seq(0,50, by=10), name="Lymph nodal yield") +
scale_y_continuous(name="Time to death") +
theme(strip.text.x = element_text(size=12,face="bold", colour = cols),
strip.text.y = element_text(size=12, face="bold"),
strip.background = element_rect(fill="white"),
legend.position="none")
My data
p <- structure(list(uiccc = structure(c(4L, 3L, 3L, 2L, 2L, 2L, 2L,
4L, 1L, 1L, 2L, 1L, 4L, 2L, 1L, 2L, 3L, 1L, 2L, 3L, 2L, 1L, 2L,
3L, 2L, 4L, 1L, 1L, 2L, 4L, 4L, 1L, 3L, 3L, 4L, 3L, 1L, 4L, 2L,
3L, 4L, 4L, 4L, 3L, 2L, 4L, 1L, 4L, 2L, 4L, 4L, 2L, 4L, 4L, 1L,
4L, 2L, 3L, 2L, 2L, 3L, 2L, 4L, 4L, 2L, 2L, 3L, 1L, 4L, 4L, 4L,
4L, 4L, 3L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 4L,
2L, 4L, 1L, 2L, 1L, 1L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 3L,
3L, 4L, 1L, 1L, 3L, 1L, 4L, 2L, 1L, 3L, 1L, 2L, 1L, 1L, 4L, 1L,
1L, 4L, 1L, 1L, 3L, 2L, 2L, 1L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 2L,
2L, 4L, 4L, 2L, 3L, 4L, 2L, 4L, 1L, 1L, 3L, 3L, 1L, 1L, 3L, 4L,
4L, 2L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 2L, 2L, 4L, 3L,
1L, 4L, 3L, 4L, 4L, 3L, 1L, 4L, 4L, 4L, 4L, 2L, 2L, 4L, 4L, 1L,
4L, 4L, 2L, 4L, 4L, 4L, 3L, 4L, 3L, 3L, 4L, 4L, 2L, 4L, 4L, 2L,
4L, 4L, 4L, 4L, 1L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 2L, 3L, 1L, 2L, 1L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 1L,
3L, 4L, 4L, 1L, 3L, 3L, 4L, 3L), .Label = c("UICC Stage I", "UICC Stage II",
"UICC Stage III", "UICC Stage IV"), class = "factor"), os.neck = c(11.5,
74.38, 17.02, 7.89, 96.03, 40.48, 17.74, 14.65, 62.46, 12.55,
9.92, 26.05, 45.47, 17.38, 39.72, 51.45, 8.61, 76.98, 67.09,
94.79, 72.15, 93.93, 17.05, 12.48, 91.6, 15.87, 11.04, 67.22,
67.02, 8.94, 6.6, 5.09, 10.68, 17.15, 0.07, 5.19, 40.77, 0.2,
170.88, 5.55, 1.61, 38.28, 10.58, 32.99, 110.98, 103.69, 122.32,
14.78, 42.74, 4.04, 8.28, 84.96, 11.7, 49.97, 120.48, 52.6, 71.26,
16.3, 100.14, 55.03, 6.51, 89.89, 51.71, 24.97, 55.66, 21.91,
81.48, 30.92, 1.58, 7.52, 30.75, 3.45, 19.22, 5.42, 17.68, 45.54,
76.22, 125.34, 83.62, 30.82, 90.32, 1.84, 19.98, 20.53, 32.59,
54.77, 2.3, 106.84, 22.28, 45.18, 4.47, 39.66, 32.3, 16.23, 3.88,
2.23, 0.23, 18.73, 0.79, 28.75, 79.54, 14.46, 15.15, 54.97, 48.59,
34.83, 58.42, 35.29, 45.73, 57.53, 63.11, 65.05, 29.54, 77.21,
63.48, 83.35, 34.3, 64.49, 29.54, 62.69, 21.62, 49.35, 99.02,
15.8, 41.89, 12.98, 13.8, 43.6, 57.23, 31.38, 70.74, 39.46, 20.76,
67.22, 127.15, 74.12, 1.97, 7.39, 25.17, 28.22, 14, 36.53, 20.83,
19.55, 40.77, 27.76, 45.31, 34.46, 35.55, 26.94, 9.43, 10.51,
6.8, 8.18, 8.02, 14.29, 6.11, 13.8, 4.9, 4.04, 14.82, 11.66,
73.07, 92.91, 99.98, 10.64, 10.05, 95.8, 7.23, 12.81, 43.99,
13.9, 10.25, 16.36, 18.2, 18.76, 12.32, 8.64, 11.79, 112.04,
70.97, 31.28, 28.85, 21.49, 19.94, 22.14, 29.44, 67.62, 11.01,
45.24, 110.72, 20.24, 14.06, 12.88, 31.51, 8.08, 13.08, 21.45,
24.28, 21.98, 32.89, 23.26, 15.41, 15.41, 13.8, 40.12, 8.02,
15.77, 49.81, 18.17, 24.21, 47.08, 6.6, 37.16, 13.01, 8.38, 14.36,
18.27, 17.28, 73.76, 68.21, 22.83, 2.66, 69.06, 17.05, 8.61,
23.33, 13.34, 12.65, 8.77, 128.92, 16.1, 4.99, 11.73, 22.97,
40.12, 20.37, 2.04, 45.73), mors = c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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, 6L, 15L, 9L, 8L, 7L, 4L, 6L, 10L, 8L, 4L, 9L, 10L,
14L, 14L, 3L, 4L, 6L, 6L, 20L, 3L, 26L, 13L, 13L, 13L, 13L, 3L,
7L, 6L, 5L, 10L, 15L, 29L, 7L, 6L, 11L, 17L, 14L, 18L, 22L, 9L,
20L, 34L, 9L, 8L, 8L, 11L, 3L, 4L, 4L, 5L, 3L, 2L, 8L, 5L, 18L,
7L, 9L, 13L, 18L, 19L, 14L, 46L, 23L, 11L, 6L, 18L, 20L, 4L,
2L, 7L, 7L, 4L, 11L, 13L, 13L, 9L, 9L, 9L, 12L, 11L, 16L, 6L,
13L, 8L, 17L, 5L, 8L, 22L, 19L, 3L, 15L, 14L, 7L, 18L, 9L, 10L,
18L, 24L, 11L, 15L, 7L, 6L, 4L, 24L, 23L, 8L, 20L, 9L, 22L, 11L,
2L, 24L, 15L, 5L, 8L, 11L, 11L, 11L, 15L, 6L, 16L, 7L, 9L, 16L,
11L, 33L, 27L, 16L, 57L, 5L, 7L, 8L, 11L, 15L, 15L, 12L, 5L,
9L, 49L, 11L, 28L, 19L, 13L, 23L, 11L, 12L, 10L, 4L, 14L, 6L,
12L, 32L, 13L, 12L, 4L, 11L, 17L, 10L, 5L, 15L, 21L, 19L, 11L,
31L, 9L, 20L, 11L, 16L, 12L, 6L, 16L, 27L, 30L, 18L, 18L, 10L,
7L, 23L, 16L, 15L, 4L, 12L, 9L, 10L, 11L, 7L, 8L, 8L, 7L, 6L,
9L, 9L, 13L, 15L, 12L, 35L, 12L, 5L, 19L, 27L, 34L, 10L, 16L,
18L, 6L, 22L)), row.names = c(3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 20L, 22L, 24L, 28L, 29L,
31L, 34L, 35L, 39L, 40L, 42L, 43L, 44L, 47L, 48L, 49L, 50L, 54L,
56L, 57L, 58L, 59L, 60L, 62L, 63L, 66L, 67L, 68L, 69L, 70L, 71L,
72L, 73L, 74L, 75L, 76L, 80L, 81L, 82L, 83L, 86L, 87L, 88L, 89L,
94L, 97L, 99L, 101L, 102L, 106L, 113L, 115L, 117L, 122L, 129L,
132L, 136L, 142L, 143L, 145L, 146L, 148L, 153L, 154L, 158L, 159L,
163L, 164L, 167L, 169L, 171L, 174L, 175L, 178L, 179L, 185L, 188L,
191L, 197L, 210L, 218L, 220L, 230L, 236L, 238L, 239L, 240L, 241L,
242L, 243L, 244L, 245L, 246L, 247L, 248L, 249L, 250L, 252L, 253L,
254L, 255L, 256L, 257L, 258L, 259L, 261L, 262L, 263L, 264L, 265L,
266L, 270L, 275L, 277L, 278L, 280L, 282L, 284L, 286L, 289L, 293L,
295L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 308L, 310L, 312L,
313L, 314L, 315L, 316L, 317L, 318L, 319L, 320L, 321L, 322L, 323L,
325L, 327L, 328L, 329L, 330L, 331L, 332L, 333L, 334L, 335L, 336L,
338L, 348L, 349L, 351L, 352L, 353L, 354L, 357L, 358L, 359L, 360L,
361L, 362L, 363L, 365L, 366L, 368L, 371L, 372L, 374L, 376L, 378L,
379L, 380L, 381L, 382L, 383L, 384L, 385L, 386L, 387L, 388L, 389L,
390L, 391L, 392L, 393L, 394L, 395L, 396L, 397L, 398L, 399L, 400L,
401L, 402L, 403L, 405L, 407L, 409L, 410L, 411L, 412L, 413L, 414L,
415L, 416L, 417L, 418L, 419L, 421L, 422L, 424L, 425L, 426L, 427L,
428L, 429L, 430L), class = "data.frame")
This is a modification of grob values for strip.text color using the grid package:
library(grid)
library(ggplot2)
g <- ggplot_gtable(ggplot_build(plot))
strip_both <- which(grepl('strip-', g$layout$name))
colors = c("#E38072","#6DBCC3", "#E1B930", "#2C77BF")
k <- 1
for (i in strip_both) {
j <- which(grepl("text", g$grobs[[i]]$grobs[[1]]$childrenOrder))
g$grobs[[i]]$grobs[[1]]$children[[j]]$children[[1]]$gp$col <- colors[k]
k <- k+1
}
grid.draw(g)
Assigning plot object to plot:
cols = c("#E1B930", "#2C77BF","#E38072","#6DBCC3")
plot <- ggplot(p, aes(x=n.fjernet,y=os.neck)) + geom_point(aes(color=uiccc),shape=20, size=5,alpha=0.7) +
geom_quantile(quantiles = 0.5,col="black", size=1,linetype=2) + facet_wrap(.~factor(uiccc)) +
scale_fill_manual(values=cols) +
scale_colour_manual(values=cols) +
scale_x_continuous(breaks = seq(0,50, by=10), name="Lymph nodal yield") +
scale_y_continuous(name="Time to death") +
theme(strip.text.x = element_text(size=12,face="bold"),
strip.text.y = element_text(size=12, face="bold"),
strip.background = element_rect(fill="white"),
legend.position="none")
Alternatively, plot them per group, then combine:
# named colours per group
cols <- setNames(c("#E1B930", "#2C77BF","#E38072","#6DBCC3"),
levels(p$uiccc))
# set pretty limits
Xlim <- round(range(p$n.fjernet), -1)
Ylim <- round(range(p$os.neck), -1)
ggList <- lapply(split(p, p$uiccc), function(i){
title <- i[1, "uiccc"]
ggplot(i, aes(x = n.fjernet, y = os.neck)) +
geom_point(aes(color = uiccc), shape = 20, size = 5, alpha = 0.7, show.legend = FALSE) +
geom_quantile(quantiles = 0.5, col = "black", size = 1, linetype = 2) +
ggtitle(title) +
scale_fill_manual(values = cols) +
scale_colour_manual(values = cols) +
scale_x_continuous(limits = Xlim) +
scale_y_continuous(limits = Ylim) +
theme_classic() +
theme(plot.title = element_text(colour = cols[ title ]))
})
Then use patchwork or cowplot to combine:
patchwork::wrap_plots(ggList)
#or
cowplot::plot_grid(plotlist = ggList)
Alternatively to #Greg's answer (that I think should be the validated answer), you can get the same plot by adding annotation in place of facet labeling:
library(tidyverse)
ggplot(df, aes(x=n.fjernet,y=os.neck)) + geom_point(aes(color=uiccc),shape=20, size=5,alpha=0.7) +
geom_quantile(quantiles = 0.5,col="black", size=1,linetype=2) + facet_wrap(.~factor(uiccc)) +
scale_fill_manual(values=cols) +
scale_colour_manual(values=cols) +
scale_x_continuous(breaks = seq(0,50, by=10), name="Lymph nodal yield") +
scale_y_continuous(name="Time to death") +
theme(#strip.text.x = element_text(size=12,face="bold", colour = cols),
#strip.text.y = element_text(size=12, face="bold"),
#strip.background = element_rect(fill="white"),
strip.background = element_blank(),
strip.text = element_text(color = "transparent"),
legend.position="none",
plot.margin = unit(c(1,3,1,1), "lines")) +
coord_cartesian(clip = "off",ylim = c(0,175))+
geom_text(data = . %>% distinct(uiccc),
aes(label = factor(uiccc), color = uiccc), y = 200, x = 30, hjust = 0.5, fontface = "bold")

Different results in Stata and R with the "same" anova code

I have some Stata code and I want to replicate the results in R. However, even with the same dataset and, I think, the same code, I get different results in R from those in Stata. I think it could be because Stata makes the order of the regression different than keyed in.
Do I need exactly the same order as in Stata to get the same results and how can I do this?
I changed all the variables to factors and tried again but the problem is still there.
I noticed that when I change the order of the explanatory variables I get different results, but I don`t find "the right order" to replicate the Stata results.
Stata code:
. anova testm2 c.testm1 i.hptreat c.cortm1 c.cortm2 i.female if inelig == 0 & anyoutv1 == 0
Number of obs =39 R-squared =0.7048
Root MSE= 16.0144 Adj R-squared =0.6601
Source | Partial SS df MS F Prob>F
---------------------------------------------------------------
Model | 20209.281 5 4041.8563 15.76 0.0000
testm1 | 3516.6527 1 3516.6527 13.71 0.0008
hptreat| 1183.5007 1 1183.5007 4.61 0.0391
cortm1 | 8.5753841 1 8.5753841 0.03 0.8560
cortm2 | 2810.9353 1 2810.9353 10.96 0.0023
female | 2557.3444 1 2557.3444 9.97 0.0034
Residual| 8463.2532 33 256.46222
----------------------------------------------------------------
Total | 28672.535 38 754.54038
R code:
FosseTest<-aov(testm2~testm1+hptreat+cortm1+cortm2+female,data=X2data)
summary(FosseTest)
Df Sum Sq Mean Sq F value Pr(>F)
testm1 1 15121 15121 58.962 7.68e-09 ***
hptreat 1 524 524 2.043 0.16228
cortm1 1 23 23 0.089 0.76715
cortm2 1 1984 1984 7.735 0.00888 **
female 1 2557 2557 9.972 0.00339 **
Residuals 33 8463 256
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
You can see that I get totally different values in the replication.
in the X2data Set I already subset the values for if inelig == 0 & anyoutv1 == 0
for the reconstruction of the data:
dput(X2data)
structure(list(id = c(29L, 30L, 31L, 32L, 34L, 35L, 36L, 37L,
39L, 41L, 42L, 43L, 44L, 46L, 47L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 57L, 58L, 59L, 60L, 61L, 62L, 64L, 65L, 66L, 67L, 68L, 69L,
70L, 71L, 72L, 73L, 74L), inelig = 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), .Label = c("Analytic sample (keep)", "Ineligible (drop)"
), class = "factor"), ccydrop = c(0L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), cortm1v2 = c(0.003, 0.086, 0.047, 0.106, NA, 0.153, 0.086,
0.005, 0.133, 0.036, 0.03, 0.015, 0.014, 0.111, 0.389, 0.298,
0.4, 0.215, 0.062, 0.021, 0.075, 0.073, 0.033, 0.243, 0.126,
0.147, 0.019, 0.048, 0.28, 0.052, 0.039, 0.105, 0.111, 0.133,
0.065, 0.051, 0.143, 0.127, 0.095), cortm2v2 = c(0.025, 0.167,
0.059, 0.112, 0.171, 0.183, 0.102, 0.018, 0.08, 0.015, 0.027,
0.05, 0.025, 0.046, 0.085, 0.144, 0.155, 0.09, 0.057, 0.023,
0.038, 0.205, 0.035, 0.198, 0.112, 0.211, 0.042, 0.142, 0.328,
0.076, 0.067, 0.094, 0.245, 0.153, 0.115, 0.127, 0.257, 0.125,
0.096), cdiffv2 = c(0.022, 0.081, 0.012, 0.006, NA, 0.03, 0.016,
0.013, -0.053, -0.021, -0.003, 0.035, 0.011, -0.065, -0.304,
-0.154, -0.245, -0.125, -0.005, 0.002, -0.037, 0.132, 0.002,
-0.045, -0.014, 0.064, 0.023, 0.094, 0.048, 0.024, 0.028, -0.011,
0.134, 0.02, 0.05, 0.076, 0.114, -0.002, 0.001), testm1v2 = c(38.72,
32.77, 32.32, 17.99, 73.58, 80.69, 48.56, 21.92, 27.24, 40.93,
31.73, 60.05, 38.04, 30.17, 59.07, 26.92, 25.41, 47.81, 63.02,
34.49, 104.38, 38.08, 30.99, 35.23, 104.81, 49.33, 50.03, 11.65,
143.57, 48.31, 90.37, 48.56, 41.67, 75.23, 60.56, 39.03, 18.16,
37.9, 84.5), testm2v2 = c(62.37, 29.23, 27.51, 28.66, 44.67,
105.48, 42.67, 15.01, 21.33, 10.87, 2.14, 44.53, 35.8, 10.43,
47.54, 48.5, 38.98, 91.32, 52.94, 22.43, 58.68, 81.63, 34.79,
38.57, 94.86, 50.83, 55.75, 45.33, 111.62, 65.15, 81.08, 50.08,
44.86, 58.63, 85.85, 58.69, 16.35, 35.97, 99.08), tdiffv2 = c(23.65,
-3.54, -4.81, 10.67, -28.91, 24.79, -5.89, -6.91, -5.91, -30.06,
-29.59, -15.52, -2.24, -19.74, -11.53, 21.58, 13.57, 43.51, -10.08,
-12.06, -45.7, 43.55, 3.8, 3.34, -9.95, 1.5, 5.72, 33.68, -31.95,
16.84, -9.29000000000001, 1.52, 3.19, -16.6, 25.29, 19.66, -1.81,
-1.93, 14.58), testoutv1 = 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), .Label = c("Not selected", "Selected"), class = "factor"),
cortoutv1 = 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
), .Label = c("Not selected", "Selected"), class = "factor"),
anyoutv1 = 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
), .Label = c("Not selected", "Selected"), class = "factor"),
testoutv2 = 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
), .Label = c("Not selected", "Selected"), class = "factor"),
cortoutv2 = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Not selected", "Selected"), class = "factor"),
anyoutv2 = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Not selected", "Selected"), class = "factor"),
pose1rate = c(6L, 7L, 6L, 6L, 7L, 7L, 6L, 7L, 5L, 6L, 7L,
4L, 7L, 7L, 7L, 6L, 7L, 7L, 7L, 7L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), pose2rate = c(6L,
6L, 5L, 7L, 7L, 7L, 7L, 7L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 6L,
6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 7L, 6L, 7L, 7L, 7L,
6L, 7L, 7L, 7L, 7L, 7L, 6L, 6L), poseratem = c(6, 6.5, 5.5,
6.5, 7, 7, 6.5, 7, 5.5, 6.5, 7, 5.5, 7, 7, 7, 6, 6.5, 7,
7, 7, 6.5, 7, 7, 7, 7, 6.5, 7, 6.5, 7, 7, 7, 6.5, 7, 7, 7,
7, 7, 6.5, 6.5), saldiff = c(24.30555556, 20.83333333, 29.16666667,
18.75, 23.61111111, 34.02777778, 18.05555556, 19.44444444,
21.52777778, 15.97222222, 22.91666667, 13.88888889, 22.22222222,
25, 22.22222222, 22.22222222, 18.05555556, 17.36111111, 22.22222222,
27.08333333, 20.83333333, 24.30555556, 22.22222222, 28.47222222,
24.30555556, 25, 27.77777778, 22.22222222, 15.97222222, 24.30555556,
21.52777778, 19.44444444, 15.97222222, 15.27777778, 15.97222222,
24.30555556, 19.44444444, 24.30555556, 15.27777778), sal2manip = c(19.80555556,
16.33333333, 24.66666667, 14.25, 19.11111111, 29.52777778,
13.55555556, 14.94444444, 17.02777778, 11.47222222, 18.41666667,
9.38888889, 17.72222222, 20.5, 17.72222222, 17.72222222,
13.55555556, 12.86111111, 17.72222222, 22.58333333, 16.33333333,
19.80555556, 17.72222222, 23.97222222, 19.80555556, 20.5,
23.27777778, 17.72222222, 11.47222222, 19.80555556, 17.02777778,
14.94444444, 11.47222222, 10.77777778, 11.47222222, 19.80555556,
14.94444444, 19.80555556, 10.77777778), hptreat = structure(c(2L,
1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("0", "1"), class = "factor"),
female = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L
), .Label = c("0", "1"), class = "factor"), age = c(19L,
20L, 20L, 18L, 21L, 20L, 18L, 21L, 35L, 20L, 18L, 20L, 20L,
18L, 20L, 25L, 18L, 23L, 21L, 19L, 20L, 20L, 30L, 19L, 22L,
18L, 19L, 22L, 19L, 20L, 28L, 28L, 19L, 19L, 20L, 25L, 20L,
25L, 23L), cort1a1 = c(0.004, 0.085, 0.049, 0.107, 0.486,
0.159, 0.088, 0.004, 0.138, 0.035, 0.03, 0.018, 0.017, 0.111,
0.39, 0.292, 0.396, 0.213, 0.065, 0.022, 0.074, 0.077, 0.035,
0.241, 0.126, 0.154, 0.021, 0.05, 0.296, 0.054, 0.04, 0.109,
0.114, 0.133, 0.063, 0.055, 0.149, 0.134, 0.098), cort1a2 = c(0.001,
0.086, 0.045, 0.105, 0.482, 0.147, 0.085, 0.005, 0.127, 0.037,
0.031, 0.013, 0.011, 0.111, 0.389, 0.304, 0.405, 0.218, 0.059,
0.02, 0.076, 0.069, 0.032, 0.246, 0.126, 0.141, 0.017, 0.046,
0.264, 0.051, 0.038, 0.101, 0.109, 0.133, 0.068, 0.048, 0.137,
0.12, 0.092), cort2a1 = c(0.027, 0.174, 0.056, 0.111, 0.175,
0.179, 0.103, 0.021, 0.079, 0.014, 0.028, 0.051, 0.024, 0.051,
0.083, 0.148, 0.156, 0.086, 0.062, 0.024, 0.038, 0.209, 0.036,
0.199, 0.114, 0.207, 0.041, 0.141, 0.333, 0.078, 0.065, 0.088,
0.238, 0.157, 0.119, 0.132, 0.268, 0.132, 0.099), cort2a2 = c(0.023,
0.161, 0.062, 0.113, 0.166, 0.188, 0.101, 0.016, 0.081, 0.015,
0.026, 0.049, 0.026, 0.041, 0.086, 0.139, 0.154, 0.093, 0.052,
0.022, 0.038, 0.202, 0.034, 0.198, 0.111, 0.215, 0.042, 0.142,
0.324, 0.075, 0.068, 0.101, 0.252, 0.149, 0.111, 0.123, 0.247,
0.118, 0.093), cortm1 = c(0.0024999999, 0.085500002, 0.046999998,
0.106, 0.484, 0.153, 0.086499996, 0.0044999998, 0.13249999,
0.035999998, 0.0305, 0.0155, 0.014, 0.111, 0.38949999, 0.29800001,
0.4005, 0.2155, 0.061999999, 0.021, 0.075000003, 0.072999999,
0.033500001, 0.24349999, 0.126, 0.14749999, 0.018999999,
0.048, 0.28, 0.052499998, 0.039000001, 0.105, 0.1115, 0.133,
0.065499999, 0.0515, 0.14300001, 0.127, 0.094999999), cortm2 = c(0.025,
0.1675, 0.059, 0.112, 0.1705, 0.18350001, 0.102, 0.0185,
0.079999998, 0.0145, 0.027000001, 0.050000001, 0.025, 0.046,
0.0845, 0.1435, 0.155, 0.089500003, 0.057, 0.023, 0.037999999,
0.20550001, 0.035, 0.19850001, 0.1125, 0.211, 0.041499998,
0.1415, 0.3285, 0.076499999, 0.066500001, 0.094499998, 0.245,
0.153, 0.115, 0.1275, 0.25749999, 0.125, 0.096000001), cdiff = c(0.022500001,
0.082000002, 0.012000002, 0.0060000047, -0.31349999, 0.03050001,
0.015500002, 0.014, -0.052499995, -0.021499999, -0.0034999996,
0.034500003, 0.011, -0.064999998, -0.30500001, -0.15450001,
-0.2455, -0.12599999, -0.004999999, 0.0020000003, -0.037000004,
0.13250001, 0.0014999993, -0.044999987, -0.013500005, 0.063500002,
0.022499999, 0.093499996, 0.048500001, 0.024, 0.0275, -0.010499999,
0.13350001, 0.019999996, 0.049500003, 0.075999998, 0.11449999,
-0.0020000041, 0.001000002), test1a1 = c(39.87, 33.22, 32.52,
19.74, 78.85, 83.51, 48.37, 22.31, 28.17, 41.44, 32.92, 61.4,
40.31, 30.36, 59.44, 27.52, 26.14, 46.75, 63.73, 34.03, 98.47,
36.62, 30.26, 37.15, 105.64, 47.99, 50.15, 11.33, 149.12,
48.57, 92.04, 51.22, 42.25, 77.07, 62.75, 38.8, 17.91, 40.28,
88.47), test1a2 = c(37.58, 32.32, 32.12, 16.25, 68.31, 77.88,
48.75, 21.53, 26.32, 40.42, 30.55, 58.7, 35.78, 29.97, 58.7,
26.32, 24.69, 48.87, 62.32, 34.95, 110.29, 39.53, 31.72,
33.32, 103.99, 50.67, 49.9, 11.97, 138.02, 48.05, 88.7, 45.89,
41.08, 73.39, 58.38, 39.25, 18.41, 35.53, 80.54), test2a1 = c(64.22,
29.43, 27.98, 28.17, 46.14, 105.92, 43.68, 16.41, 21.42,
11.35, 1.66, 44.17, 38.58, 11.11, 48.57, 48.31, 39.71, 92.04,
52.73, 22.3, 58.23, 82.01, 35.76, 39.59, 94.06, 50.52, 55.82,
45.91, 115.13, 67.59, 82.97, 49.89, 45.09, 57.86, 86.76,
58.83, 16.53, 36.7, 100.4), test2a2 = c(60.53, 29.04, 27.04,
29.14, 43.2, 105.05, 41.66, 13.62, 21.25, 10.39, 2.63, 44.9,
33.02, 9.75, 46.52, 48.7, 38.25, 90.59, 53.15, 22.57, 59.14,
81.24, 33.81, 37.55, 95.66, 51.14, 55.69, 44.74, 108.1, 62.71,
79.18, 50.27, 44.63, 59.39, 84.94, 58.55, 16.16, 35.24, 97.75
), testm1 = c(38.724998, 32.77, 32.32, 17.995001, 73.580002,
80.695, 48.560001, 21.92, 27.245001, 40.93, 31.735001, 60.049999,
38.044998, 30.165001, 59.07, 26.92, 25.415001, 47.810001,
63.025002, 34.490002, 104.38, 38.075001, 30.99, 35.235001,
104.815, 49.330002, 50.025002, 11.65, 143.57001, 48.310001,
90.370003, 48.555, 41.665001, 75.230003, 60.564999, 39.025002,
18.16, 37.904999, 84.504997), testm2 = c(62.375, 29.235001,
27.51, 28.655001, 44.669998, 105.485, 42.669998, 15.015,
21.334999, 10.87, 2.145, 44.535, 35.799999, 10.43, 47.544998,
48.505001, 38.98, 91.315002, 52.939999, 22.434999, 58.685001,
81.625, 34.785, 38.57, 94.860001, 50.830002, 55.755001, 45.325001,
111.615, 65.150002, 81.074997, 50.080002, 44.860001, 58.625,
85.849998, 58.689999, 16.344999, 35.970001, 99.074997), tdiff = c(23.650002,
-3.5349998, -4.8099995, 10.66, -28.910004, 24.790001, -5.8900032,
-6.9049997, -5.9100018, -30.060001, -29.59, -15.514999, -2.2449989,
-19.735001, -11.525002, 21.585001, 13.564999, 43.505001,
-10.085003, -12.055002, -45.694996, 43.549999, 3.7950001,
3.3349991, -9.9550018, 1.5, 5.7299995, 33.675003, -31.955009,
16.84, -9.2950058, 1.5250015, 3.1949997, -16.605003, 25.285,
19.664997, -1.8150005, -1.9349976, 14.57), feelpower = structure(c(2L,
3L, 1L, 2L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 1L, 2L, 1L,
1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 4L, 3L,
4L, 3L, 1L, 3L, 4L, 2L, 2L, 3L), .Label = c("2", "3", "Not at all",
"Very much"), class = "factor"), incharge = structure(c(1L,
1L, 3L, 4L, 1L, 2L, 3L, 3L, 1L, 1L, 3L, 4L, 3L, 2L, 2L, 1L,
3L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L,
1L, 3L, 1L, 1L, 4L, 3L, 1L, 1L), .Label = c("2", "3", "Not at all",
"Very much"), class = "factor"), powm = structure(c(3L, 1L,
1L, 5L, 2L, 4L, 6L, 6L, 1L, 1L, 6L, 7L, 6L, 3L, 4L, 2L, 1L,
4L, 4L, 3L, 2L, 4L, 2L, 2L, 3L, 3L, 3L, 4L, 1L, 5L, 1L, 4L,
6L, 2L, 1L, 7L, 2L, 3L, 1L), .Label = c("1.5", "2", "2.5",
"3", "3.5", "Not at all", "Very much"), class = "factor"),
diceroll = structure(c(2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L
), .Label = c("No", "Yes"), class = "factor")), row.names = c(2L,
3L, 4L, 5L, 7L, 8L, 9L, 10L, 12L, 14L, 15L, 16L, 17L, 19L, 20L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 30L, 31L, 32L, 33L, 34L, 35L,
37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L), class = "data.frame")
You can get the same results in R using drop1(FosseTest, test = "F"). This will test the effect of leaving one of the variables off the aov.
drop1(FosseTest, test = "F")
#
# Single term deletions
#
# Model:
# testm2 ~ testm1 + hptreat + cortm1 + cortm2 + female
# Df Sum of Sq RSS AIC F value Pr(>F)
# <none> 8463.3 221.82
# testm1 1 3516.7 11979.9 233.37 13.7122 0.0007751 ***
# hptreat 1 1183.5 9646.8 224.92 4.6147 0.0391333 *
# cortm1 1 8.6 8471.8 219.86 0.0334 0.8560279
# cortm2 1 2810.9 11274.2 231.00 10.9604 0.0022605 **
# female 1 2557.3 11020.6 230.11 9.9716 0.0033895 **
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
summary(FosseTest) displays the sequential effect of addeding the variables one after another.
There was a different way how to access this, but at the moment I can't remember...

How to differentiate Bars in geom_bar without color: ggplot

Note: A similar question is present at link, but I posed it a separate question due to: 1) only a hack is provided to the previos question which I thought would make code unnecessary complex 2) I thought after 2013 a fix might have been suggested for this
I am using following code to draw bars/stacks
ggplot(finaldataframe,aes(day,score))+
geom_bar(aes(fill=identify),stat="identity",position = "dodge",width = .7, show.legend = TRUE)+
labs(x= "Day of the Month", y="Anomaly Score") +
scale_fill_discrete(name="Method", labels=c("Mean","Maximum","Cumulative \n sum"))+
theme(axis.text= element_text(color="Black"))+ scale_x_continuous(breaks=seq(1,31,5))
A portion of output is as
The problem with this figure is that once I print this via black and white printer It gets hard to differentiate between different stacks. Is there any way to make the stacks differentiable on a black and white print. I am looking for some what like this:
For reproduction, Here is the dput of dataframe:
> dput(finaldataframe)
finaldataframe = structure(list(day = 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, 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, 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), score = c(0, 0.02, 0.01, 0, 0.02, 0.01, 0.01,
0.02, 0.02, 0.28, 0.24, 0.01, 0.94, 0.22, 0.25, 0.01, 0.31, 0.22,
0.24, 0.83, 0.4, 0.44, 0.06, 0.02, 0.37, 0.07, 0.12, 0.06, 0.1,
0.06, 0.1, 0, 0.05, 0.04, 0.02, 0.05, 0.01, 0.02, 0.03, 0.04,
0.37, 0.36, 0.04, 1, 0.28, 0.34, 0.03, 0.55, 0.35, 0.32, 1, 0.71,
1, 0.13, 0.04, 0.47, 0.12, 0.17, 0.1, 0.18, 0.1, 0.14, 0, 0.02,
0.01, 0, 0.02, 0.01, 0.01, 0.02, 0.02, 0.3, 0.25, 0.01, 1, 0.23,
0.27, 0, 0.33, 0.24, 0.26, 0.89, 0.42, 0.47, 0.06, 0.02, 0.4,
0.07, 0.13, 0.06, 0.11, 0.06, 0.1), identify = 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, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 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), .Label = c("Mean",
"Maximum", "Cummulative Sum"), class = "factor")), .Names = c("day",
"score", "identify"), row.names = c(NA, 93L), class = "data.frame")

Changing the order of plotting levels in Latitice

I am trying to get a boxplot with a specific order of the levels that are being plotted.
Using the following data and code I generate the boxplot, but the order in which I need this is 6,12,15,18.
I have tried a number of thing using the with() function but can't make it work.
library(lattice)
rate<-structure(list(Temp = c(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, 6L, 6L, 6L, 6L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 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, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L,
6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L,
6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L), Weight = c(1.756,
1.756, 1.756, 1.756, 0.92, 0.92, 0.92, 0.92, 1.201, 1.201, 1.201,
1.201, 2.601, 2.601, 2.601, 2.601, 2.057, 2.057, 2.057, 2.057,
0.784, 0.784, 0.784, 0.784, 0.663, 0.663, 0.663, 0.663, 1.272,
1.272, 1.272, 1.272, 3.389, 3.389, 3.389, 3.389, 1.433, 1.433,
1.433, 1.433, 3.822, 3.822, 3.822, 3.822, 1.55, 1.55, 1.55, 1.55,
1.198, 1.198, 1.198, 1.198, 1.029, 1.029, 1.029, 1.029, 1.113,
1.113, 1.113, 1.113, 0.261, 0.261, 0.261, 0.261, 0.639, 0.639,
0.639, 0.639, 0.749, 0.749, 0.749, 0.749, 1.083, 1.083, 1.083,
1.083, 1.429, 1.429, 1.429, 1.429, 3.083, 3.083, 3.083, 3.083,
1.061, 1.061, 1.061, 1.061, 1.154, 1.154, 1.154, 1.154, 1.691,
1.691, 1.691, 1.691, 1.185, 1.185, 1.185, 1.185, 0.552, 0.552,
0.552, 0.552, 1.507, 1.507, 1.507, 1.507, 1.175, 1.175, 1.175,
1.175, 1.773, 1.773, 1.773, 1.773, 1.712, 1.712, 1.712, 1.712,
3.784, 3.784, 3.784, 3.784, 0.715, 0.715, 0.715, 0.715, 1.271,
1.271, 1.271, 1.271, 0.788, 0.788, 0.788, 0.788, 1.72, 1.72,
1.72, 1.72, 0.571, 0.571, 0.571, 0.571, 1, 1, 1, 1, 1.037, 1.037,
1.037, 1.037, 1.656, 1.656, 1.656, 1.656, 2.083, 2.083, 2.083,
2.083), Rate = c(0.387, 0.116, -0.141, 0.184, 0.785, 0.151, -0.69,
0.16, 0.477, 0.368, -0.544, 0.49, 0.152, 0.183, -0.137, 0.259,
0.239, 0.292, 0.018, 0.411, 0.322, 0.073, -0.148, 0.287, 0.214,
0.21, -0.579, 0.419, 0.23, 0.271, 0.685, 0.426, 0.248, 0.125,
0.053, 0.176, 0.465, 0.107, 0.02, 0.339, 0.261, 0.327, 0.279,
0.424, 0.308, 0.223, 0.287, 0.383, 0.306, 0.24, 0.258, 0.253,
0.437, 0.315, 0.275, 0.481, 0.372, 0.306, 0.267, 0.449, 0.727,
0.441, 0.624, 1.262, 0.334, 0.447, 0.548, 0.654, 0.846, 0.661,
0.66, 0.734, 0.191, 0.316, 0.551, 0.581, 0.332, 0.403, 0.509,
0.603, 0.411, 0.683, 0.427, 0.516, 0.498, 0.674, 0.371, 0.326,
0.288, 0.435, 0.297, 0.435, 0.165, 0.387, 0.212, 0.345, 0.334,
0.664, 0.526, 0.338, 0.094, 0.066, 0.39, 0.525, 0.215, 0.431,
0.151, 0.361, 0.153, 0.297, 0.127, 0.339, 0.292, 0.434, 0.411,
0.442, 0.25, 0.607, 0.369, 0.567, 0.189, 0.39, 0.372, 0.333,
0.339, 0.327, 0.449, 0.224, 0.086, 0.242, 0.465, 0.374, -0.063,
-0.006, 0.364, 0.308, 0.069, 0.223, 0.397, 0.264, 0.478, 0.345,
0.582, 0.36, 0.426, 0.403, 0.583, 0.544, 0.57, 0.567, 0.388,
0.531, 0.111, 0.125, 0.366, 0.266, 0.26, 0.315, 0.387, 0.549)), .Names = c("Temp",
"Rep", "Ind", "Week", "Weight", "Rate"), class = "data.frame", row.names = c(NA,
-160L))
rate$Temp <- as.character(rate$Temp)
rate$Week <- as.character(rate$Week)
rate$Rep <- as.character(rate$Rep)
rate$Weight<- as.character(rate$Weight)
bwplot(Rate~Temp, rate,
main="Boxplot for data over all weeks by temperature"
)
This can be tackled in the same manner as your question from a month ago. You need to set the order of levels of a factor. I would generally advise you work with factors, unless you have a really good reason to use characters.
rate$Temp <- as.factor(rate$Temp)
levels(rate$Temp) <- c("6", "12", "15", "18")

Resources