I'm trying to make a for loop to automatically generate ~50 scatterplots comparing two sets of data. It's a quality control analysis, so I'm looking at geochemical values that were analyzed twice (duplicates). So I have a list of 53 elements (periodic table elements) labeled Al1, Ag1, Au1..... and another list of 53 labeled Al2, Ag2....etc.
I've successfully gotten my loop to work for generating graphs that only need one variable, with the x axis being fixed, like below.
for(i in colNames){
plt <- ggplot(YGS_Dupes, mapping = aes_string(x=Dup_Num, y = i)) +
geom_bar() + theme_calc() + ggtitle(paste(i, "Duplicate Comparison", sep=" -
"))
print(plt)
ggsave(paste0(i,".png"))
Sys.sleep(2)
}
I set colNames to be the element columns, and the function runs through the different elements and generates a bar plot for each, where it's just showing Sample 1 or Sample 2 as the X axis (so it produces two bar plots side by side).
What I need to make now is a scatterplot where I compare the data from Al1 to Al2 or Fe1 to Fe2, so I need the for loop to run using two parallel sets of changing variables. I made the function for a single graph like so:
ggplot(YGS_Dup_Scatter, mapping = aes(x = Fe_pct1, y =
Fe_pct2))+geom_point()
and it looks like this:
Fe vs Fe Scatterplot
So what I have done is made a similar set of colNames groups, like below:
colNames_scatter_dup <- names(YGS_Dup_Scatter)[4:56]
colNames_scatter_dup2 <- names(YGS_Dup_Scatter)[57:109]
Where 4-56 are all the element 1 set and 57-109 are the element 2 set. They are ordered the same so I want 4/57, 5/58....etc to be pairs.
How do I set up my for loop equation to do this?
Thank you for any help
Edit: Adding the dput data for people to try. I had too many observations and variables so I cut most of them out:
Edit 2: Ok, so I made a nested loop and it makes what I want, but it also makes way too many graphs, shown below:
for (j in colNames_scatter_dup2) {
for(i in colNames_scatter_dup){
plt <- ggplot(YGS_Dup_Scatter, mapping = aes_string(x=j, y = i)) +
geom_point()
print(plt)
ggsave(paste0(i,".png"))
Sys.sleep(2)
}
}
The issue I have now is that it does Al1 vs Al2, then Ag1 vs Al2, ......then gets to Al1 vs Ag2.....and make hundreds of graphs. I only want to make the actual 53 element pairs, and I can't figure out how to restrict it to just those.
thanks
structure(list(DUP_COMP_ID = structure(c(1L, 12L, 23L, 34L, 45L,
56L, 67L, 78L, 89L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 68L, 69L,
70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 79L, 80L, 81L, 82L, 83L,
84L, 85L, 86L, 87L, 88L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L,
98L, 99L), .Label = c("DCI_1", "DCI_10", "DCI_11", "DCI_12",
"DCI_13", "DCI_14", "DCI_15", "DCI_16", "DCI_17", "DCI_18", "DCI_19",
"DCI_2", "DCI_20", "DCI_21", "DCI_22", "DCI_23", "DCI_24", "DCI_25",
"DCI_26", "DCI_27", "DCI_28", "DCI_29", "DCI_3", "DCI_30", "DCI_31",
"DCI_32", "DCI_33", "DCI_34", "DCI_35", "DCI_36", "DCI_37", "DCI_38",
"DCI_39", "DCI_4", "DCI_40", "DCI_41", "DCI_42", "DCI_43", "DCI_44",
"DCI_45", "DCI_46", "DCI_47", "DCI_48", "DCI_49", "DCI_5", "DCI_50",
"DCI_51", "DCI_52", "DCI_53", "DCI_54", "DCI_55", "DCI_56", "DCI_57",
"DCI_58", "DCI_59", "DCI_6", "DCI_60", "DCI_61", "DCI_62", "DCI_63",
"DCI_64", "DCI_65", "DCI_66", "DCI_67", "DCI_68", "DCI_69", "DCI_7",
"DCI_70", "DCI_71", "DCI_72", "DCI_73", "DCI_74", "DCI_75", "DCI_76",
"DCI_77", "DCI_78", "DCI_79", "DCI_8", "DCI_80", "DCI_81", "DCI_82",
"DCI_83", "DCI_84", "DCI_85", "DCI_86", "DCI_87", "DCI_88", "DCI_89",
"DCI_9", "DCI_90", "DCI_91", "DCI_92", "DCI_93", "DCI_94", "DCI_95",
"DCI_96", "DCI_97", "DCI_98", "DCI_99"), class = "factor"), Dup_Code = 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), .Label = "Sample 1", class = "factor"), Dup_Code.1 = 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), .Label = "Sample 2", class = "factor"), Ag_ppb1 = c(56L,
58L, 52L, 59L, 68L, 318L, 50L, 70L, 398L, 114L, 38L, 52L, 63L,
64L, 65L, 81L, 66L, 62L, 86L, 146L, 67L, 70L, 49L, 69L, 74L,
55L, 55L, 47L, 109L, 41L, 78L, 115L, 65L, 373L, 59L, 47L, 85L,
72L, 86L, 72L, 77L, 554L, 68L, 85L, 105L, 70L, 67L, 127L, 69L,
67L, 38L, 59L, 284L, 94L, 57L, NA, 92L, 88L, 74L, 73L, 50L, NA,
63L, 57L, 111L, 71L, 47L, 69L, 81L, 45L, 52L, 42L, 34L, 176L,
73L, 140L, 87L, 41L, 36L, 204L, 272L, 52L, 37L, 45L, 187L, 180L,
100L, 60L, 39L, 71L, 92L, 29L, 308L, 157L, 78L, 91L, NA, 60L,
217L), As_ppm1 = c(4.3, 4.8, 4.6, 5, 1.9, 14.3, 3, 5.8, 49.7,
9.2, 3.8, 3.1, 5.9, 5.4, 5, 4.3, 5.3, 4.2, 3.8, 35, 5.8, 6.6,
3.3, 11.2, 3.5, 3.8, 3.8, 4.4, 8.8, 4.9, 3.6, 18.3, 3.6, 6.1,
4.2, 4.4, 9, 7.3, 3.7, 3.4, 13.7, 21.9, 3.9, 5.8, 3.6, 4.4, 2.9,
5.2, 4.9, 5.4, 4.4, 4.3, 5.5, 8.3, 3.4, NA, 6.2, 4.2, 3.5, 5.5,
5, NA, 3.4, 4.2, 7.1, 5.1, 3.8, 6.9, 6.7, 3.2, 4.8, 4.3, 2.6,
4.6, 4.8, 9.3, 7.5, 2.8, 4.2, 4.9, 17, 3.1, 3.9, 4.7, 9.7, 883.2,
7.8, 5.1, 2.4, 10.4, 7.2, 2.9, 6.7, 9.3, 3.7, 7.3, NA, 4.8, 21.5
), Au_ppb1 = c(0.7, 4.6, 1.5, 0.6, 11.9, 2.4, 0.8, 0.8, 2.2,
3.5, 0.4, 0.8, 0.9, 1.7, 1.2, 3.5, 1.4, 1.4, 2.2, 2.6, 3, 0.9,
0.6, 1.5, 0.9, 0.7, 1.4, 3.5, 8.7, 0.4, 0.6, 2.4, 1.1, 1.7, 1.5,
1.3, 0.1, 0.1, 4.5, 44.5, 0.8, 6.6, 48.7, 1.5, 0.7, 0.3, 0.8,
1.1, 1.2, 5.5, 1.4, 1.4, 2.7, 1.9, 1, NA, 0.4, 1, 1.6, 0.3, 0.4,
NA, 0.8, 1.8, 1.9, 0.1, 0.5, 1.4, 0.8, 0.2, 0.8, 0.6, 0.3, 1.1,
1, 2.1, 0.8, 0.4, 0.9, 0.9, 1.2, 1.2, 1.2, 1.3, 1.2, 1.6, 1.8,
0.5, 1.4, 1.3, 1.4, 0.1, 0.6, 1.9, 0.8, 1.5, NA, 0.6, 3.4), B_ppm1 = c(10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 21L, NA, 10L, 10L, 10L, 10L, 10L, NA, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, NA, 10L, 10L), Ba_ppm1 = c(141, 124.2, 171.9,
171, 246.8, 359.3, 96, 205.4, 187.4, 195.3, 115.2, 134.9, 162.9,
156.9, 186.7, 148.4, 164.9, 165.5, 329.1, 106.8, 137.3, 150.7,
180.9, 123.4, 150.6, 122.7, 230.4, 176.1, 208.9, 154.5, 147.2,
242.2, 184.2, 465.5, 217.2, 171.3, 286.6, 248, 243.1, 265.9,
273.3, 317.4, 150.7, 272.7, 332.1, 293.1, 185.7, 262.9, 203.4,
333, 185.2, 203.4, 300.8, 227.3, 193.2, NA, 328, 293.2, 225.7,
286.9, 237.6, NA, 193.5, 293.8, 294.5, 252.2, 160.5, 277, 349.2,
184.5, 231.3, 251.4, 150, 372.4, 237.7, 227.9, 271.8, 66.6, 92.8,
53.4, 112.5, 172.6, 188.5, 177, 315.5, 193.8, 300.2, 132.9, 199.4,
221.4, 375.6, 128.7, 82.7, 157.4, 175.5, 297.9, NA, 190.9, 206.4
), Be_ppm1 = c(0.3, 0.5, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.6, 0.3,
0.4, 0.4, 0.3, 0.3, 0.4, 0.5, 0.4, 0.3, 0.2, 0.3, 0.9, 0.4, 0.6,
0.3, 0.5, 0.3, 0.3, 0.2, 0.3, 0.3, 0.4, 0.6, 0.3, 0.2, 0.3, 0.3,
0.3, 0.2, 0.6, 0.4, 0.4, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.4,
0.2, 0.3, 0.3, 0.2, 0.3, 0.3, NA, 0.3, 0.05, 0.3, 0.3, 0.2, NA,
0.3, 0.5, 0.3, 0.5, 0.3, 0.3, 0.3, 0.3, 0.2, 0.3, 0.2, 0.4, 0.3,
0.5, 0.4, 0.2, 0.1, 1.8, 1.8, 0.4, 0.2, 0.2, 0.8, 35.9, 0.3,
0.4, 0.2, 0.4, 0.2, 0.4, 0.2, 0.4, 0.3, 0.4, NA, 0.4, 1.2), Bi_ppm1 = c(0.24,
0.29, 0.21, 0.19, 0.13, 0.28, 0.15, 0.16, 0.73, 0.14, 0.12, 0.39,
0.1, 0.12, 0.4, 0.42, 0.13, 0.13, 0.11, 6.67, 0.14, 0.22, 0.15,
0.18, 0.09, 0.06, 0.09, 0.1, 0.18, 0.08, 0.08, 0.14, 0.06, 0.23,
0.1, 0.09, 0.08, 0.14, 0.13, 0.06, 0.08, 0.13, 0.08, 0.15, 0.11,
0.1, 0.07, 0.11, 0.1, 0.06, 0.11, 0.08, 0.11, 0.11, 0.08, NA,
0.12, 0.22, 0.1, 0.13, 0.08, NA, 0.06, 0.18, 0.13, 0.1, 0.16,
0.15, 0.13, 0.07, 0.09, 0.08, 0.06, 0.14, 0.07, 0.21, 0.17, 0.01,
0.05, 2.07, 0.35, 0.13, 0.08, 0.09, 0.23, 0.55, 0.17, 1.1, 0.06,
0.07, 0.14, 0.04, 0.06, 0.15, 0.08, 0.12, NA, 0.09, 0.97), Ca_pct1 = c(0.69,
0.58, 0.46, 0.46, 0.42, 0.41, 0.51, 0.5, 0.6, 0.83, 0.42, 0.34,
0.69, 0.98, 0.51, 0.43, 0.78, 0.44, 0.38, 0.56, 1.07, 0.46, 0.72,
0.77, 1.08, 0.64, 0.46, 0.57, 0.5, 0.5, 0.88, 0.65, 0.67, 0.28,
0.75, 0.59, 0.49, 0.72, 0.31, 0.42, 0.71, 0.14, 0.42, 0.69, 0.29,
0.39, 0.31, 0.94, 0.7, 0.47, 0.71, 0.38, 0.31, 0.5, 0.47, NA,
0.47, 0.37, 0.67, 0.68, 0.32, NA, 0.64, 0.31, 0.83, 0.52, 0.33,
0.71, 0.91, 0.49, 0.58, 0.35, 0.34, 0.5, 0.54, 0.92, 0.4, 3.74,
1.69, 0.21, 0.4, 0.45, 0.66, 0.49, 0.56, 0.88, 0.41, 0.41, 0.31,
0.53, 0.96, 1.13, 0.35, 0.58, 0.33, 0.56, NA, 0.68, 0.32), Cd_ppm1 = c(0.13,
0.22, 0.12, 0.15, 0.09, 0.99, 0.13, 0.19, 0.88, 0.34, 0.1, 0.15,
0.17, 0.16, 0.14, 0.2, 0.14, 0.11, 0.15, 0.2, 0.14, 0.17, 0.1,
0.17, 0.18, 0.13, 0.11, 0.13, 0.2, 0.12, 0.13, 0.27, 0.13, 0.37,
0.21, 0.12, 0.18, 0.08, 0.14, 0.11, 0.15, 0.41, 0.19, 0.3, 0.23,
0.15, 0.1, 0.34, 0.13, 0.13, 0.09, 0.15, 0.25, 0.17, 0.12, NA,
0.17, 0.22, 0.14, 0.21, 0.11, NA, 0.1, 0.16, 0.27, 0.19, 0.13,
0.22, 0.26, 0.05, 0.17, 0.15, 0.1, 0.39, 0.16, 0.47, 0.21, 0.17,
0.14, 0.59, 1.11, 0.12, 0.13, 0.1, 0.63, 0.47, 0.33, 0.2, 0.11,
0.26, 0.28, 0.11, 0.1, 0.55, 0.37, 0.29, NA, 0.18, 0.82), Ag_ppb2 = c(59L,
73L, 69L, 75L, 85L, 319L, 43L, 73L, 405L, 121L, 33L, 45L, 71L,
67L, 67L, 80L, 50L, 45L, 68L, 140L, 56L, 69L, 51L, 71L, 79L,
51L, 36L, 52L, 93L, 31L, 98L, 134L, 67L, 386L, 47L, 46L, 90L,
63L, 86L, 54L, 59L, 478L, 61L, 114L, 108L, 74L, 72L, 147L, 60L,
74L, 40L, 56L, 256L, 112L, 62L, 87L, 71L, 104L, 109L, 55L, 45L,
84L, 69L, 63L, 107L, 70L, 57L, 73L, 100L, 45L, 43L, 36L, 39L,
161L, 108L, 100L, 93L, 32L, 45L, 187L, 267L, 68L, 37L, 57L, 228L,
74L, 69L, 47L, 65L, 101L, 33L, 32L, 139L, 77L, 78L, NA, 59L,
214L, 410L), As_ppm2 = c(3.9, 3.8, 4.4, 5.4, 1.7, 14.4, 3.1,
5.9, 52.3, 9.7, 3.5, 2.7, 6.7, 5.2, 5, 4.3, 4.8, 4, 3.9, 31.9,
5.3, 6.5, 3.6, 10.4, 3.5, 3.9, 3.6, 4.3, 8.9, 5.3, 3.8, 16.7,
3.7, 6.1, 3.7, 4, 9.6, 6.4, 4, 3.1, 13.2, 22.1, 4.3, 6.9, 3.6,
4.9, 3.4, 4.8, 4.1, 4.8, 4.2, 3.8, 5.3, 9.2, 3.3, 12.5, 5.3,
4.4, 4.8, 5.7, 5, 5.5, 3.4, 4.4, 6.5, 4.8, 4, 6.5, 6.2, 3.4,
4.5, 3.8, 2.6, 4.7, 8, 8.5, 7.6, 2.6, 4.7, 5.2, 15.8, 4, 3.1,
5.3, 343.7, 7.4, 5.1, 3, 11, 7.3, 3, 6.8, 21.1, 4.1, 9.1, NA,
4.4, 21, 122.1), Au_ppb2 = c(0.9, 1.6, 0.1, 1.3, 0.7, 1.8, 0.6,
0.8, 1.6, 2.7, 0.4, 0.9, 0.9, 1.8, 1.5, 1.6, 1.5, 0.9, 2, 1.3,
0.3, 3, 0.8, 2.5, 1.5, 0.4, 1.2, 1.4, 1, 1.1, 0.4, 113.3, 0.6,
2.2, 1.9, 0.7, 0.5, 0.1, 1.8, 0.9, 1.4, 4.3, 1.6, 0.8, 0.7, 0.9,
0.6, 2.4, 5.6, 1.2, 0.9, 1.1, 2.1, 1.1, 0.9, 0.8, 0.9, 1, 4,
0.3, 1.5, 0.5, 1.2, 1, 1.5, 0.1, 1.2, 19.8, 32.8, 0.1, 0.7, 0.7,
1, 0.5, 2.3, 1.6, 1.6, 0.6, 0.9, 1.7, 1.9, 1.3, 1.1, 1.1, 0.9,
4.8, 0.5, 0.4, 1.6, 1, 0.1, 0.9, 1.3, 0.8, 2.7, NA, 0.8, 4, 3.6
), B_ppm2 = c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 22L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 23L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, NA, 10L, 10L, 10L), Ba_ppm2 = c(137.5,
128, 175, 205.6, 262.7, 356.1, 91.2, 212.8, 207, 217.4, 111,
132.4, 179.4, 139.8, 188.9, 164.4, 136, 158.7, 348.9, 96.6, 141.3,
143.7, 187, 121.2, 166.9, 131, 235.9, 189.5, 201.4, 158.7, 148.3,
227, 190, 415.9, 197.2, 178, 268, 221.1, 251.5, 243.3, 260.4,
310, 165.8, 308.2, 342.8, 317, 185, 241.7, 189.2, 291.4, 199.4,
214.7, 312.2, 273, 197.8, 265, 255, 315.2, 281.7, 326, 236.5,
229.7, 197.8, 308.4, 277.2, 258.7, 185.7, 261.2, 354.7, 177.7,
213.2, 226.7, 159.2, 369.5, 359.1, 224.9, 275.4, 54, 106.7, 53.4,
100.9, 194.7, 188.4, 187.4, 162.9, 237.7, 146.9, 189, 214.9,
368.1, 134.8, 82.4, 130.4, 187.8, 291.2, NA, 171.9, 209.5, 318.5
), Be_ppm2 = c(0.2, 0.3, 0.4, 0.3, 0.3, 0.4, 0.1, 0.3, 0.6, 0.4,
0.4, 0.5, 0.4, 0.3, 0.5, 0.7, 0.3, 0.3, 0.2, 0.4, 0.7, 0.4, 0.4,
0.3, 0.4, 0.2, 0.3, 0.3, 0.5, 0.6, 0.5, 0.4, 0.3, 0.3, 0.3, 0.2,
0.2, 0.2, 0.5, 0.2, 0.3, 0.3, 0.2, 0.4, 0.3, 0.2, 0.2, 0.2, 0.3,
0.2, 0.3, 0.2, 0.3, 0.5, 0.3, 0.4, 0.3, 0.3, 0.2, 0.3, 0.1, 0.5,
0.2, 0.6, 0.3, 0.4, 0.4, 0.2, 0.4, 0.3, 0.3, 0.2, 0.2, 0.3, 0.5,
0.3, 0.3, 0.2, 0.2, 1.6, 1.8, 0.5, 0.2, 0.6, 33.1, 0.1, 0.6,
0.05, 0.2, 0.3, 0.7, 0.2, 1.5, 0.3, 0.3, NA, 0.3, 1.2, 1.4),
Bi_ppm2 = c(0.23, 0.28, 0.23, 0.21, 0.12, 0.26, 0.14, 0.16,
0.69, 0.16, 0.12, 0.34, 0.11, 0.11, 0.41, 0.36, 0.12, 0.11,
0.11, 2.86, 0.14, 0.23, 0.19, 0.18, 0.1, 0.05, 0.08, 0.11,
0.15, 0.08, 0.09, 0.15, 0.06, 0.24, 0.08, 0.09, 0.09, 0.12,
0.14, 0.07, 0.07, 0.12, 0.09, 0.18, 0.1, 0.1, 0.09, 0.09,
0.11, 0.06, 0.1, 0.07, 0.1, 0.12, 0.08, 0.09, 0.1, 0.2, 0.09,
0.1, 0.09, 0.17, 0.06, 0.15, 0.12, 0.1, 0.17, 0.13, 0.12,
0.05, 0.08, 0.08, 0.07, 0.17, 0.12, 0.21, 0.17, 0.01, 0.05,
1.93, 0.33, 0.15, 0.05, 0.08, 0.68, 0.12, 0.3, 0.06, 0.06,
0.14, 0.05, 0.08, 0.4, 0.09, 0.12, NA, 0.07, 0.98, 2.21),
Ca_pct2 = c(0.6, 0.56, 0.48, 0.53, 0.4, 0.41, 0.47, 0.51,
0.58, 0.86, 0.41, 0.33, 0.7, 0.9, 0.51, 0.45, 0.67, 0.44,
0.39, 0.56, 1.05, 0.48, 1.21, 0.83, 1.1, 0.66, 0.45, 0.62,
0.5, 0.47, 1.04, 0.66, 0.64, 0.3, 0.74, 0.58, 0.49, 0.65,
0.31, 0.42, 0.62, 0.13, 0.42, 0.84, 0.29, 0.4, 0.32, 1.01,
0.6, 0.46, 0.71, 0.41, 0.3, 0.58, 0.5, 1.02, 0.4, 0.39, 0.87,
0.79, 0.34, 0.44, 0.67, 0.31, 0.79, 0.47, 0.33, 0.67, 0.86,
0.5, 0.49, 0.29, 0.35, 0.5, 0.87, 0.8, 0.39, 3.36, 1.78,
0.22, 0.36, 0.5, 0.57, 0.53, 0.58, 0.37, 0.43, 0.3, 0.46,
1.03, 1.12, 0.36, 0.48, 0.38, 0.52, NA, 0.52, 0.33, 1.21),
Cd_ppm2 = c(0.13, 0.19, 0.12, 0.15, 0.1, 0.97, 0.1, 0.21,
0.92, 0.35, 0.1, 0.09, 0.16, 0.18, 0.16, 0.17, 0.11, 0.11,
0.2, 0.16, 0.11, 0.16, 0.13, 0.17, 0.2, 0.13, 0.14, 0.15,
0.25, 0.05, 0.18, 0.28, 0.09, 0.3, 0.22, 0.09, 0.18, 0.12,
0.1, 0.1, 0.15, 0.3, 0.17, 0.33, 0.2, 0.15, 0.1, 0.59, 0.16,
0.16, 0.1, 0.13, 0.24, 0.21, 0.11, 0.46, 0.12, 0.24, 0.23,
0.17, 0.11, 0.22, 0.13, 0.18, 0.24, 0.16, 0.17, 0.18, 0.23,
0.09, 0.12, 0.1, 0.1, 0.35, 0.37, 0.43, 0.24, 0.16, 0.17,
0.62, 1, 0.13, 0.12, 0.11, 0.56, 0.23, 0.22, 0.15, 0.23,
0.28, 0.12, 0.1, 0.97, 0.36, 0.3, NA, 0.19, 0.89, 3.59)), class = "data.frame", row.names = c(NA,
-99L))
Consider Map (wrapper to mapply) which is the iteration function to run elementwise between equal length lists and saves output into a list. Doing so, you avoid extraneous looping as seen with nested for loops approach.
# EXTRACT NEEDED NAMES
samples1 <- names(YGS_Dupes)[grep("1$", names(YGS_Dupes))][-1] # -1 TO REMOVE Dupe_Code.1
samples2 <- names(YGS_Dupes)[grep("2$", names(YGS_Dupes))]
# SET UP LOOPING FUNCTION
plot_fct <- function(s1, s2) {
s_title <- gsub("1", "", s1)
p <- ggplot(YGS_Dupes, aes_string(x=s1, y=s2)) + geom_point(color="#0072B2") +
ggtitle(paste(s_title, "Duplicate Comparison", sep=" - ")) +
theme(plot.title = element_text(hjust = 0.5), legend.position="top",
axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5))
ggsave(paste0(s_title,".png"))
return(p)
}
# BUILD LIST LOOPING ELEMENTWISE
plot_list2 <- Map(plot_fct, samples1, samples2)
# OUTPUT PLOTS BY NAME
plot_list2$Ag_ppb1
plot_list2$As_ppm1
plot_list2$Au_ppb1
Output (first three plots)
As a general solution to plot scatterplots using for loop, you can use following flow.
Step 1: Create a plotting function
In the following code, I have explicitly provided my dataframe and the x-axis target variable. For the variable on the y-axis I pass the column number in the function so that it would be to run a for-loop later.
sct_plot_function <- function(dataset = car.c2.num, target_x = car.c2.num$price, target_y_num){
ggplot(dataset, aes(x = target_x, y = car.c2.num[,target_y_num])) +
geom_point() +
geom_smooth(level = 0.95) +
theme_bw() +
labs(title = paste("Scatter plot of Price Vs ", colnames(car.c2.num)[target_y_num]), y = colnames(car.c2.num)[target_y_num], x = "Price") +
theme(plot.title = element_text(hjust = 0.5))
}
Step 2: Use a for loop to plot multiple scatter plots in one go.
Using dim(car.c2.num)[2] - 1 to extract the number of columns minus from the dataframe and loop it using i in 1:(dim(car.c2.num)[2] - 1)
The reason I have done is that the 14 variable for me is the target variable which is fixed for the x-axis.
for(i in 1:(dim(car.c2.num)[2] - 1) ){
plot(sct_plot_function(target_y_num = i))
}
you can use this as a basic structure to re-define for your multiple x and y axes.
Can further use nested for loop if you are aiming to plot all variable combinations on x and y axes respectively.
Sample Image:
Scatter plot of Price Vs compression_ratio for Automobile dataset UCI
Try this:
For (i in 1:length(colNames_scatter_dup)){
print(ggplot(YGS_Dup_Scatter, mapping = aes(x = YGS_Dup_Scatter[,names(YGS_Dup_Scatter) %in% colNames_scatter_dup[i]], y = YGS_Dup_Scatter[,names(YGS_Dup_Scatter) %in% colNames_scatter_dup2[i]]))+geom_point())
}
ok Parfait, thank you for helping, discussing your answer with a colleague got me to where I needed to be.
The final result was the following:
YGS_Dup_Scatter = read.csv(file.choose(), header=TRUE, sep=",")
colNames_scatter_dup <- names(YGS_Dup_Scatter)[4:56]
colNames_scatter_dup2 <- names(YGS_Dup_Scatter)[57:109]
for (j in 1:length(colNames_scatter_dup)) {
plt <- ggplot(YGS_Dup_Scatter, mapping = aes_string(x=colNames_scatter_dup[j], y =colNames_scatter_dup2[j])) +
geom_point() + theme_calc() + ggtitle(paste(colNames_scatter_dup[j], "Duplicate Comparison", sep=" - ")) + theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 16), axis.text.x = element_text(face = "bold", size = "14"), axis.text.y = element_text(face = "bold", size = "12"), plot.margin = margin(10, 30, 2, 2), axis.title.y=element_text(face = "bold", size = "14"), plot.background = element_rect(fill = "lightskyblue2"))
print(plt)
ggsave(paste0(i,".png"))
Sys.sleep(2)
}
The key was using the length function and structuring my columns so that it went A1, A2...A53, then B1, B2....etc.
Being the same length allowed the length function to keep them paired.
Thanks for the help everyone!
Related
I have following dataframe:
df <- structure(list(y = c(0.82, 0.77, 0.46, 0.7, 0.82, 0.92, 0.84, 0.88, 0.86, 0.92, 0.91, 0.96, 0.91, 0.92, 0.89, 0.95, 0.95, 0.88, 0.92, 0.88, 0.94, 0.72, 0.9, 0.95, 0.96, 0.92, 0.94, 0.93, 0.93, 0.94, 0.93, 0.89, 0.94, 0.94, 0.91, 0.88, 0.96, 0.91, 0.9, 0.95, 0.83, 0.95, 0.92, 0.91, 0.86, 0.94, 0.93, 0.83, 0.87, 0.76), x = c(0, 0.03, 0.07, 0.1, 2.2, 2.18, 2.33, 2.48, 2.63, 2.77, 2.92, 3.07, 3.22, 3.37, 3.52, 3.66, 3.81, 3.96, 4.11, 4.16, 4.21, 4.26, 4.31, 4.36, 4.41, 4.46, 4.51, 4.55, 4.6, 4.65, 4.7, 4.75, 4.8, 4.85, 4.9, 4.96, 5.01, 5.07, 5.12, 5.18, 5.24, 5.29, 5.35, 5.4, 5.46, 5.51, 5.57, 5.27, 4.98, 4.68), z = c(1.54, 1.48, 1.51, 1.05, 1.29, 0.6, 1.03, 0.95, 0.98, 0.89, 0.81, 0.91, 0.31, 0.69, 0.17, 0.48, 0.51, 0.74, 0.79, 0.77, 0.69, 0.5, 0.75, 0.85, 0.77, 0.7, 0.66, 1.02, 0.69, 0.51, 0.63, 0.45, 0.46, 0.7, 0.74, 0.68, 0.72, 0.84, 0.5, 0.62, 0.32, 0.74, 0.52, 0.65, 1.07, 0.96, 1.03, 1.41, 1.88, 0.83)), row.names = c(1L, 2L, 3L, 4L, 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, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L), class = "data.frame")
I need to fit equation y ~ ax^bz^c for the data given above. I tried the code below:
Log transforms the above equation to log(y) ~ log(a) + blog(x) + clog(z), so, one can fit a linear model as below:
x <- log(df$x)
y = log(df$y)
z = log(df$z)
x[!(is.finite(x))] <- NA
y[!(is.finite(y))] = NA
z[!(is.finite(z))] = NA
# Model fitting
m <- lm(y~x+z, na.action = na.exclude)
coeff <- list(a = coef(m)[1], b = coef(m)[2], c = coef(m)[3])
# Prediction
y_pred <- coeff[[1]] + coeff[[2]]*x + coeff[[3]]*z # or predict(m)
CORR_1 <- cor(y,y_pred, use = "pairwise.complete.obs")
Converting back to original scale
x <- df$x
y = df$y
z = df$z
y_pred <- exp(coeff[[1]])*x^coeff[[2]]*z^coeff[[3]]
CORR_2 <- cor(y,y_pred, use = "pairwise.complete.obs")
I was expecting CORR_1 and CORR_2 to be same but their values are different. Why is that so? What is the best way to fit y ~ ax^bz^c?
I have collected data on 216 individuals. I measured the concentration of the same 7 Substances in each individual, represented by Sub1:Sub7. The concentration of these Substances may be different in individuals from different Locations. I am interested in the level of refinement at which these individuals can be classified into groups based on their concentrations of these substances. I am also interested in seeing how these Substances may be correlated with each other, as the concentration of some may effect the concentration of others. Each Individual in my data set is represented by a unique ID number. Three "nested" grouping variables (Location, State, and Region) can be used to separate these individuals. Multiple Locations are in each State, and multiple States are part of larger Regions. For instance, the individuals in the Locations: APNG, BLEA, and NEAR are all in FL, while the individuals in the Locations: CACT, OYLE, and PIY are all in GA. The states FL and GA are both in Region A. I used this function to conduct an anova:
library(tidyverse)
library(multicomp)
library(multicompView)
tests <- list()
Groups <- c(1:3)
Variables <- 6:12
for(i in Groups){
Group <- as.factor(data[[i]])
for(j in Variables)
{
test_name <- paste0(names(data)[j], "_by_", names(data[i]))
Response <- data[[j]]
sublist <- list()
sublist$aov <- aov(Response ~ Group)
sublist$tukey <- TukeyHSD(sublist$aov)
sublist$multcomp <- multcompLetters(extract_p(sublist$tukey$Group))
tests[[test_name]] <- sublist
}
}
#i can access the results like this:
lapply(tests, function(x) summary(x$aov))
#and access the compact letter display results like this:
lapply(tests, function(x) x$multcomp)
using the object tests, how can I tell R to create boxplots of the TukeyHSD results and show the CLD letters and paste the plots onto a pdf?
This website: r-graph-gallery.com/84-tukey-test.html explains how to do this, but I cannot get it to work with the object tests.
here is my data:
> dput(data)
structure(list(Region = 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 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, 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, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L), .Label = c("A", "B", "C", "D", "E"), class = "factor"),
State = structure(c(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, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 10L, 10L, 10L,
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L), .Label = c("DE", "FL", "GA", "MA",
"MD", "ME", "NC", "NH", "NY", "SC", "VA", "VT"), class = "factor"),
Location = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 20L, 20L, 20L, 20L, 20L, 20L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L
), .Label = c("APNG", "BATO", "BLEA", "CACT", "CHAG", "CHOG",
"COTR", "DTU", "HAB", "LOP", "MASV", "NEAR", "NGUP", "OYLE",
"PIRT", "PIY", "PKE", "PONO", "PPP", "ROG", "VONG", "YENQ"
), class = "factor"), Sex = structure(c(1L, 1L, 1L, 2L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L,
1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
2L), .Label = c("F", "M"), class = "factor"), ID = 1:216,
Sub1 = c(0.03, 0.03, 0.03, 0.04, 0.04, 0.03, 0.03, 0.03,
0.03, 0.03, 0.04, 0.03, 0.04, 0.03, 0.03, 0.03, 0.02, 0.04,
0.03, 0.03, 0.03, 0.02, 0.04, 0.04, 0.02, 0.03, 0.02, 0.03,
0.05, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03,
0.03, 0.03, 0.04, 0.03, 0.04, 0.06, 0.03, 0.03, 0.03, 0.03,
0.02, 0.03, 0.03, 0.03, 0.04, 0.03, 0.02, 0.02, 0.04, 0.03,
0.04, 0.03, 0.03, 0.03, 0.05, 0.03, 0.03, 0.04, 0.03, 0.02,
0.04, 0.02, 0.03, 0.02, 0.02, 0.04, 0.03, 0.02, 0.03, 0.03,
0.05, 0.04, 0.03, 0.02, 0.03, 0.05, 0.02, 0.04, 0.03, 0.05,
0.03, 0.04, 0.02, 0.03, 0.02, 0.03, 0.03, 0.03, 0.02, 0.05,
0.03, 0.03, 0.04, 0.02, 0.02, 0.04, 0.05, 0.03, 0.03, 0.02,
2.03, 2.03, 2.03, 2.04, 2.04, 2.03, 2.03, 2.03, 2.03, 2.03,
2.04, 2.03, 2.04, 2.03, 2.03, 2.03, 2.02, 2.04, 2.03, 2.03,
2.03, 2.02, 2.04, 2.04, 2.02, 2.03, 2.02, 2.03, 2.05, 2.03,
2.03, 2.03, 2.03, 2.03, 2.03, 2.03, 2.03, 2.03, 2.03, 2.03,
2.04, 2.03, 2.04, 2.06, 2.03, 2.03, 2.03, 2.03, 2.02, 2.03,
2.03, 2.03, 2.04, 2.03, 2.02, 2.02, 2.04, 2.03, 2.04, 2.03,
2.03, 2.03, 2.05, 2.03, 2.03, 2.04, 2.03, 2.02, 2.04, 2.02,
2.03, 2.02, 2.02, 2.04, 2.03, 2.02, 2.03, 2.03, 2.05, 2.04,
2.03, 2.02, 2.03, 2.05, 2.02, 2.04, 2.03, 2.05, 2.03, 2.04,
2.02, 2.03, 2.02, 2.03, 2.03, 2.03, 2.02, 2.05, 2.03, 2.03,
2.04, 2.02, 2.02, 2.04, 2.05, 2.03, 2.03, 2.02), Sub2 = c(0.69,
1.28, 1.27, 2.25, 1.05, 1.76, 1.57, 1.09, 0.68, 1.35, 0.85,
1.55, 0.12, 0, 0.58, 1.13, 0.1, 1.9, 0.54, 1.48, 0.8, 0.52,
1.76, 1.77, 1.24, 0.63, 0.63, 0.57, 0.63, 0.53, 1.32, 1.79,
1.16, 1.11, 1.1, 1.92, 1.06, 1.18, 0.43, 0.67, 0.75, 2.37,
3.93, 0.3, 2.8, 1.25, 0.9, 1.32, 0.5, 0.4, 0.72, 0.34, 0.12,
0.89, 0.69, 1.13, 1.22, 0.88, 4.13, 1.27, 0.62, 2.9, 2.42,
0.9, 0.4, 1.29, 1.61, 0.3, 1.47, 0.36, 1.27, 0.84, 1.81,
0.18, 0.47, 1.01, 0.85, 0.59, 1.73, 0.72, 0.5, 0.83, 0.9,
0.81, 0.59, 2.84, 2.24, 2.68, 1.18, 1.36, 0.84, 1.79, 1.01,
0.34, 0.41, 2.22, 0.51, 0.42, 1.26, 2.26, 1.79, 1.43, 1.3,
1.8, 2.21, 1.65, 2.39, 0.31, 2.69, 3.28, 3.27, 4.25, 3.05,
3.76, 3.57, 3.09, 2.68, 3.35, 2.85, 3.55, 2.12, 2, 2.58,
3.13, 2.1, 3.9, 2.54, 3.48, 2.8, 2.52, 3.76, 3.77, 3.24,
2.63, 2.63, 2.57, 2.63, 2.53, 3.32, 3.79, 3.16, 3.11, 3.1,
3.92, 3.06, 3.18, 2.43, 2.67, 2.75, 4.37, 5.93, 2.3, 4.8,
3.25, 2.9, 3.32, 2.5, 2.4, 2.72, 2.34, 2.12, 2.89, 2.69,
3.13, 3.22, 2.88, 6.13, 3.27, 2.62, 4.9, 4.42, 2.9, 2.4,
3.29, 3.61, 2.3, 3.47, 2.36, 3.27, 2.84, 3.81, 2.18, 2.47,
3.01, 2.85, 2.59, 3.73, 2.72, 2.5, 2.83, 2.9, 2.81, 2.59,
4.84, 4.24, 4.68, 3.18, 3.36, 2.84, 3.79, 3.01, 2.34, 2.41,
4.22, 2.51, 2.42, 3.26, 4.26, 3.79, 3.43, 3.3, 3.8, 4.21,
3.65, 4.39, 2.31), Sub3 = c(1.32, 0.19, 0.27, 0.73, 0.41,
0.37, 0.89, 1.35, 0.49, 1.32, 0.69, 0, 0.57, 0.24, 0.23,
0.71, 0, 0, 0, 0.58, 0.32, 1.1, 0.45, 0.61, 0.38, 0.3, 0.01,
0.06, 0.48, 0.62, 0.64, 1.96, 0.61, 0.43, 0.25, 0.34, 0.17,
0.57, 0.1, 0.6, 1.07, 0.44, 0.12, 0.55, 0.08, 0.56, 0.59,
0.66, 0.44, 0.58, 0.75, 0.99, 0.77, 0.57, 0.35, 0.18, 0.16,
0.31, 0.04, 0.17, 0.46, 0.19, 0.8, 0.61, 1.14, 0.3, 0.08,
0.25, 0.78, 1.07, 0.38, 0.17, 0.42, 0.48, 0.55, 0.74, 2.98,
1.96, 0.51, 0.63, 0, 0.52, 0.32, 0.23, 0.31, 0.09, 0.06,
0.26, 0.23, 0.58, 1.49, 0.46, 0.33, 0.37, 1.16, 0.91, 0.41,
0.72, 0.2, 0.84, 0.71, 0.56, 0.34, 0.68, 0.81, 0.52, 0.78,
0.19, 3.32, 2.19, 2.27, 2.73, 2.41, 2.37, 2.89, 3.35, 2.49,
3.32, 2.69, 2, 2.57, 2.24, 2.23, 2.71, 2, 2, 2, 2.58, 2.32,
3.1, 2.45, 2.61, 2.38, 2.3, 2.01, 2.06, 2.48, 2.62, 2.64,
3.96, 2.61, 2.43, 2.25, 2.34, 2.17, 2.57, 2.1, 2.6, 3.07,
2.44, 2.12, 2.55, 2.08, 2.56, 2.59, 2.66, 2.44, 2.58, 2.75,
2.99, 2.77, 2.57, 2.35, 2.18, 2.16, 2.31, 2.04, 2.17, 2.46,
2.19, 2.8, 2.61, 3.14, 2.3, 2.08, 2.25, 2.78, 3.07, 2.38,
2.17, 2.42, 2.48, 2.55, 2.74, 4.98, 3.96, 2.51, 2.63, 2,
2.52, 2.32, 2.23, 2.31, 2.09, 2.06, 2.26, 2.23, 2.58, 3.49,
2.46, 2.33, 2.37, 3.16, 2.91, 2.41, 2.72, 2.2, 2.84, 2.71,
2.56, 2.34, 2.68, 2.81, 2.52, 2.78, 2.19), Sub4 = c(0.63,
0.05, 0.2, 0.41, 0.43, 0.54, 0.26, 0.78, 0.13, 0.8, 0.47,
0.65, 0, 0.22, 0.45, 0.85, 0.47, 0, 0.62, 0.59, 0.14, 0.8,
0.9, 0.88, 0.56, 0.56, 0.47, 0.24, 0.62, 1.77, 0.56, 0.99,
0.21, 0.9, 0.62, 0.58, 0.41, 0.97, 0.2, 0.9, 0.68, 0.52,
0.14, 1.27, 0.63, 0.51, 0.12, 0.61, 0.31, 0.43, 0.62, 1.18,
0.95, 0.59, 0.39, 0.26, 0.53, 0.77, 0.4, 0.39, 0, 0.19, 0.82,
1.1, 0.46, 0.25, 0.29, 0.2, 2.01, 0.36, 0.62, 0.54, 0.48,
0.87, 0.66, 1.46, 2.59, 1.37, 1.28, 0.99, 0.71, 0.32, 0.64,
0.66, 0.47, 0.48, 0.38, 0.67, 0.18, 1.02, 0.54, 0.53, 0.25,
0.43, 1.02, 0.58, 0.58, 0.48, 0.2, 0.7, 0.38, 0.28, 0.65,
1.21, 1.03, 0.38, 0.6, 0.44, 2.63, 2.05, 2.2, 2.41, 2.43,
2.54, 2.26, 2.78, 2.13, 2.8, 2.47, 2.65, 2, 2.22, 2.45, 2.85,
2.47, 2, 2.62, 2.59, 2.14, 2.8, 2.9, 2.88, 2.56, 2.56, 2.47,
2.24, 2.62, 3.77, 2.56, 2.99, 2.21, 2.9, 2.62, 2.58, 2.41,
2.97, 2.2, 2.9, 2.68, 2.52, 2.14, 3.27, 2.63, 2.51, 2.12,
2.61, 2.31, 2.43, 2.62, 3.18, 2.95, 2.59, 2.39, 2.26, 2.53,
2.77, 2.4, 2.39, 2, 2.19, 2.82, 3.1, 2.46, 2.25, 2.29, 2.2,
4.01, 2.36, 2.62, 2.54, 2.48, 2.87, 2.66, 3.46, 4.59, 3.37,
3.28, 2.99, 2.71, 2.32, 2.64, 2.66, 2.47, 2.48, 2.38, 2.67,
2.18, 3.02, 2.54, 2.53, 2.25, 2.43, 3.02, 2.58, 2.58, 2.48,
2.2, 2.7, 2.38, 2.28, 2.65, 3.21, 3.03, 2.38, 2.6, 2.44),
Sub5 = c(1.14, 1.38, 1.5, 1.43, 1.65, 1.34, 1.29, 1.72, 1.32,
1.17, 1.19, 1.35, 1.34, 1.06, 1.24, 1.33, 1.2, 1.31, 1.29,
1.37, 1.42, 1.08, 1.77, 1.32, 1.2, 1.14, 1.48, 0.98, 1.33,
1.65, 1.24, 1.43, 1.41, 1.2, 1.42, 1.09, 1.04, 1.57, 0.78,
1.37, 0.99, 1.4, 1.13, 1.34, 1.35, 1.23, 0.93, 0.94, 1.02,
1.16, 1.08, 0.96, 1.33, 1.19, 1.25, 1.44, 1.62, 1.27, 1.4,
1.4, 1.29, 1.53, 1.43, 1.33, 1.25, 1.82, 1.45, 1.36, 1.38,
1.34, 1.29, 1.86, 1.15, 1.31, 1.21, 1.23, 1.42, 1.57, 1.23,
0.99, 1.33, 1.74, 1.03, 1.33, 1.41, 1.01, 0.97, 1.46, 1.55,
1.04, 1.22, 1.19, 1.74, 1.64, 1.35, 1.34, 1.21, 1.55, 1.31,
1.5, 1.45, 1.21, 0.83, 1.17, 1.25, 1.54, 1.5, 1.11, 3.14,
3.38, 3.5, 3.43, 3.65, 3.34, 3.29, 3.72, 3.32, 3.17, 3.19,
3.35, 3.34, 3.06, 3.24, 3.33, 3.2, 3.31, 3.29, 3.37, 3.42,
3.08, 3.77, 3.32, 3.2, 3.14, 3.48, 2.98, 3.33, 3.65, 3.24,
3.43, 3.41, 3.2, 3.42, 3.09, 3.04, 3.57, 2.78, 3.37, 2.99,
3.4, 3.13, 3.34, 3.35, 3.23, 2.93, 2.94, 3.02, 3.16, 3.08,
2.96, 3.33, 3.19, 3.25, 3.44, 3.62, 3.27, 3.4, 3.4, 3.29,
3.53, 3.43, 3.33, 3.25, 3.82, 3.45, 3.36, 3.38, 3.34, 3.29,
3.86, 3.15, 3.31, 3.21, 3.23, 3.42, 3.57, 3.23, 2.99, 3.33,
3.74, 3.03, 3.33, 3.41, 3.01, 2.97, 3.46, 3.55, 3.04, 3.22,
3.19, 3.74, 3.64, 3.35, 3.34, 3.21, 3.55, 3.31, 3.5, 3.45,
3.21, 2.83, 3.17, 3.25, 3.54, 3.5, 3.11), Sub6 = c(0.2, 0.15,
0.16, 0.14, 0.19, 0.12, 0.14, 0.35, 0.29, 0.25, 0.06, 0.16,
0.18, 0.65, 0.18, 0.12, 0.42, 0.09, 0.13, 0.12, 0.22, 0.49,
0.18, 0.11, 0.29, 0.16, 0.18, 0.15, 0.46, 0.19, 0.15, 0.19,
0.1, 0.09, 0.11, 0.14, 0.1, 0.31, 0.53, 0.32, 0.23, 0.18,
0.14, 0.38, 0.19, 0.1, 0.14, 0.08, 0.21, 0.13, 0.08, 0.08,
0.26, 0.14, 0.17, 0.09, 0.09, 0.22, 0.26, 0.09, 0.3, 0.16,
0.17, 0.09, 0.12, 0.17, 0.14, 0.34, 0.12, 0.21, 0.1, 0.27,
0.11, 0.13, 0.15, 0.17, 0.21, 0.16, 0.12, 0.36, 0.16, 0.17,
0.27, 0.32, 0.15, 0.13, 0.14, 0.15, 0.1, 0.26, 0.25, 0.08,
0.25, 0.19, 0.38, 0.08, 0.64, 0.71, 0.1, 0.18, 0.12, 0.13,
0.1, 1.17, 0.14, 0.19, 0.14, 0.24, 2.2, 2.15, 2.16, 2.14,
2.19, 2.12, 2.14, 2.35, 2.29, 2.25, 2.06, 2.16, 2.18, 2.65,
2.18, 2.12, 2.42, 2.09, 2.13, 2.12, 2.22, 2.49, 2.18, 2.11,
2.29, 2.16, 2.18, 2.15, 2.46, 2.19, 2.15, 2.19, 2.1, 2.09,
2.11, 2.14, 2.1, 2.31, 2.53, 2.32, 2.23, 2.18, 2.14, 2.38,
2.19, 2.1, 2.14, 2.08, 2.21, 2.13, 2.08, 2.08, 2.26, 2.14,
2.17, 2.09, 2.09, 2.22, 2.26, 2.09, 2.3, 2.16, 2.17, 2.09,
2.12, 2.17, 2.14, 2.34, 2.12, 2.21, 2.1, 2.27, 2.11, 2.13,
2.15, 2.17, 2.21, 2.16, 2.12, 2.36, 2.16, 2.17, 2.27, 2.32,
2.15, 2.13, 2.14, 2.15, 2.1, 2.26, 2.25, 2.08, 2.25, 2.19,
2.38, 2.08, 2.64, 2.71, 2.1, 2.18, 2.12, 2.13, 2.1, 3.17,
2.14, 2.19, 2.14, 2.24), Sub7 = c(0.01, 0, 0, 0.01, 0, 0,
0.01, 0.01, 0.02, 0.03, 0.01, 0, 0.03, 0, 0.02, 0, 0, 0,
0.01, 0.03, 0.03, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01, 0,
0, 0.05, 0.02, 0.04, 0.02, 0, 0.02, 0.02, 0.02, 0.04, 0.01,
0.02, 0.04, 0.02, 0.01, 0.01, 0.01, 0.01, 0.03, 0.02, 0,
0.02, 0.05, 0.14, 0, 0.01, 0, 0.01, 0.01, 0, 0.01, 0.02,
0.01, 0.02, 0.01, 0.03, 0.05, 0.06, 0.03, 0.02, 0.11, 0.05,
0.02, 0.02, 0, 0.01, 0, 0.01, 0.06, 0.04, 0.02, 0.02, 0,
0.02, 0.01, 0.02, 0.01, 0, 0.01, 0.01, 0.02, 0.01, 0.02,
0.01, 0, 0.01, 0.06, 0.01, 0.02, 0.01, 0.01, 0.03, 0.02,
0.03, 0.03, 0.02, 0.09, 0, 0.19, 0.02, 2.01, 2, 2, 2.01,
2, 2, 2.01, 2.01, 2.02, 2.03, 2.01, 2, 2.03, 2, 2.02, 2,
2, 2, 2.01, 2.03, 2.03, 2.02, 2.02, 2.02, 2.01, 2.01, 2.01,
2, 2, 2.05, 2.02, 2.04, 2.02, 2, 2.02, 2.02, 2.02, 2.04,
2.01, 2.02, 2.04, 2.02, 2.01, 2.01, 2.01, 2.01, 2.03, 2.02,
2, 2.02, 2.05, 2.14, 2, 2.01, 2, 2.01, 2.01, 2, 2.01, 2.02,
2.01, 2.02, 2.01, 2.03, 2.05, 2.06, 2.03, 2.02, 2.11, 2.05,
2.02, 2.02, 2, 2.01, 2, 2.01, 2.06, 2.04, 2.02, 2.02, 2,
2.02, 2.01, 2.02, 2.01, 2, 2.01, 2.01, 2.02, 2.01, 2.02,
2.01, 2, 2.01, 2.06, 2.01, 2.02, 2.01, 2.01, 2.03, 2.02,
2.03, 2.03, 2.02, 2.09, 2, 2.19, 2.02)), class = "data.frame", row.names = c(NA,
-216L))
I think the issue with your tests object is that it holds too much informations to figure out how to plot it.
Here, I focused only on Regions columns, but you can apply the same workflow to other categorical columns of your dataset.
1) We need to obtain the label (letters) associated to each region for each substance, so recycling your loop, I did this:
library(multcomp)
library(multcompView)
Labels_box = NULL
Group <- as.factor(data[,"Region"])
for(j in 6:12)
{
Response <- data[, j]
TUKEY <- TukeyHSD(aov(lm(Response ~ Group)))
MultComp <- multcompLetters(extract_p(TUKEY$Group))
Region <- names(MultComp$Letters)
Labels <- MultComp$Letters
df <- data.frame(Region, Labels)
df$Substance <- colnames(data)[j]
if(j == 1){Labels_box = df}
else{Labels_box = rbind(Labels_box,df)}
}
Now, the dataset Labels_box should look like:
head(Labels_box)
Region Labels Substance
B B a Sub1
C C b Sub1
D D b Sub1
E E b Sub1
A A a Sub1
B1 B a Sub2
2) Next, in order to add them on the top of each boxplot, we will have to define the y position for each labels. So, we are going to calculate the max value of each region for each substance using dplyr and tidyr:
library(tidyverse)
Max_Val <- data %>% pivot_longer(., cols = starts_with("Sub"), names_to = "Substance", values_to = "Value") %>%
group_by(Region, Substance) %>% summarise(MAX = max(Value)+0.2)
# A tibble: 6 x 3
# Groups: Region [1]
Region Substance MAX
<fct> <chr> <dbl>
1 A Sub1 0.26
2 A Sub2 4.13
3 A Sub3 1.55
4 A Sub4 2.21
5 A Sub5 2.06
6 A Sub6 0.85
And we combine both Labels_box and Max_Val datasets using left_join:
Labels_box <- left_join(Labels_box, Max_Val, by = c("Region" = "Region", "Substance" = "Substance"))
Region Labels Substance MAX
1 B a Sub1 0.25
2 C b Sub1 2.25
3 D b Sub1 2.26
4 E b Sub1 2.25
5 A a Sub1 0.26
6 B a Sub2 4.33
3) Finally, we need to reshape in a long format all values for each substances from your data to match the grammar used by ggplot. For that, we can re-use the pivot_longer function seen in 2):
library(tidyverse)
data_box <- data %>% pivot_longer(., cols = starts_with("Sub"), names_to = "Substance", values_to = "Value")
# A tibble: 6 x 7
Region State Location Sex ID Substance Value
<fct> <fct> <fct> <fct> <int> <chr> <dbl>
1 A FL APNG F 1 Sub1 0.03
2 A FL APNG F 1 Sub2 0.69
3 A FL APNG F 1 Sub3 1.32
4 A FL APNG F 1 Sub4 0.63
5 A FL APNG F 1 Sub5 1.14
6 A FL APNG F 1 Sub6 0.2
We are almost ready but in order to set a color matching group identified by Tukey test, we need to add the label on our data_box.
For that, we can do a left_join:
data_box <- left_join(data_box,Labels_box, by = c("Region" = "Region", "Substance" = "Substance"))
# A tibble: 6 x 9
Region State Location Sex ID Substance Value Labels MAX
<fct> <fct> <fct> <fct> <int> <chr> <dbl> <fct> <dbl>
1 A FL APNG F 1 Sub1 0.03 a 0.26
2 A FL APNG F 1 Sub2 0.69 a 4.13
3 A FL APNG F 1 Sub3 1.32 a 1.55
4 A FL APNG F 1 Sub4 0.63 a 2.21
5 A FL APNG F 1 Sub5 1.14 a 2.06
6 A FL APNG F 1 Sub6 0.2 a 0.85
4) Now, we are ready to plot everything:
library(ggplot2)
ggplot(data_box, aes(x = Region, y = Value, fill = Labels))+
geom_boxplot()+
geom_text(data = Labels_box,aes( x = Region, y = MAX, label = Labels))+
facet_grid(.~Substance, scales = "free")
And you get this:
Does it look satisfying for you ?
I am adding a color bar using geom_rect() in combination with facet_wrap(), but for some reason 30 layers are added, which means the bar is completely dark even though I use alpha = 0.2.
I can export to powerpoint and delete all the extra layes manually, but that is a huuuge pain. Is there a way to fix this problem?
I have tried to restart the terminal and to only load the package needed, thinking maybe the error occured do to a loaded function, but no, it does not seem to be the case.
dat <- structure(list(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, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 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, 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, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 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, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L), .Label = c("alat", "asat", "chol", "cl",
"fhb", "ft3", "ft4", "ggt", "gluk", "hdlch", "hrs", "k", "kreae",
"ldh", "ldlch", "lipa", "mg", "na", "phos", "prot", "trig", "tsh"
), class = "factor"), diff_A = c(0.03, -0.02, -0.01, 0, -0.03,
-0.01, -0.01, 0.03, 0.02, 0, 0.01, 0.0099999999999999, 0, 0.02,
0.01, 0, -0.03, -0.02, -0.01, 0.02, 0, -0.01, 0.02, 0.02, 0,
0.03, -0.02, -0.01, 0, 0.00999999999999995, 0, 0, 0.01, 0, -0.0299999999999999,
0.04, 0.03, -0.04, 0, 0.02, 0.03, -0.03, -0.01, -0.05, -0.07,
-0.01, -0.00999999999999995, 0.02, 0, 0.0099999999999999, 0.01,
0.06, -0.04, 0.01, 0.06, -0.03, 0.01, 0.03, 0.02, 0, 0.02, 0,
0.00999999999999995, 0.00999999999999995, -0.01, 0.01, -0.02,
0.02, 0.02, 0.13, 0.0999999999999996, 0.2, 0, 0, 0.3, 0, -0.2,
0.100000000000001, 0, -0.2, 0.0999999999999996, -0.1, 0, -0.300000000000001,
-0.0999999999999996, 0, 0, -0.2, 0.100000000000001, -0.100000000000001,
-0.3, -0.0999999999999996, -0.0999999999999996, 0.3, 0, 0.2,
0, 0, 0.100000000000001, -0.2, 0, 0, -0.199999999999999, 0.0999999999999996,
-0.0999999999999996, 1, -1, 0, 0, 3, -1, 0, 0, -1, -2, 0, -2,
0, -1, 1, 0, -2, -2, 0, 1, -1, 1, -1, 3, -2, 0, 0, -1, -1, 1,
0, 0, -1, 0, 0, 1, 0, 2, 1, -1, 1, 0, 3, 2, -3, 4, 1, -2, 2,
1, 3, 0, 2, 2, 4, -2, -1, 1, 1, 1, 1, 1, 3, 5, 0, 1.1, -1, 1,
1, 1, 0.23, -0.71, 0.21, 0.0599999999999996, -0.4, 0.59, -0.0299999999999994,
0.0899999999999999, 0.15, -0.0700000000000003, -0.04, -0.0999999999999996,
0.13, -0.79, -0.27, -0.18, -0.0600000000000001, -0.26, 0.24,
0.63, -0.0500000000000007, -0.28, -0.31, 0.43, -0.2, -0.0499999999999998,
0.149999999999999, -0.319999999999999, 0.0999999999999996, 0.34,
0.0499999999999998, -0.1, 0.3, 0.0699999999999998, 0.0600000000000001,
0.699999999999999, 0.6, 0, 0.300000000000001, -0.199999999999999,
-0.0299999999999994, -0.299999999999999, -0.0999999999999996,
-0.199999999999999, 0.0999999999999996, 0, 0.0999999999999979,
0.0999999999999996, -0.0999999999999996, -0.200000000000001,
-0.0299999999999994, -0.300000000000001, -0.9, -0.0999999999999996,
0.5, -0.5, 0.0999999999999996, -0.0999999999999996, 0.4, -0.200000000000001,
0.300000000000001, 0, -0.199999999999999, -0.4, 0.4, -0.0999999999999996,
0.5, 0.800000000000001, -0.100000000000001, 0.5, 0.02, 0.01,
-0.02, -0.01, 0.05, 0, 0.02, 0, -0.00999999999999995, 0, -0.01,
0.0599999999999999, -0.01, 0.03, 0.01, 0.04, 0.07, 0.05, -0.01,
-0.06, 0.03, -0.03, 0, -0.03, 0.04, 0.01, -0.01, 0, 0.02, -0.03,
0.02, 0.03, 0.03, -0.02, 0, -0.0999999999999996, 0.100000000000001,
0.0999999999999996, -0.199999999999999, -0.4, -0.6, -0.0999999999999996,
0.2, 0, 0.1, -0.0999999999999996, 0.0999999999999996, -0.1, 0.0999999999999996,
-0.100000000000001, 0.0999999999999996, -0.7, -0.2, 0.4, 0.399999999999999,
-0.0999999999999996, -0.0999999999999996, -0.100000000000001,
-0.2, -0.100000000000001, 0.100000000000001, -0.0999999999999996,
-0.1, 0.100000000000001, -0.3, 0, 0, -0.300000000000001, -0.1,
0.3, 0.01, -0.02, 0.01, -0.0900000000000001, 0.11, 0.00999999999999979,
-0.01, -0.01, 0.04, -0.0699999999999998, -0.04, -0.03, 0.03,
-0.0399999999999998, 0.1, 0, 0.03, -0.0700000000000001, -0.0599999999999998,
0.04, 0.03, 0.12, -0.0900000000000001, 0.1, -0.0600000000000001,
0.0700000000000001, 0.02, 0, -0.0399999999999998, 0.0900000000000003,
-0.02, -0.03, 0.03, 0.11, 0, 1, 2, 0, 1, 20, 8, 4, 9, -12, -23,
1, -13, -2, 2, -10, 0, 2, 2, 2, 2, 7, 9, -7, 6, 1, -9, -3, 0,
-12, 12, -2, 1, 14, -3, 4, 0, 0, 0.1, 0, 0.0999999999999996,
-0.0999999999999996, 0, 0.1, 0, -0.1, 0, 0, -0.0999999999999996,
0, 0, 0, -0.3, -0.0999999999999996, 0.1, 0.1, 0, -0.1, 0, 0,
0.1, -0.2, 0.1, 0, 0, 0, 0, 0, 0, 0, 0.2, -3, 1, 4, 0, -2, 0,
-1, 3, 1, -3, -5, -2, -1, -4, -2, -1, -3, -8, 4, 0, -14, 6, 1,
16, -14, 1, 5, 1, -2, 7, 0, -8, 3, -2, -2, 0.0800000000000001,
0.12, 0.04, 0.0600000000000001, 0.0499999999999998, -0.26, 0.0600000000000001,
0.0499999999999998, 0.42, 0.02, 0.1, 0.3, 0, 0.32, 0.02, 0.11,
0.0900000000000003, 0.0600000000000001, -0.2, 0.26, -0.14, -0.32,
0.27, -0.24, 0.0300000000000002, 0.0799999999999996, 0.14, 0.59,
0.25, 0.02, 0.11, 0.0500000000000003, 0.13, 0.27, 0.14, 0, 0.0100000000000002,
-0.02, 0, -0.11, -0.12, -0.02, -0.13, -0.02, 0.1, 0, 0.17, 0.11,
-0.14, 0.0500000000000003, 0.00999999999999979, 0.02, -0.0900000000000003,
-0.0599999999999998, 0.04, -0.0899999999999999, -0.0899999999999999,
0.13, 0.32, -0.22, 0.14, 0.00999999999999979, 0.04, -0.11, -0.01,
0.0299999999999998, 0.0800000000000001, -0.34, 0.04, -0.2, 0.04,
0.0799999999999996, 0.0299999999999998, 0.0499999999999998, 0.19,
-0.0100000000000007, 0, 0.17, -0.0800000000000001, -0.12, 0.15,
0.00999999999999979, 0.15, 0.1, -0.0299999999999998, 0.04, -0.15,
-0.22, 0.17, 0.0899999999999999, -0.26, -0.2, 0.1, 0.2, -0.46,
0.02, 0.13, -0.0100000000000002, -0.01, 0.0299999999999998, -0.1,
-0.18, -0.11, -0.0899999999999999, -0.11, 0.01, -0.01, 0.02,
0.0199999999999999, -0.01, 0.03, -0.01, 0.03, 0, -0.02, 0, 0,
0.02, -0.04, 0.05, 0, 0.0299999999999999, 0.01, 0.0399999999999999,
0.0499999999999999, -0.0599999999999999, -0.01, -0.01, 0.03,
-0.0299999999999999, -0.01, -0.03, -0.01, 0.02, 0.01, -0.03,
0, 0.0499999999999999, -0.05, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, -3,
0, -1, 0, -2, 2, 0, 1, 0, 0, 1, -1, 1, 1, 0, 0, 0, 2, -1, -1,
1, -1, -1, -1, 0, 0, 0.01, 0.05, -0.02, 0.01, 0.02, -0.0299999999999998,
-0.02, 0.03, 0.03, 0.01, 0.04, 0.21, -0.03, -0.0700000000000001,
0.01, -0.0299999999999999, -0.08, -0.0600000000000001, 0.02,
0, -0.0699999999999998, -0.03, 0.03, -0.02, 0, -0.03, 0, -0.05,
0.02, 0.01, -0.0399999999999999, 0, 0.03, 0.01, 0.01, 1, 0, 0,
0, 2, -1, -2, 3, -1, -1, 1, -1, 0, 0, 0, -2, 1, 2, -3, -1, 2,
1, 1, 1, -1, 2, 1, 1, 1, 2, 0, -1, 3, -2, -1, 0.01, 0, -0.02,
0.04, 0, -0.04, 0.03, -0.0299999999999999, -0.01, -0.01, 0.01,
0.01, 0.01, 0.02, 0.03, -0.09, 0.04, -0.0600000000000001, 0.05,
0.05, -0.0499999999999998, -0.0199999999999999, 0.01, 0.05, -0.0599999999999999,
0.0699999999999998, 0, 0.02, -0.01, -0.05, -0.02, 0.02, 0, 0.0399999999999998,
-0.0399999999999998, 0.01, -0.03, -0.02, -0.01, 0.02, 0.0600000000000001,
-0.05, 0, -0.12, -0.13, -0.03, 0, -0.0600000000000001, 0.03,
-0.01, 0, 0.02, 0.04, -0.0600000000000001, 0.035, -0.02, 0.0309999999999999,
0.0599999999999998, 0.01, 0.03, 0.0500000000000003, -0.0399999999999996,
0.0499999999999998, 0, 0.00800000000000001, 0, -0.00900000000000001,
0.14, 0, -0.025), MD_Fuss = c(0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.09, 0.09, 0.09, 0.09,
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09,
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09,
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.54, 0.54,
0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54,
0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54,
0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54,
6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49,
6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49,
6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49,
6.49, 6.49, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66,
0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66,
0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66,
0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.81, 0.81, 0.81, 0.81, 0.81,
0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81,
0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81,
0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.35,
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35,
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35,
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35,
0.35, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84,
12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84,
12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84,
12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84,
12.84, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17,
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17,
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17,
0.17, 0.17, 0.17, 0.17, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79,
16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79,
16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79,
16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79,
16.79, 16.79, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36,
0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36,
0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36,
0.36, 0.36, 0.36, 0.36, 0.36, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29,
0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29,
0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29,
0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.35, 0.35, 0.35, 0.35,
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35,
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35,
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21,
5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21,
5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21,
5.21, 5.21, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19,
3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19,
3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19,
3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 0.11, 0.11, 0.11, 0.11, 0.11,
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11,
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11,
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16)), class = "data.frame", row.names = c(NA,
-770L))
and this is the plot:
p <- ggplot(data=dat, aes(y = diff_A))+
geom_boxplot(outlier.shape = 1)+
geom_rect(aes(ymin = -MD_Fuss, ymax = MD_Fuss), xmin = -Inf, xmax =Inf, alpha = 0.2)+
theme_bw()+ theme(panel.grid = element_blank())+
xlab('')+ ylab('[mmol/L]') +
scale_y_continuous(expand = c(0.5, 0))+
facet_wrap(.~variable, scales = 'free')
p
geom_rect() draws a rectangle for each row in your data. To get only one rectangle per facet, you need to pass it a data set that contains only one row per faceting variable. Since MD_Fuss seems to be constant within a variable, you can create that data set with unique(dat[, c("variable", "MD_Fuss")]), and then pass it as the data argument to geom_rect():
library(ggplot2)
p <- ggplot(data = dat) +
geom_boxplot(aes(y = diff_A), outlier.shape = 1) +
geom_rect(
data = unique(dat[, c("variable", "MD_Fuss")]),
aes(ymin = -MD_Fuss, ymax = MD_Fuss),
xmin = -Inf, xmax = Inf, alpha = 0.2,
) +
theme_bw() + theme(panel.grid = element_blank()) +
xlab("") + ylab("[mmol/L]") +
scale_y_continuous(expand = c(0.5, 0)) +
facet_wrap(. ~ variable, scales = "free")
p
#> Warning: Removed 2 rows containing missing values (geom_rect).
Created on 2019-07-19 by the reprex package (v0.3.0.9000)
I have a previous post regarding ploting longer ticks at particular y-axis values (plotting longer ticks at particular y-axis values).
My y-axis is at log-scale.y values were labeled at 0.01, 0.1, 1, 10, and 20. I also want ticks corresponding to these breaks longer than other ticks. jaySF suggested using annotation_logticks, however, the ticks appear inside the plotting area and I want the ticks to be outside.
Sandy Muspratt's answer from ggplot2: Have shorter tick marks for tick marks without labels seems to help, but I tried similar code and did not succeed. My code following Sandy's method can be seen in my previous post.
I wish to know either how should I follow Sandy's method, or use some new way, to make ticks at 0.01, 0.1, 1, 10, and 20 longer, which nothing else in the graph changes.
Your code (from here) was close. ticks contains two grobs: the tick marks and the tick mark labels. On the x-axis, the tick marks come first, followed by the labels; but on the y-axis, the the labels come first, followed by the tick marks. Thus to select the tick marks on the y-axis, you need: marks = ticks$grobs[[2]]
In addition, the sequence of long and short tick marks is not as simple as in the example you followed; in particular, the last two tick marks are long. In the code below, I list the x-coordinates of start and end points of all the tick marks. (There is probably some way to automate this step using the ggplot_build information, but if your plot is a one-off, it is just as easy to set the x-coordinates manually.)
Also, I would move the tick mark labels a little to the left. (Your data is at the very end of the post.)
library(ggplot2)
library(scales)
library(grid)
y_breaks <- c(seq(0.01, 0.1, 0.01), seq(0.2, 1, 0.1), seq(2, 10, 1), 20)
y_labels <- y_breaks
y_labels[c(F, rep(T, 8), F, rep(T, 8), F, rep(T, 8), F, F)] <- ''
x_breaks <- seq(1970, 2015, 5)
x_labels <- x_breaks
x_labels[c(F, T)] <- ''
p <- ggplot(data, aes(year, rate)) + geom_point(aes(col = size)) +
scale_y_continuous(breaks = y_breaks,
labels = y_labels,
limits = c(0.01, 20),
trans = 'log10',
expand = c(0, 0)) +
scale_x_continuous(name = "Year of Diagnosis",
breaks = x_breaks,
labels = x_labels,
limits = c(1970, 2015),
expand = c(0, 0)) +
theme(panel.background = element_blank(),
axis.line = element_line(colour = "black"),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
# Move the tick mark labels a little to the left
axis.text.y = element_text(margin = margin(r = 5, unit = "pt"))
)
p
# Get the ggplot grob
g = ggplotGrob(p)
# Get the x axis
yaxis <- g$grobs[[which(g$layout$name == "axis-l")]]
# Get the tick marks and tick mark labels
ticks <- yaxis$children[[2]]
# Get the tick marks
marks = ticks$grobs[[2]]
# Edit the x positions of the end points of the tick marks
# The '5.5' and the '2.75' in the code below
# are the lengths in pts of the major and minor tick marks respectively.
marks$x = unit.c(rep(
unit.c(unit(1, "npc") - unit(5.5, "pt"), unit(1, "npc"), # 1st long tick mark
rep(unit.c(unit(1, "npc") - unit(2.75, "pt"), unit(1, "npc")), 8)), # 8 shorter tick marks
3), # repeat that sequence of tick marks 3 times
rep(unit.c(unit(1, "npc") - unit(5.5, "pt"), unit(1, "npc")), 2)) # top 2 long tick marks
# Put the tick marks back into the plot
ticks$grobs[[2]] = marks
yaxis$children[[2]] = ticks
g$grobs[[which(g$layout$name == "axis-l")]] = yaxis
# Draw the plot
grid.newpage()
grid.draw(g)
However, knowing what I now know, I would not recommend this method. I think it is too difficult finding the path through the sequence of lists within lists to the appropriate slots. I would recommend using grid editing tools for this edit.
# Get the ggplot grob
g= ggplotGrob(p)
# Get a list of the grobs that make up the plot
grid.ls(grid.force(g))
Look for a grob and its children that have something to do with the left axis.
This is the relevant sequence (the trailing numbers might differ):
axis-l.7-4-7-4
axis.line.y.left..polyline.106
axis
axis.1-1-1-1
GRID.text.102
axis.1-2-1-2
axis.1-2-1-2 is the tick marks grob; GRID.text.102 is the tick mark labels grob.
# Edit the x-coordinates of the tick marks
g1 = editGrob(grid.force(g), gPath("axis-l", "axis", "axis.1-2"), grep = TRUE,
x = unit.c(rep(
unit.c(unit(1, "npc") - unit(5.5, "pt"), unit(1, "npc"), # 1st long tick mark
rep(unit.c(unit(1, "npc") - unit(2.75, "pt"), unit(1, "npc")), 8)), # 8 shorter tick marks
3), # repeat that sequence of tick marks 3 times
rep(unit.c(unit(1, "npc") - unit(5.5, "pt"), unit(1, "npc")), 2))) # top 2 long tick marks
# Draw the edited plot
grid.newpage()
grid.draw(g1)
Your data
data = structure(list(size = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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, 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), .Label = c("(1,2]", "(2,4]", "<=1", ">4", "Unknown"
), class = "factor"), year = c(1983L, 1984L, 1985L, 1986L, 1987L,
1988L, 1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L,
1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L,
2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 1983L,
1984L, 1985L, 1986L, 1987L, 1988L, 1989L, 1990L, 1991L, 1992L,
1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L,
2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L,
2011L, 2012L, 2013L, 1983L, 1984L, 1985L, 1986L, 1987L, 1988L,
1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L,
1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L,
2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 1983L, 1984L,
1985L, 1986L, 1987L, 1988L, 1989L, 1990L, 1991L, 1992L, 1993L,
1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L,
2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L,
2012L, 2013L, 1983L, 1984L, 1985L, 1986L, 1987L, 1988L, 1989L,
1990L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L,
1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L,
2008L, 2009L, 2010L, 2011L, 2012L, 2013L), rate = c(0.53, 0.53,
0.63, 0.62, 0.6, 0.58, 0.63, 0.82, 0.79, 0.94, 0.88, 1.03, 1.17,
1.22, 1.32, 1.47, 1.71, 1.69, 2.11, 2.36, 2.61, 2.91, 3.48, 3.49,
4.26, 4.44, 4.92, 4.87, 5.09, 5.23, 5.39, 0.87, 0.92, 1.06, 1,
1.04, 1.08, 1.18, 1.13, 1.22, 1.16, 1.12, 1.17, 1.33, 1.29, 1.47,
1.57, 1.41, 1.68, 1.75, 2.12, 2.18, 2.31, 2.5, 2.78, 3.02, 3.18,
3.64, 3.43, 3.87, 3.78, 3.92, 0.8, 0.81, 0.87, 0.99, 0.87, 0.78,
0.98, 1.02, 1.04, 1.04, 1.04, 1, 1.02, 1.22, 1.2, 1.26, 1.34,
1.38, 1.49, 1.75, 1.82, 1.95, 1.8, 1.95, 2.07, 2.31, 2.58, 2.52,
2.62, 2.83, 2.7, 0.22, 0.24, 0.18, 0.25, 0.24, 0.17, 0.24, 0.19,
0.22, 0.23, 0.25, 0.34, 0.38, 0.38, 0.35, 0.36, 0.44, 0.4, 0.51,
0.58, 0.53, 0.55, 0.73, 0.69, 0.67, 0.72, 0.79, 0.95, 0.88, 0.93,
0.96, 0.74, 0.91, 0.85, 1.02, 0.81, 0.88, 0.82, 1.05, 0.88, 1.07,
0.97, 1.2, 1.08, 1.01, 1.07, 0.94, 0.98, 1.16, 1.02, 0.98, 0.96,
0.74, 0.66, 0.66, 0.64, 0.62, 0.61, 0.52, 0.55, 0.55, 0.47),
se = c(0.05, 0.05, 0.06, 0.06, 0.05, 0.05, 0.05, 0.06, 0.06,
0.06, 0.06, 0.07, 0.07, 0.07, 0.07, 0.08, 0.08, 0.08, 0.09,
0.09, 0.1, 0.1, 0.11, 0.11, 0.12, 0.12, 0.13, 0.13, 0.13,
0.13, 0.13, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07,
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.08, 0.08, 0.07, 0.08,
0.08, 0.09, 0.09, 0.09, 0.09, 0.1, 0.1, 0.11, 0.11, 0.11,
0.12, 0.11, 0.12, 0.06, 0.06, 0.06, 0.07, 0.06, 0.06, 0.07,
0.07, 0.07, 0.07, 0.07, 0.06, 0.06, 0.07, 0.07, 0.07, 0.07,
0.07, 0.07, 0.08, 0.08, 0.08, 0.08, 0.08, 0.09, 0.09, 0.1,
0.09, 0.09, 0.1, 0.1, 0.03, 0.03, 0.03, 0.04, 0.03, 0.03,
0.03, 0.03, 0.03, 0.03, 0.03, 0.04, 0.04, 0.04, 0.04, 0.04,
0.04, 0.04, 0.04, 0.05, 0.04, 0.04, 0.05, 0.05, 0.05, 0.05,
0.05, 0.06, 0.05, 0.06, 0.06, 0.06, 0.07, 0.07, 0.07, 0.06,
0.06, 0.06, 0.07, 0.06, 0.07, 0.06, 0.07, 0.07, 0.06, 0.06,
0.06, 0.06, 0.07, 0.06, 0.06, 0.06, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.04, 0.04, 0.04, 0.04), lci = c(0.43, 0.44,
0.52, 0.51, 0.5, 0.48, 0.53, 0.7, 0.68, 0.82, 0.77, 0.91,
1.04, 1.09, 1.18, 1.32, 1.56, 1.53, 1.94, 2.18, 2.43, 2.71,
3.27, 3.28, 4.02, 4.2, 4.67, 4.62, 4.84, 4.97, 5.13, 0.75,
0.8, 0.93, 0.87, 0.91, 0.95, 1.04, 0.99, 1.08, 1.03, 0.99,
1.04, 1.19, 1.15, 1.32, 1.43, 1.27, 1.53, 1.6, 1.96, 2.01,
2.13, 2.32, 2.59, 2.82, 2.97, 3.42, 3.22, 3.64, 3.56, 3.7,
0.68, 0.69, 0.75, 0.86, 0.75, 0.67, 0.86, 0.89, 0.91, 0.92,
0.92, 0.88, 0.9, 1.09, 1.07, 1.13, 1.21, 1.24, 1.35, 1.59,
1.67, 1.79, 1.64, 1.79, 1.91, 2.13, 2.4, 2.34, 2.44, 2.65,
2.51, 0.16, 0.17, 0.12, 0.18, 0.18, 0.12, 0.18, 0.14, 0.16,
0.17, 0.19, 0.27, 0.31, 0.31, 0.28, 0.29, 0.36, 0.33, 0.42,
0.5, 0.45, 0.47, 0.63, 0.6, 0.58, 0.63, 0.69, 0.84, 0.78,
0.83, 0.85, 0.63, 0.79, 0.72, 0.88, 0.7, 0.76, 0.71, 0.92,
0.76, 0.94, 0.85, 1.06, 0.96, 0.89, 0.95, 0.82, 0.86, 1.04,
0.9, 0.86, 0.85, 0.65, 0.57, 0.57, 0.55, 0.54, 0.53, 0.44,
0.46, 0.47, 0.4), uci = c(0.64, 0.65, 0.74, 0.74, 0.71, 0.69,
0.74, 0.95, 0.92, 1.07, 1.01, 1.17, 1.32, 1.37, 1.46, 1.62,
1.88, 1.85, 2.29, 2.55, 2.81, 3.12, 3.71, 3.71, 4.5, 4.69,
5.18, 5.13, 5.36, 5.5, 5.66, 1.01, 1.06, 1.21, 1.15, 1.19,
1.23, 1.33, 1.27, 1.37, 1.31, 1.27, 1.32, 1.48, 1.44, 1.62,
1.73, 1.56, 1.84, 1.92, 2.31, 2.36, 2.49, 2.69, 2.99, 3.23,
3.39, 3.86, 3.65, 4.1, 4, 4.15, 0.93, 0.94, 1, 1.14, 1.01,
0.91, 1.12, 1.16, 1.18, 1.18, 1.18, 1.14, 1.15, 1.37, 1.35,
1.41, 1.49, 1.53, 1.65, 1.91, 1.99, 2.13, 1.96, 2.12, 2.25,
2.49, 2.78, 2.71, 2.82, 3.03, 2.89, 0.3, 0.31, 0.24, 0.33,
0.32, 0.23, 0.32, 0.25, 0.28, 0.3, 0.32, 0.42, 0.47, 0.47,
0.43, 0.44, 0.53, 0.49, 0.6, 0.68, 0.63, 0.65, 0.84, 0.8,
0.78, 0.83, 0.9, 1.07, 1, 1.05, 1.08, 0.88, 1.06, 0.98, 1.16,
0.95, 1.01, 0.95, 1.19, 1.01, 1.21, 1.11, 1.34, 1.22, 1.14,
1.21, 1.06, 1.11, 1.3, 1.15, 1.1, 1.08, 0.85, 0.76, 0.76,
0.74, 0.72, 0.71, 0.61, 0.64, 0.64, 0.55), count = structure(c(15L,
16L, 21L, 20L, 19L, 18L, 22L, 35L, 36L, 53L, 48L, 63L, 79L,
83L, 88L, 96L, 101L, 100L, 112L, 118L, 121L, 126L, 136L,
1L, 7L, 8L, 10L, 9L, 11L, 12L, 13L, 38L, 44L, 56L, 51L, 55L,
60L, 75L, 68L, 78L, 76L, 73L, 78L, 89L, 86L, 95L, 98L, 93L,
101L, 102L, 113L, 115L, 117L, 120L, 125L, 130L, 132L, 3L,
2L, 4L, 5L, 6L, 30L, 32L, 42L, 49L, 40L, 34L, 52L, 57L, 61L,
65L, 67L, 62L, 67L, 85L, 84L, 87L, 90L, 92L, 97L, 103L, 105L,
108L, 105L, 110L, 114L, 119L, 123L, 122L, 124L, 129L, 127L,
104L, 106L, 91L, 106L, 111L, 94L, 107L, 99L, 106L, 109L,
116L, 128L, 134L, 135L, 131L, 133L, 17L, 14L, 21L, 27L, 23L,
25L, 43L, 41L, 39L, 47L, 54L, 77L, 71L, 77L, 81L, 26L, 40L,
31L, 50L, 31L, 41L, 37L, 58L, 45L, 65L, 57L, 80L, 72L, 64L,
73L, 59L, 66L, 82L, 74L, 70L, 69L, 46L, 34L, 35L, 36L, 33L,
35L, 26L, 28L, 29L, 24L), .Label = c("1,010", "1,022", "1,064",
"1,147", "1,157", "1,204", "1,234", "1,303", "1,462", "1,465",
"1,570", "1,612", "1,684", "107", "110", "114", "115", "130",
"131", "132", "135", "140", "145", "149", "153", "155", "158",
"167", "173", "177", "178", "179", "183", "184", "185", "186",
"187", "190", "191", "195", "196", "198", "201", "202", "205",
"206", "210", "214", "216", "218", "224", "226", "227", "230",
"234", "235", "236", "238", "244", "245", "250", "251", "253",
"255", "256", "258", "259", "260", "264", "266", "267", "268",
"274", "275", "280", "282", "283", "291", "293", "294", "295",
"308", "310", "313", "316", "334", "335", "337", "342", "356",
"37", "372", "375", "38", "381", "383", "406", "418", "44",
"451", "453", "478", "479", "48", "500", "51", "54", "542",
"55", "557", "56", "573", "580", "593", "598", "60", "636",
"643", "668", "703", "720", "741", "750", "785", "789", "813",
"816", "84", "857", "869", "90", "919", "92", "95", "98",
"984"), class = "factor"), pop = structure(c(1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 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, 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
), .Label = c("21,998,396", "22,197,735", "22,423,982", "22,644,373",
"22,872,669", "23,111,066", "23,349,445", "23,657,474", "23,998,620",
"24,368,037", "24,713,120", "25,022,087", "25,339,972", "25,652,964",
"25,969,420", "26,263,552", "26,520,657", "26,787,544", "27,018,187",
"27,165,850", "27,298,693", "27,458,357", "27,662,860", "27,954,662",
"28,212,877", "28,496,587", "28,777,105", "29,014,912", "29,276,092",
"29,546,129", "29,806,864"), class = "factor")), class = "data.frame", row.names = c(NA,
-155L))
I have the following data:
df <- structure(list(TPR = c(0.02, 0.04, 0.06, 0.08, 0.1, 0.12, 0.14,
0.16, 0.18, 0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 0.32, 0.34, 0.36,
0.38, 0.4, 0.42, 0.44, 0.46, 0.48, 0.5, 0.52, 0.54, 0.56, 0.58,
0.6, 0.62, 0.64, 0.64, 0.64, 0.66, 0.68, 0.7, 0.72, 0.74, 0.76,
0.78, 0.8, 0.8, 0.82, 0.82, 0.84, 0.84, 0.84, 0.86, 0.86, 0.86,
0.86, 0.88, 0.88, 0.9, 0.92, 0.92, 0.92, 0.92, 0.94, 0.94, 0.96,
0.96, 0.96, 0.96, 0.96, 0.96, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98,
0.98, 0.98, 0.98, 0.98, 0.98, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.02, 0.04, 0.06, 0.08, 0.1,
0.12, 0.14, 0.16, 0.18, 0.2, 0.22, 0.24, 0.24, 0.26, 0.28, 0.3,
0.32, 0.34, 0.36, 0.38, 0.4, 0.42, 0.42, 0.42, 0.44, 0.46, 0.48,
0.5, 0.52, 0.54, 0.56, 0.58, 0.6, 0.6, 0.6, 0.6, 0.62, 0.62,
0.62, 0.64, 0.66, 0.66, 0.68, 0.68, 0.68, 0.7, 0.72, 0.74, 0.76,
0.78, 0.8, 0.8, 0.8, 0.82, 0.82, 0.84, 0.84, 0.84, 0.86, 0.86,
0.86, 0.86, 0.86, 0.88, 0.88, 0.88, 0.9, 0.9, 0.9, 0.9, 0.9,
0.9, 0.9, 0.92, 0.94, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96,
0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0.02, 0.04, 0.06, 0.08, 0.1, 0.1, 0.1, 0.12,
0.14, 0.16, 0.18, 0.2, 0.22, 0.24, 0.24, 0.26, 0.28, 0.28, 0.3,
0.32, 0.34, 0.36, 0.38, 0.4, 0.42, 0.42, 0.42, 0.42, 0.44, 0.44,
0.44, 0.46, 0.48, 0.48, 0.5, 0.52, 0.54, 0.56, 0.58, 0.58, 0.6,
0.62, 0.62, 0.62, 0.64, 0.66, 0.68, 0.68, 0.7, 0.72, 0.72, 0.72,
0.72, 0.74, 0.74, 0.74, 0.76, 0.76, 0.78, 0.78, 0.8, 0.82, 0.84,
0.84, 0.84, 0.86, 0.88, 0.88, 0.9, 0.9, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.94, 0.94, 0.96, 0.96, 0.96, 0.96, 0.98,
0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98,
1, 1, 1, 1, 0.02, 0.04, 0.06, 0.06, 0.06, 0.08, 0.08, 0.1, 0.12,
0.14, 0.16, 0.16, 0.18, 0.2, 0.22, 0.24, 0.26, 0.28, 0.28, 0.3,
0.32, 0.32, 0.34, 0.34, 0.36, 0.38, 0.4, 0.42, 0.42, 0.44, 0.46,
0.46, 0.46, 0.48, 0.48, 0.5, 0.52, 0.54, 0.56, 0.56, 0.58, 0.6,
0.62, 0.64, 0.64, 0.64, 0.64, 0.64, 0.66, 0.68, 0.68, 0.7, 0.7,
0.7, 0.7, 0.7, 0.72, 0.74, 0.76, 0.76, 0.78, 0.78, 0.78, 0.8,
0.8, 0.82, 0.82, 0.84, 0.86, 0.86, 0.86, 0.86, 0.88, 0.9, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.94, 0.94, 0.94,
0.94, 0.94, 0.94, 0.96, 0.98, 0.98, 0.98, 0.98, 1, 1, 1, 1, 1,
1), FPR = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.02, 0.04, 0.04,
0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.06, 0.06, 0.08, 0.08,
0.1, 0.12, 0.12, 0.14, 0.16, 0.18, 0.18, 0.2, 0.2, 0.2, 0.22,
0.24, 0.26, 0.26, 0.28, 0.28, 0.3, 0.32, 0.34, 0.36, 0.38, 0.38,
0.4, 0.42, 0.44, 0.46, 0.48, 0.5, 0.52, 0.54, 0.56, 0.58, 0.58,
0.6, 0.62, 0.64, 0.66, 0.68, 0.7, 0.72, 0.74, 0.76, 0.78, 0.8,
0.82, 0.84, 0.86, 0.88, 0.9, 0.92, 0.94, 0.96, 0.98, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02,
0.02, 0.02, 0.02, 0.02, 0.04, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.08, 0.1, 0.12, 0.12, 0.14, 0.16, 0.16,
0.16, 0.18, 0.18, 0.2, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22,
0.24, 0.26, 0.26, 0.28, 0.28, 0.3, 0.32, 0.32, 0.34, 0.36, 0.38,
0.4, 0.4, 0.42, 0.44, 0.44, 0.46, 0.48, 0.5, 0.52, 0.54, 0.56,
0.56, 0.56, 0.56, 0.58, 0.6, 0.62, 0.64, 0.66, 0.68, 0.68, 0.7,
0.72, 0.74, 0.76, 0.78, 0.8, 0.82, 0.84, 0.84, 0.86, 0.88, 0.9,
0.92, 0.94, 0.96, 0.98, 1, 0, 0, 0, 0, 0, 0.02, 0.04, 0.04, 0.04,
0.04, 0.04, 0.04, 0.04, 0.04, 0.06, 0.06, 0.06, 0.08, 0.08, 0.08,
0.08, 0.08, 0.08, 0.08, 0.08, 0.1, 0.12, 0.14, 0.14, 0.16, 0.18,
0.18, 0.18, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.22, 0.22, 0.22, 0.24,
0.26, 0.26, 0.26, 0.26, 0.28, 0.28, 0.28, 0.3, 0.32, 0.34, 0.34,
0.36, 0.38, 0.38, 0.4, 0.4, 0.42, 0.42, 0.42, 0.42, 0.44, 0.46,
0.46, 0.46, 0.48, 0.48, 0.5, 0.5, 0.52, 0.54, 0.56, 0.58, 0.6,
0.62, 0.64, 0.64, 0.66, 0.66, 0.68, 0.7, 0.72, 0.72, 0.74, 0.76,
0.78, 0.8, 0.82, 0.84, 0.86, 0.88, 0.9, 0.92, 0.94, 0.94, 0.96,
0.98, 1, 0, 0, 0, 0.02, 0.04, 0.04, 0.06, 0.06, 0.06, 0.06, 0.06,
0.08, 0.08, 0.08, 0.08, 0.08, 0.08, 0.08, 0.1, 0.1, 0.1, 0.12,
0.12, 0.14, 0.14, 0.14, 0.14, 0.14, 0.16, 0.16, 0.16, 0.18, 0.2,
0.2, 0.22, 0.22, 0.22, 0.22, 0.22, 0.24, 0.24, 0.24, 0.24, 0.24,
0.26, 0.28, 0.3, 0.32, 0.32, 0.32, 0.34, 0.34, 0.36, 0.38, 0.4,
0.42, 0.42, 0.42, 0.42, 0.44, 0.44, 0.46, 0.48, 0.48, 0.5, 0.5,
0.52, 0.52, 0.52, 0.54, 0.56, 0.58, 0.58, 0.58, 0.58, 0.6, 0.62,
0.64, 0.66, 0.68, 0.7, 0.72, 0.74, 0.74, 0.76, 0.78, 0.8, 0.82,
0.84, 0.84, 0.84, 0.86, 0.88, 0.9, 0.9, 0.92, 0.94, 0.96, 0.98,
1), GeneSet = 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, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 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), .Label = c("Distort = 1", "Distort = 1.5",
"Distort = 2", "Distort = 2.5"), class = "factor")), .Names = c("TPR",
"FPR", "GeneSet"), row.names = c(NA, -400L), class = "data.frame")
But why the following code fail to create the desired plot?
library(ggplot2)
library(RColorBrewer)
p <- qplot(FPR, TPR, data = df, geom = "blank", main = "ROC curve", xlab = "False Positive Rate (1-Specificity)", ylab = "True Positive Rate (Sensitivity)" )
p <- p + geom_line(aes(x = FPR, y = TPR, data = data, colour = GeneSet), size = 2, alpha = 0.7) + scale_colour_manual(values=colors)
p
I got this error message:
Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous
Error: Aesthetics must either be length one, or the same length as the dataProblems:data
The desired plot is this:
You don't need to plot geom="blank" and geom_line() - it can be done just by geom_line(). Only colors can't be reproduced because variable colors isn't provided in question.
ggplot(df,aes(FPR,TPR,color=GeneSet))+geom_line(size = 2, alpha = 0.7)+
labs(title= "ROC curve",
x = "False Positive Rate (1-Specificity)",
y = "True Positive Rate (Sensitivity)")