Related
I have data that i want to show in barplot in a way that i could'nt figure out how. Hope you can help me with this!
My table consists of 4 columns: cluster (0:6), IgG_Status (mild_high, mild_low,Severe_High), patient (1-16) and value (normalized value per each cluster). These are the lines of code i'm using now to create a barplot of the sum of values for each cluster, divided to IgG_Status (dodged style).
ggplot(mat, aes(x= cluster, fill= IgG_status, group=IgG_status)) + geom_bar(aes(weight = normalizedppstatus), position = "dodge")
I want to add to this graph horizontal lines that describe each patient contribution to the each bar. i managed to do it using facet_grid but it changed the whole style of the figure so it's not good for me. code for using facet_grid below:
ggplot(mat, aes(x= IgG_status , y= normalizedppstatus, fill= IgG_status)) + geom_col(color = "black") + facet_grid(~cluster) +
theme(axis.text.x = element_text(angle=30))
mat structure:
structure(list(cluster = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("0", "1", "2", "3", "4"), class = "factor"), IgG_status = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("mild_high", "mild_low", "Severe_High" ), class = "factor"), patient = structure(c(5L, 11L, 16L, 4L, 6L, 7L, 8L, 12L, 2L, 3L, 9L, 10L, 13L, 14L, 17L, 5L, 11L, 16L, 4L, 6L, 7L, 8L, 12L, 2L, 3L, 9L, 10L, 13L, 14L, 17L, 5L, 11L, 16L, 4L, 6L, 7L, 8L, 12L, 2L, 3L, 9L, 10L, 13L, 14L, 17L, 5L, 11L, 16L, 4L, 6L, 7L, 8L, 12L, 2L, 3L, 9L, 10L, 13L, 14L, 17L, 5L, 16L, 4L, 6L, 7L, 8L, 2L, 3L, 9L, 13L, 14L, 17L), .Label = c("Contact3", "CoV1", "CoV2", "CoV3", "CoV4", "CoV5", "CoV6", "CoV7", "CoV8", "CoV9", "CoV10", "CoV11", "CoV12", "CoV13", "CoV14", "CoV15", "CoV16"), class = "factor"), Freq = c(176L, 164L, 345L, 505L, 277L, 1421L, 679L, 104L, 235L, 933L, 692L, 682L, 133L, 1278L, 330L, 420L, 166L, 288L, 231L, 701L, 1105L, 431L, 506L, 180L, 814L, 410L, 363L, 283L, 182L, 268L, 657L, 155L, 82L, 872L, 385L, 277L, 23L, 298L, 87L, 128L, 469L, 640L, 197L, 148L, 73L, 688L, 220L, 51L, 263L, 456L, 312L, 693L, 303L, 120L, 400L, 373L, 35L, 62L, 170L, 166L, 7L, 530L, 5L, 1L, 80L, 876L, 19L, 7L, 2L, 4L, 15L, 153L), percentperstatus = c(0.0445682451253482, 0.041529501139529, 0.0873638895923018, 0.0467419474268789, 0.0256386523509811, 0.131525360977416, 0.0628470936690115, 0.00962606442058497, 0.0233807581335191, 0.0928265844194607, 0.0688488707591284, 0.0678539448811064, 0.0132325141776938, 0.127151527211223, 0.0328325539747289, 0.106356039503672, 0.0420359584704989, 0.0729298556596607, 0.021380970011107, 0.0648833765272121, 0.102276934468715, 0.0398926323583858, 0.0468345057386153, 0.0179086658043976, 0.0809869664709979, 0.0407919609989056, 0.0361158093722018, 0.0281564023480251, 0.018107650980002, 0.0266640135309919, 0.166371233223601, 0.0392504431501646, 0.0207647505697645, 0.0807108478341355, 0.0356349500185117, 0.0256386523509811, 0.00212884116993706, 0.0275823768974454, 0.00865585513879216, 0.0127350512386827, 0.0466620236792359, 0.0636752561934136, 0.0196000397970351, 0.0147249029947269, 0.00726295890956124, 0.174221321853634, 0.0557103064066852, 0.0129146619397316, 0.0243428359866716, 0.0422065901517956, 0.0288781932617549, 0.064142910033321, 0.0280451684561274, 0.011939110536265, 0.0397970351208835, 0.0371107352502239, 0.00348224057307731, 0.00616854044373694, 0.0169137399263755, 0.0165157695751667, 0.00177260065839453, 0.134211192707014, 0.00046279155868197, 9.25583117363939e-05, 0.00740466493891151, 0.0810810810810811, 0.00189035916824197, 0.000696448114615461, 0.000198985175604417, 0.000397970351208835, 0.00149238881703313, 0.0152223659337379), normalizedppstatus = c(0.0508788793021933, 0.0474098648043165, 0.0997341668139585, 0.0533603666644943, 0.0292689535961681, 0.150148675307419, 0.0717459187429537, 0.0109890656101137, 0.0266913531758627, 0.105970351119489, 0.0785975165859447, 0.077461714323142, 0.0151061700952755, 0.145155529186181, 0.0374814746724881, 0.142698157688979, 0.0563997480389775, 0.0978501652724429, 0.0286868996290798, 0.0870541845886795, 0.137225212511399, 0.0535240421650797, 0.0628379706160796, 0.0240281006032283, 0.10866041050571, 0.0547306735962423, 0.0484566695498438, 0.0377775137261868, 0.0242950794988198, 0.0357751720092511, 0.291165142146192, 0.0686919285124198, 0.0363402460517317, 0.141251495387918, 0.0623644790416841, 0.0448700277780429, 0.00372567017651619, 0.048271726634862, 0.0151485520846987, 0.0222875249062233, 0.0816628842267089, 0.111437624531117, 0.0343018938009843, 0.0257699506728207, 0.0127108540480805, 0.309787838022592, 0.0990600644839683, 0.0229639240394654, 0.0432846821009943, 0.0750487263804311, 0.0513491285760844, 0.114054314433418, 0.0498679037133128, 0.0212292686198854, 0.0707642287329512, 0.065987643293477, 0.00619187001413323, 0.0109684554536074, 0.0300747972115043, 0.0293671549241748, 0.00723736731183195, 0.547972096467276, 0.00188953585407682, 0.000377907170815364, 0.0302325736652291, 0.331046681634259, 0.00771816460016861, 0.00284353432637791, 0.000812438378965117, 0.00162487675793023, 0.00609328784223838, 0.0621515359908314)), row.names = c(5L, 11L, 16L, 21L, 23L, 24L, 25L, 29L, 36L, 37L, 43L, 44L, 47L, 48L, 51L, 56L, 62L, 67L, 72L, 74L, 75L, 76L, 80L, 87L, 88L, 94L, 95L, 98L, 99L, 102L, 107L, 113L, 118L, 123L, 125L, 126L, 127L, 131L, 138L, 139L, 145L, 146L, 149L, 150L, 153L, 158L, 164L, 169L, 174L, 176L, 177L, 178L, 182L, 189L, 190L, 196L, 197L, 200L, 201L, 204L, 209L, 220L, 225L, 227L, 228L, 229L, 240L, 241L, 247L, 251L, 252L, 255L), class = "data.frame")
Thanks!
library(tidyverse)
ggplot(mat, aes(x= cluster, fill= IgG_status, group=IgG_status)) +
geom_col(aes(y = normalizedppstatus), color = "white", position = "dodge")
By using geom_col, ggplot plots each patient as a bar, rather than calculating one grouped total to display with geom_bar. Adding a white border with the color aesthetic seems like the simplest way to separate the patients visually.
In the past, I have used asreml-r to account for spatial auto-correlation in agricultural field trials that were laid out in a “row and range” design. It is relatively easy to use the asreml package to specify a spatial model (i.e. rcov=~at(LOCATION):ar1(ROW):ar1(RANGE))
Unfortunately, asreml-r is expensive and difficult to learn. My research group also prefers to rely on nlme and lmer for the majority of it’s analytical needs. So they are reluctant to either pay for asreml-r or consider using.
Several years ago a question was posted asking if an open-source alternative to asreml-r was available that could be used to construct a two-dimensional spatial model with error structure in both direction. The consensus at the time seemed to be that it wasn’t straight forward to do this in either lmer or nlme.
After spending a few hours searching, it’s not totally clear to me whether there has been any progress on addressing this. Can anyone refer me to a recent discussion regarding this type of analysis? Or can they offer advice on how to construct a mixed effects models that accounts for spatial correlation in nlme or lmer?
Please note that neither myself nor other members of our group are exactly statisticians or high-level r coders. It is also not practical to contract an outside group to analyze our data. We just want to apply the best methods we can to routine annual analyses of data.
An example of the data being analyzed:
my.data <- structure(list(ENTRY = structure(c(23L, 23L, 23L, 40L, 12L, 8L,
1L, 15L, 30L, 1L, 24L, 8L, 1L, 8L, 30L, 33L, 12L, 38L, 41L, 36L,
43L, 32L, 44L, 31L, 26L, 11L, 13L, 34L, 5L, 22L, 4L, 14L, 11L,
20L, 25L, 11L, 21L, 43L, 44L, 4L, 42L, 45L, 42L, 41L, 42L, 4L,
44L, 20L, 40L, 29L, 29L, 24L, 2L, 3L, 28L, 24L, 34L, 27L, 41L,
28L, 29L, 5L, 3L, 25L, 14L, 20L, 15L, 21L, 31L, 22L, 40L, 21L,
6L, 38L, 43L, 12L, 6L, 14L, 5L, 3L, 30L, 45L, 31L, 7L, 9L, 39L,
22L, 15L, 26L, 28L, 34L, 10L, 25L, 27L, 16L, 45L, 10L, 18L, 32L,
10L, 6L, 18L, 33L, 16L, 37L, 9L, 32L, 38L, 39L, 2L, 2L, 39L,
36L, 36L, 7L, 27L, 7L, 26L, 17L, 9L, 33L, 13L, 17L, 17L, 35L,
37L, 37L, 18L, 16L, 19L, 13L, 19L, 35L, 19L, 35L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L,
44L, 45L, 52L, 54L, 52L, 54L, 49L, 51L, 50L, 54L, 49L, 46L, 51L,
50L, 53L, 49L, 50L, 51L, 53L, 52L, 53L, 48L, 47L, 46L, 46L, 47L,
48L, 48L, 47L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L), .Label = c("20",
"112", "1478", "1495", "1521", "1522", "1590", "1608", "1657",
"1658", "1660", "1667", "1680", "1688", "1723", "1728", "1730",
"1731", "1743", "1745", "1748", "1751", "1766", "1778", "1802",
"1815", "1817", "1819", "1828", "1830", "1831", "1834", "1835",
"1836", "1837", "1838", "1839", "1840", "1841", "1842", "1843",
"1844", "1845", "1846", "1847", "3097", "3164", "3168", "3169",
"3170", "3178", "3180", "3181", "3182"), class = "factor"), BLOCK = structure(c(12L,
77L, 163L, 67L, 28L, 170L, 90L, 36L, 52L, 2L, 15L, 19L, 168L,
103L, 188L, 31L, 203L, 66L, 29L, 46L, 34L, 32L, 27L, 16L, 83L,
48L, 82L, 30L, 171L, 14L, 115L, 54L, 93L, 65L, 50L, 187L, 58L,
91L, 200L, 6L, 169L, 135L, 99L, 148L, 101L, 104L, 107L, 128L,
153L, 146L, 41L, 22L, 53L, 87L, 131L, 151L, 110L, 10L, 44L, 11L,
13L, 20L, 42L, 202L, 111L, 38L, 183L, 51L, 199L, 109L, 75L, 134L,
92L, 166L, 182L, 97L, 100L, 1L, 86L, 181L, 25L, 108L, 94L, 116L,
72L, 18L, 23L, 76L, 185L, 81L, 62L, 63L, 56L, 204L, 85L, 95L,
129L, 49L, 147L, 106L, 145L, 205L, 73L, 207L, 105L, 24L, 43L,
8L, 167L, 164L, 3L, 96L, 184L, 45L, 74L, 39L, 89L, 4L, 152L,
130L, 165L, 40L, 57L, 70L, 206L, 186L, 7L, 37L, 9L, 102L, 132L,
127L, 88L, 80L, 98L, 139L, 196L, 174L, 118L, 215L, 194L, 193L,
208L, 172L, 122L, 143L, 141L, 123L, 161L, 209L, 213L, 178L, 159L,
160L, 191L, 177L, 192L, 144L, 175L, 211L, 140L, 180L, 173L, 125L,
119L, 120L, 210L, 214L, 136L, 154L, 162L, 190L, 158L, 216L, 142L,
124L, 212L, 195L, 155L, 121L, 64L, 68L, 117L, 59L, 71L, 35L,
69L, 201L, 21L, 84L, 61L, 114L, 17L, 112L, 55L, 150L, 113L, 79L,
78L, 47L, 33L, 149L, 60L, 189L, 5L, 133L, 26L, 137L, 197L, 179L,
126L, 198L, 157L, 176L, 138L, 156L), .Label = c("101", "102",
"103", "104", "105", "106", "107", "108", "109", "110", "111",
"112", "113", "114", "115", "116", "117", "118", "201", "202",
"203", "204", "205", "206", "207", "208", "209", "210", "211",
"212", "213", "214", "215", "216", "217", "218", "301", "302",
"303", "304", "305", "306", "307", "308", "309", "310", "311",
"312", "313", "314", "315", "316", "317", "318", "401", "402",
"403", "404", "405", "406", "407", "408", "409", "410", "411",
"412", "413", "414", "415", "416", "417", "418", "501", "502",
"503", "504", "505", "506", "507", "508", "509", "510", "511",
"512", "513", "514", "515", "516", "517", "518", "601", "602",
"603", "604", "605", "606", "607", "608", "609", "610", "611",
"612", "613", "614", "615", "616", "617", "618", "701", "702",
"703", "704", "705", "706", "707", "708", "709", "710", "711",
"712", "713", "714", "715", "716", "717", "718", "801", "802",
"803", "804", "805", "806", "807", "808", "809", "810", "811",
"812", "813", "814", "815", "816", "817", "818", "901", "902",
"903", "904", "905", "906", "907", "908", "909", "910", "911",
"912", "913", "914", "915", "916", "917", "918", "1001", "1002",
"1003", "1004", "1005", "1006", "1007", "1008", "1009", "1010",
"1011", "1012", "1013", "1014", "1015", "1016", "1017", "1018",
"1101", "1102", "1103", "1104", "1105", "1106", "1107", "1108",
"1109", "1110", "1111", "1112", "1113", "1114", "1115", "1116",
"1117", "1118", "1201", "1202", "1203", "1204", "1205", "1206",
"1207", "1208", "1209", "1210", "1211", "1212", "1213", "1214",
"1215", "1216", "1217", "1218"), class = "factor"), PLOT = structure(c(3L,
1L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 1L, 3L, 1L, 2L, 3L, 2L, 3L, 2L,
3L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 2L, 3L, 1L,
3L, 3L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 3L, 3L, 3L, 2L, 2L,
2L, 1L, 1L, 3L, 3L, 2L, 2L, 2L, 3L, 1L, 3L, 3L, 1L, 1L, 2L, 2L,
1L, 2L, 3L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 3L, 1L, 3L, 2L, 1L,
3L, 1L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 5L, 3L, 5L, 1L, 9L, 8L, 10L,
3L, 4L, 4L, 12L, 11L, 1L, 3L, 1L, 6L, 8L, 8L, 5L, 5L, 6L,
8L, 11L, 10L, 7L, 12L, 11L, 11L, 12L, 10L, 7L, 8L, 8L, 7L,
9L, 12L, 12L, 10L, 9L, 9L, 11L, 10L, 11L, 8L, 10L, 12L, 8L,
10L, 10L, 7L, 7L, 7L, 12L, 12L, 8L, 9L, 9L, 11L, 9L, 12L,
8L, 7L, 12L, 11L, 9L, 7L, 4L, 4L, 7L, 4L, 4L, 2L, 4L, 12L,
2L, 5L, 4L, 7L, 1L, 7L, 4L, 9L, 7L, 5L, 5L, 3L, 2L, 9L, 4L,
11L, 1L, 8L, 2L, 8L, 11L, 10L, 7L, 11L, 9L, 10L, 8L, 9L), .Label = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"), class = "factor"),
ROW = structure(c(12L, 5L, 1L, 13L, 10L, 8L, 18L, 18L, 16L,
2L, 15L, 1L, 6L, 13L, 8L, 13L, 5L, 12L, 11L, 10L, 16L, 14L,
9L, 16L, 11L, 12L, 10L, 12L, 9L, 14L, 7L, 18L, 3L, 11L, 14L,
7L, 4L, 1L, 2L, 6L, 7L, 9L, 9L, 4L, 11L, 14L, 17L, 2L, 9L,
2L, 5L, 4L, 17L, 15L, 5L, 7L, 2L, 10L, 8L, 11L, 13L, 2L,
6L, 4L, 3L, 2L, 3L, 15L, 1L, 1L, 3L, 8L, 2L, 4L, 2L, 7L,
10L, 1L, 14L, 1L, 7L, 18L, 4L, 8L, 18L, 18L, 5L, 4L, 5L,
9L, 8L, 9L, 2L, 6L, 13L, 5L, 3L, 13L, 3L, 16L, 1L, 7L, 1L,
9L, 15L, 6L, 7L, 8L, 5L, 2L, 3L, 6L, 4L, 9L, 2L, 3L, 17L,
4L, 8L, 4L, 3L, 4L, 3L, 16L, 8L, 6L, 7L, 1L, 9L, 12L, 6L,
1L, 16L, 8L, 8L, 13L, 16L, 12L, 10L, 17L, 14L, 13L, 10L,
10L, 14L, 17L, 15L, 15L, 17L, 11L, 15L, 16L, 15L, 16L, 11L,
15L, 12L, 18L, 13L, 13L, 14L, 18L, 11L, 17L, 11L, 12L, 12L,
16L, 10L, 10L, 18L, 10L, 14L, 18L, 16L, 16L, 14L, 15L, 11L,
13L, 10L, 14L, 9L, 5L, 17L, 17L, 15L, 3L, 3L, 12L, 7L, 6L,
17L, 4L, 1L, 6L, 5L, 7L, 6L, 11L, 15L, 5L, 6L, 9L, 5L, 7L,
8L, 11L, 17L, 17L, 18L, 18L, 13L, 14L, 12L, 12L), .Label = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
"13", "14", "15", "16", "17", "18"), class = "factor"), YIELD = c(7882.814724,
7641.976671, 7535.187491, 8462.821158, 6470.762695, 7086.39647,
7260.626003, 8374.363239, 8225.545799, 6870.562479, 7260.303179,
6472.786879, 6535.801894, 7335.468082, 8101.853381, 7544.810974,
5597.940891, 8144.903193, 8489.541356, 7420.247609, 8267.229308,
7388.809243, 8753.922873, 7675.2452, 7540.083649, 7459.719121,
7614.590404, 6910.577593, 7655.161236, 8086.00529, 6754.554032,
9141.060314, 7728.70075, 7210.881432, 8872.660416, 7341.942246,
8211.265337, 9030.218757, 8957.01212, 7134.079145, 8580.60533,
8901.807114, 9009.635596, 8972.04225, 8850.07798, 7244.08863,
9357.355395, 7693.962907, 9059.604638, 8115.135788, 8073.220877,
7694.865425, 7168.389384, 7931.776306, 8310.054831, 7743.358631,
7241.417998, 7887.710882, 8671.335868, 7900.074562, 7089.929401,
8252.964285, 8038.601576, 8749.99335, 7880.418003, 7227.593551,
9733.562528, 7715.095262, 6926.775409, 7770.203085, 9000.211927,
7808.710708, 8239.82626, 8252.964285, 9546.314331, 2801.654022,
7865.302917, 6472.037973, 11286.93314, 7698.702989, 8239.164252,
8391.871173, 7817.085477, 7987.7324, 8517.420004, 8286.027753,
8021.268999, 8605.836444, 8360.390812, 8408.648702, 6980.52271,
8484.391646, 7604.489488, 8047.32564, 6859.736888, 8211.744547,
8338.224508, 7549.875965, 7831.170315, 8002.372075, 8092.398475,
7233.303386, 7880.198456, 6431.676768, 8146.454012, 9012.217125,
7696.760712, 7916.314754, 8372.430545, 4552.305881, 4744.119616,
8072.706265, 8038.601576, 8070.612573, 7631.800415, 8124.412039,
7958.686488, 8565.578204, 7204.2532, 7782.851494, 8195.743097,
8075.444598, 7468.681342, 7376.4572, 7019.132415, 7450.186973,
7900.853201, 7077.396698, 6781.366002, 8195.304822, 7581.211378,
8155.600681, 7446.611537, 7887.710882, 6849.690117, 6384.206298,
6965.647058, 7732.576444, 7687.296996, 7887.710882, 8061.034883,
7861.831189, 6690.298381, 7982.777954, 8310.054831, 7476.530867,
5840.137517, 8012.816166, 9211.484507, 8906.076566, 7227.155276,
6795.608201, 6926.023806, 8026.998142, 7388.809243, 7700.812705,
7493.134187, 7397.470718, 6794.411986, 8475.249868, 8387.892097,
8503.435859, 7890.106874, 7631.800415, 8349.757061, 7852.912013,
7758.848165, 7580.919692, 6402.21648, 6920.804051, 8628.194894,
7489.137138, 7866.037678, 7311.596266, 8746.497033, 9147.374207,
9022.033508, 8475.348448, 8911.007949, 8961.95446, 8476.003123,
8932.837953, 8661.336305, 8949.625535, 9048.100379, 10684.87284,
8845.185424, 8182.999872, 8986.675848, 8136.137692, 10504.2443,
8848.254372, 7233.813327, 8707.732966, 8381.547529, 10471.33626,
7682.888263, 8071.666541, 7428.171461, 9736.360333, 9378.789551,
8294.552055, 8225.545799, 8874.930993, 8459.226077, 8749.99335,
9192.455984, 7875.820212, 8982.410256, 8642.199262, 8935.14394,
8480.821358, 10240.80452, 8746.68483, 7619.897735, 8417.475201
)), .Names = c("ENTRY", "BLOCK", "PLOT", "RANGE", "ROW",
"YIELD"), row.names = 372:587, class = "data.frame")
The spatial arrangement of the data:
library(reshape2)
dcast(my.data, RANGE ~ ROW, value.var ="YIELD")
Possible examples of models to analyze the data:
library(nlme)
fit1 = lme(fixed = YIELD ~ ENTRY, data = my.data,
random= ~1 | BLOCK,
method = "ML")
fit2 = lme(fixed = YIELD ~ ENTRY, data = my.data,
random= ~1 | BLOCK,
corr = corSpatial(form = ~RANGE+ROW),
method = "ML")
I have a dataframe looking like this:
chr <- c(1,1,1,1,1)
b1 <- c('HP', 'HP', 'CP', 'CP', 'KP')
b2 <- c('HP', 'HP', 'CP', 'CP', 'KP')
b3 <- c('CP', 'KP', 'CP', 'HP', 'CP')
b4 <- c('CP', 'KP', 'CP', 'HP', 'CP')
b5 <- c('CP', 'CP', 'KP', 'KP', 'HP')
b6 <- c('CP', 'CP', 'KP', 'KP', 'HP')
b7 <- c('CP', 'KP', 'HP', 'CP', 'CP')
b8 <- c('CP', 'KP', 'HP', 'CP', 'CP')
df <- data.frame(chr, b1,b2,b3,b4,b5,b6,b7,b8)
I want to write a function that looks at each 'b' column and asks if it contains the value 'HP'. If it does, and the other six 'b' columns contain 'CP' or 'KP', I want to change the value 'HP' into 'CP' or 'KP' depending on which is the majority. If CP is the majority, change the HP to CP. If KP is the majority, change HP to KP.
(note that the value of b1 and b2, b3 and b4 etc is always the same, so really only 4 columns need to be looked at, b1, b3, b5, and b7).
To clarify, if the columns are e.g. HP HP CP CP CP CP KP KP, I want to change the two HPs into CPs (and leave the other columns the same).
So, the example I gave would become:
chr <- c(1,1,1,1,1)
b1 <- c('CP', 'KP', 'CP', 'CP', 'KP')
b2 <- c('CP', 'KP', 'CP', 'CP', 'KP')
b3 <- c('CP', 'KP', 'CP', 'CP', 'CP')
b4 <- c('CP', 'KP', 'CP', 'CP', 'CP')
b5 <- c('CP', 'CP', 'KP', 'KP', 'CP')
b6 <- c('CP', 'CP', 'KP', 'KP', 'CP')
b7 <- c('CP', 'KP', 'CP', 'CP', 'CP')
b8 <- c('CP', 'KP', 'CP', 'CP', 'CP')
df <- data.frame(chr, b1,b2,b3,b4,b5,b6,b7,b8)
df
I have written a function (just for df$b1) with if statements, but it doesn't work.
(note the rules for whether the HP changes to KP or CP depend on how many other CPs or KPs there are):
fun <- function(df){
if(df$b1 == 'HP' && df$b3 == 'CP' && df$b5 == 'CP' && df$b7 == 'CP') {df$b1 <- 'KP'}
if(df$b1 == 'HP' && df$b3 == 'KP' && df$b5 == 'CP' && df$b7 == 'CP') {df$b1 <- 'CP'}
if(df$b1 == 'HP' && df$b3 == 'CP' && df$b5 == 'KP' && df$b7 == 'CP') {df$b1 <- 'CP'}
if(df$b1 == 'HP' && df$b3 == 'CP' && df$b5 == 'CP' && df$b7 == 'KP') {df$b1 <- 'CP'}
if(df$b1 == 'HP' && df$b3 == 'KP' && df$b5 == 'KP' && df$b7 == 'CP') {df$b1 <- 'KP'}
if(df$b1 == 'HP' && df$b3 == 'KP' && df$b5 == 'CP' && df$b7 == 'KP') {df$b1 <- 'KP'}
if(df$b1 == 'HP' && df$b3 == 'CP' && df$b5 == 'KP' && df$b7 == 'KP') {df$b1 <- 'KP'}
if(df$b1 == 'HP' && df$b3 == 'KP' && df$b5 == 'KP' && df$b7 == 'KP') {df$b1 <- 'CP'}
df$b2 <-df$b1
}
Thanks very much for any help. I'm really stuck on this one.
EDIT: This is a sample of my actual data which is more complex than the example I gave above.
structure(list(chr = 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), pos_c = c(2373L, 2406L, 2418L, 2419L,
2447L, 2450L, 2468L, 2524L, 2533L, 2535L, 2536L, 2542L, 2623L,
2709L, 3942L, 11716L, 11893L, 11898L, 12190L, 12396L, 26639L,
26640L, 26643L, 26646L, 26655L, 26657L, 26661L, 26667L, 26670L,
26676L, 26679L, 26684L, 26685L, 26688L, 26694L, 26703L, 26710L,
26712L, 26713L, 26723L, 26733L, 26737L, 26738L, 26739L, 26742L,
26743L, 26748L, 26761L, 26765L, 26766L, 26778L, 26781L, 26790L,
26792L, 26796L, 26802L, 26805L, 26811L, 26814L, 26819L, 26820L,
26823L, 26829L, 26838L, 26846L, 26847L, 26848L, 26872L, 26873L,
26874L, 26877L, 26878L, 26883L, 26889L, 26901L, 26904L, 26907L,
26916L, 26923L, 26925L, 26927L, 26931L, 26937L, 26940L, 26946L,
26954L, 26958L, 26961L, 26963L, 26964L, 26970L, 26981L, 26982L,
26983L, 26991L, 26994L, 26997L, 27007L, 27008L, 27009L, 27012L,
27015L, 27018L, 27027L, 202471L, 203660L, 203668L, 203669L, 203670L,
203672L, 203678L, 203683L, 203686L, 203687L, 203690L, 203704L,
203705L, 203711L, 203714L, 203732L, 203749L, 203752L, 203754L,
203755L, 203903L, 203910L, 203911L, 203912L, 203913L, 203914L,
203915L, 203922L, 203924L, 203933L, 203937L, 203939L, 203945L,
203948L, 203951L, 203957L, 203960L, 203961L, 203963L, 203969L,
203972L, 203973L, 203974L, 203975L, 203981L, 203991L, 204220L,
204227L, 204230L, 204232L, 204242L, 204245L, 204262L, 204272L,
204278L, 204282L, 204290L), c1 = c(101L, 60L, 63L, 64L, 100L,
97L, 94L, 83L, 80L, 48L, 46L, 51L, 69L, 46L, 23L, 79L, 63L, 59L,
53L, 85L, 13L, 12L, 1L, 9L, 11L, 13L, 9L, 14L, 14L, 12L, 15L,
9L, 15L, 14L, 14L, 2L, 2L, 8L, 3L, 0L, 0L, 4L, 2L, 1L, 4L, 4L,
8L, 39L, 7L, 5L, 2L, 41L, 69L, 79L, 89L, 120L, 128L, 90L, 134L,
107L, 169L, 120L, 103L, 48L, 58L, 132L, 62L, 19L, 9L, 13L, 12L,
12L, 17L, 251L, 8L, 367L, 367L, 264L, 5L, 170L, 113L, 234L, 134L,
143L, 189L, 224L, 255L, 296L, 448L, 239L, 169L, 80L, 312L, 84L,
403L, 397L, 430L, 529L, 544L, 556L, 565L, 549L, 555L, 4L, 11L,
0L, 18L, 18L, 19L, 19L, 18L, 18L, 17L, 17L, 15L, 15L, 16L, 15L,
13L, 14L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 2L, 3L, 2L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 13L, 2L, 10L, 4L, 10L, 24L, 33L, 33L, 63L, 42L), c2 = c(101L,
60L, 63L, 64L, 100L, 97L, 94L, 83L, 80L, 48L, 46L, 51L, 69L,
46L, 23L, 79L, 63L, 59L, 53L, 85L, 13L, 12L, 1L, 9L, 11L, 13L,
9L, 14L, 14L, 12L, 15L, 9L, 15L, 14L, 14L, 2L, 2L, 8L, 3L, 0L,
0L, 4L, 2L, 1L, 4L, 4L, 8L, 39L, 7L, 5L, 2L, 41L, 69L, 79L, 89L,
120L, 128L, 90L, 134L, 107L, 169L, 120L, 103L, 48L, 58L, 132L,
62L, 19L, 9L, 13L, 12L, 12L, 17L, 251L, 8L, 367L, 367L, 264L,
5L, 170L, 113L, 234L, 134L, 143L, 189L, 224L, 255L, 296L, 448L,
239L, 169L, 80L, 312L, 84L, 403L, 397L, 430L, 529L, 544L, 556L,
565L, 549L, 555L, 4L, 11L, 0L, 18L, 18L, 19L, 19L, 18L, 18L,
17L, 17L, 15L, 15L, 16L, 15L, 13L, 14L, 0L, 1L, 0L, 0L, 0L, 0L,
0L, 2L, 3L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 13L, 2L, 10L, 4L, 10L, 24L,
33L, 33L, 63L, 42L), c3 = c(37L, 0L, 0L, 0L, 42L, 46L, 46L, 21L,
26L, 6L, 2L, 7L, 11L, 4L, 0L, 4L, 1L, 0L, 0L, 2L, 29L, 29L, 0L,
22L, 23L, 23L, 26L, 27L, 29L, 24L, 32L, 26L, 35L, 32L, 32L, 3L,
3L, 10L, 1L, 5L, 1L, 6L, 1L, 0L, 5L, 11L, 6L, 81L, 15L, 14L,
0L, 92L, 157L, 174L, 168L, 236L, 221L, 143L, 228L, 251L, 292L,
273L, 281L, 33L, 39L, 260L, 57L, 53L, 24L, 22L, 26L, 37L, 37L,
484L, 16L, 721L, 724L, 436L, 7L, 367L, 163L, 411L, 167L, 373L,
275L, 599L, 637L, 773L, 866L, 615L, 223L, 63L, 531L, 59L, 878L,
868L, 911L, 939L, 975L, 995L, 980L, 931L, 958L, 12L, 16L, 0L,
12L, 13L, 12L, 11L, 9L, 12L, 11L, 11L, 10L, 1L, 0L, 0L, 0L, 1L,
1L, 2L, 1L, 0L, 1L, 1L, 0L, 2L, 2L, 2L, 0L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 2L, 28L,
5L, 28L, 3L, 12L, 39L, 40L, 50L, 90L, 80L), c4 = c(37L, 0L, 0L,
0L, 42L, 46L, 46L, 21L, 26L, 6L, 2L, 7L, 11L, 4L, 0L, 4L, 1L,
0L, 0L, 2L, 29L, 29L, 0L, 22L, 23L, 23L, 26L, 27L, 29L, 24L,
32L, 26L, 35L, 32L, 32L, 3L, 3L, 10L, 1L, 5L, 1L, 6L, 1L, 0L,
5L, 11L, 6L, 81L, 15L, 14L, 0L, 92L, 157L, 174L, 168L, 236L,
221L, 143L, 228L, 251L, 292L, 273L, 281L, 33L, 39L, 260L, 57L,
53L, 24L, 22L, 26L, 37L, 37L, 484L, 16L, 721L, 724L, 436L, 7L,
367L, 163L, 411L, 167L, 373L, 275L, 599L, 637L, 773L, 866L, 615L,
223L, 63L, 531L, 59L, 878L, 868L, 911L, 939L, 975L, 995L, 980L,
931L, 958L, 12L, 16L, 0L, 12L, 13L, 12L, 11L, 9L, 12L, 11L, 11L,
10L, 1L, 0L, 0L, 0L, 1L, 1L, 2L, 1L, 0L, 1L, 1L, 0L, 2L, 2L,
2L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L,
1L, 1L, 0L, 1L, 1L, 2L, 28L, 5L, 28L, 3L, 12L, 39L, 40L, 50L,
90L, 80L), c5 = c(96L, 77L, 74L, 72L, 96L, 96L, 92L, 80L, 79L,
79L, 76L, 76L, 66L, 55L, 64L, 78L, 110L, 100L, 165L, 171L, 38L,
41L, 2L, 38L, 33L, 37L, 21L, 40L, 41L, 21L, 37L, 19L, 45L, 30L,
22L, 22L, 28L, 34L, 30L, 31L, 25L, 40L, 34L, 33L, 34L, 46L, 41L,
96L, 48L, 51L, 38L, 93L, 152L, 155L, 155L, 193L, 195L, 189L,
222L, 213L, 284L, 248L, 230L, 56L, 70L, 208L, 82L, 85L, 67L,
64L, 64L, 83L, 71L, 495L, 77L, 570L, 577L, 499L, 55L, 292L, 236L,
352L, 244L, 296L, 351L, 391L, 440L, 483L, 653L, 417L, 194L, 57L,
460L, 57L, 538L, 520L, 573L, 731L, 753L, 770L, 772L, 757L, 761L,
35L, 73L, 66L, 70L, 70L, 71L, 70L, 74L, 79L, 82L, 83L, 85L, 69L,
68L, 71L, 71L, 70L, 73L, 72L, 72L, 74L, 103L, 107L, 106L, 107L,
109L, 106L, 106L, 105L, 106L, 105L, 108L, 104L, 105L, 106L, 106L,
103L, 112L, 112L, 113L, 112L, 109L, 114L, 114L, 115L, 120L, 114L,
97L, 125L, 103L, 124L, 107L, 116L, 145L, 139L, 138L, 177L, 139L
), c6 = c(96L, 77L, 74L, 72L, 96L, 96L, 92L, 80L, 79L, 79L, 76L,
76L, 66L, 55L, 64L, 78L, 110L, 100L, 165L, 171L, 38L, 41L, 2L,
38L, 33L, 37L, 21L, 40L, 41L, 21L, 37L, 19L, 45L, 30L, 22L, 22L,
28L, 34L, 30L, 31L, 25L, 40L, 34L, 33L, 34L, 46L, 41L, 96L, 48L,
51L, 38L, 93L, 152L, 155L, 155L, 193L, 195L, 189L, 222L, 213L,
284L, 248L, 230L, 56L, 70L, 208L, 82L, 85L, 67L, 64L, 64L, 83L,
71L, 495L, 77L, 570L, 577L, 499L, 55L, 292L, 236L, 352L, 244L,
296L, 351L, 391L, 440L, 483L, 653L, 417L, 194L, 57L, 460L, 57L,
538L, 520L, 573L, 731L, 753L, 770L, 772L, 757L, 761L, 35L, 73L,
66L, 70L, 70L, 71L, 70L, 74L, 79L, 82L, 83L, 85L, 69L, 68L, 71L,
71L, 70L, 73L, 72L, 72L, 74L, 103L, 107L, 106L, 107L, 109L, 106L,
106L, 105L, 106L, 105L, 108L, 104L, 105L, 106L, 106L, 103L, 112L,
112L, 113L, 112L, 109L, 114L, 114L, 115L, 120L, 114L, 97L, 125L,
103L, 124L, 107L, 116L, 145L, 139L, 138L, 177L, 139L), c7 = c(28L,
3L, 1L, 1L, 52L, 50L, 60L, 49L, 50L, 3L, 2L, 2L, 37L, 11L, 0L,
1L, 2L, 2L, 0L, 1L, 28L, 30L, 1L, 17L, 23L, 28L, 11L, 30L, 32L,
13L, 32L, 19L, 39L, 18L, 17L, 23L, 29L, 46L, 37L, 25L, 21L, 42L,
32L, 29L, 30L, 41L, 44L, 141L, 72L, 64L, 25L, 93L, 219L, 234L,
218L, 294L, 277L, 184L, 294L, 273L, 382L, 293L, 280L, 131L, 132L,
386L, 157L, 99L, 77L, 75L, 68L, 66L, 88L, 615L, 55L, 746L, 740L,
685L, 27L, 305L, 158L, 511L, 151L, 326L, 371L, 605L, 650L, 727L,
886L, 623L, 314L, 170L, 734L, 162L, 937L, 908L, 987L, 964L, 997L,
1002L, 1007L, 960L, 980L, 28L, 75L, 61L, 96L, 98L, 97L, 96L,
93L, 101L, 99L, 100L, 98L, 91L, 90L, 90L, 89L, 87L, 76L, 75L,
75L, 76L, 88L, 92L, 87L, 86L, 88L, 87L, 85L, 87L, 87L, 83L, 86L,
87L, 86L, 86L, 89L, 83L, 83L, 84L, 84L, 86L, 83L, 86L, 88L, 87L,
88L, 84L, 81L, 118L, 90L, 120L, 90L, 101L, 127L, 134L, 140L,
172L, 160L), c8 = c(28L, 3L, 1L, 1L, 52L, 50L, 60L, 49L, 50L,
3L, 2L, 2L, 37L, 11L, 0L, 1L, 2L, 2L, 0L, 1L, 28L, 30L, 1L, 17L,
23L, 28L, 11L, 30L, 32L, 13L, 32L, 19L, 39L, 18L, 17L, 23L, 29L,
46L, 37L, 25L, 21L, 42L, 32L, 29L, 30L, 41L, 44L, 141L, 72L,
64L, 25L, 93L, 219L, 234L, 218L, 294L, 277L, 184L, 294L, 273L,
382L, 293L, 280L, 131L, 132L, 386L, 157L, 99L, 77L, 75L, 68L,
66L, 88L, 615L, 55L, 746L, 740L, 685L, 27L, 305L, 158L, 511L,
151L, 326L, 371L, 605L, 650L, 727L, 886L, 623L, 314L, 170L, 734L,
162L, 937L, 908L, 987L, 964L, 997L, 1002L, 1007L, 960L, 980L,
28L, 75L, 61L, 96L, 98L, 97L, 96L, 93L, 101L, 99L, 100L, 98L,
91L, 90L, 90L, 89L, 87L, 76L, 75L, 75L, 76L, 88L, 92L, 87L, 86L,
88L, 87L, 85L, 87L, 87L, 83L, 86L, 87L, 86L, 86L, 89L, 83L, 83L,
84L, 84L, 86L, 83L, 86L, 88L, 87L, 88L, 84L, 81L, 118L, 90L,
120L, 90L, 101L, 127L, 134L, 140L, 172L, 160L), k1 = c(39L, 64L,
68L, 69L, 38L, 38L, 41L, 51L, 54L, 84L, 83L, 84L, 57L, 50L, 43L,
58L, 72L, 71L, 29L, 35L, 0L, 0L, 10L, 1L, 1L, 0L, 3L, 0L, 0L,
1L, 0L, 3L, 0L, 0L, 0L, 14L, 14L, 9L, 15L, 18L, 24L, 20L, 20L,
27L, 28L, 10L, 28L, 27L, 59L, 64L, 73L, 43L, 19L, 7L, 27L, 5L,
23L, 30L, 29L, 65L, 10L, 46L, 27L, 160L, 168L, 95L, 175L, 255L,
265L, 271L, 270L, 76L, 269L, 77L, 14L, 12L, 11L, 118L, 382L,
204L, 220L, 181L, 290L, 290L, 114L, 209L, 89L, 159L, 7L, 144L,
95L, 9L, 180L, 411L, 105L, 125L, 97L, 19L, 3L, 3L, 2L, 12L, 1L,
540L, 1L, 32L, 14L, 14L, 13L, 13L, 15L, 14L, 12L, 11L, 12L, 11L,
12L, 13L, 13L, 9L, 18L, 17L, 8L, 18L, 6L, 2L, 1L, 2L, 1L, 2L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 4L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 0L, 2L, 1L, 21L, 28L, 49L, 50L, 54L, 45L,
44L), k2 = c(39L, 64L, 68L, 69L, 38L, 38L, 41L, 51L, 54L, 84L,
83L, 84L, 57L, 50L, 43L, 58L, 72L, 71L, 29L, 35L, 0L, 0L, 10L,
1L, 1L, 0L, 3L, 0L, 0L, 1L, 0L, 3L, 0L, 0L, 0L, 14L, 14L, 9L,
15L, 18L, 24L, 20L, 20L, 27L, 28L, 10L, 28L, 27L, 59L, 64L, 73L,
43L, 19L, 7L, 27L, 5L, 23L, 30L, 29L, 65L, 10L, 46L, 27L, 160L,
168L, 95L, 175L, 255L, 265L, 271L, 270L, 76L, 269L, 77L, 14L,
12L, 11L, 118L, 382L, 204L, 220L, 181L, 290L, 290L, 114L, 209L,
89L, 159L, 7L, 144L, 95L, 9L, 180L, 411L, 105L, 125L, 97L, 19L,
3L, 3L, 2L, 12L, 1L, 540L, 1L, 32L, 14L, 14L, 13L, 13L, 15L,
14L, 12L, 11L, 12L, 11L, 12L, 13L, 13L, 9L, 18L, 17L, 8L, 18L,
6L, 2L, 1L, 2L, 1L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 4L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 0L, 2L, 1L, 21L,
28L, 49L, 50L, 54L, 45L, 44L), k3 = c(84L, 122L, 120L, 120L,
92L, 88L, 90L, 107L, 98L, 114L, 120L, 117L, 91L, 64L, 59L, 100L,
113L, 109L, 56L, 136L, 1L, 0L, 29L, 7L, 4L, 6L, 5L, 6L, 6L, 9L,
7L, 11L, 7L, 10L, 9L, 44L, 46L, 38L, 51L, 60L, 79L, 75L, 80L,
83L, 80L, 41L, 97L, 61L, 133L, 135L, 180L, 100L, 50L, 28L, 75L,
18L, 79L, 94L, 100L, 117L, 47L, 74L, 68L, 393L, 390L, 191L, 416L,
504L, 532L, 545L, 545L, 181L, 556L, 175L, 19L, 24L, 19L, 312L,
766L, 389L, 416L, 418L, 639L, 475L, 239L, 293L, 70L, 135L, 37L,
122L, 84L, 42L, 408L, 886L, 93L, 115L, 65L, 67L, 35L, 37L, 47L,
50L, 54L, 942L, 9L, 43L, 29L, 29L, 29L, 29L, 28L, 27L, 25L, 25L,
26L, 32L, 33L, 32L, 33L, 30L, 26L, 23L, 24L, 23L, 8L, 1L, 2L,
2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 3L,
4L, 4L, 3L, 3L, 4L, 3L, 2L, 2L, 0L, 7L, 3L, 65L, 73L, 111L, 98L,
133L, 107L, 64L), k4 = c(84L, 122L, 120L, 120L, 92L, 88L, 90L,
107L, 98L, 114L, 120L, 117L, 91L, 64L, 59L, 100L, 113L, 109L,
56L, 136L, 1L, 0L, 29L, 7L, 4L, 6L, 5L, 6L, 6L, 9L, 7L, 11L,
7L, 10L, 9L, 44L, 46L, 38L, 51L, 60L, 79L, 75L, 80L, 83L, 80L,
41L, 97L, 61L, 133L, 135L, 180L, 100L, 50L, 28L, 75L, 18L, 79L,
94L, 100L, 117L, 47L, 74L, 68L, 393L, 390L, 191L, 416L, 504L,
532L, 545L, 545L, 181L, 556L, 175L, 19L, 24L, 19L, 312L, 766L,
389L, 416L, 418L, 639L, 475L, 239L, 293L, 70L, 135L, 37L, 122L,
84L, 42L, 408L, 886L, 93L, 115L, 65L, 67L, 35L, 37L, 47L, 50L,
54L, 942L, 9L, 43L, 29L, 29L, 29L, 29L, 28L, 27L, 25L, 25L, 26L,
32L, 33L, 32L, 33L, 30L, 26L, 23L, 24L, 23L, 8L, 1L, 2L, 2L,
2L, 2L, 2L, 4L, 4L, 4L, 4L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 3L, 3L, 4L, 3L, 2L, 2L, 0L, 7L, 3L, 65L, 73L, 111L, 98L,
133L, 107L, 64L), k5 = c(0L, 14L, 14L, 14L, 1L, 0L, 0L, 8L, 7L,
5L, 5L, 5L, 0L, 3L, 0L, 8L, 2L, 3L, 18L, 15L, 0L, 2L, 38L, 3L,
5L, 1L, 18L, 1L, 2L, 2L, 3L, 21L, 2L, 15L, 1L, 26L, 22L, 17L,
27L, 33L, 41L, 39L, 42L, 45L, 51L, 14L, 50L, 31L, 82L, 84L, 108L,
55L, 24L, 16L, 51L, 33L, 44L, 55L, 54L, 87L, 15L, 20L, 27L, 285L,
297L, 151L, 293L, 343L, 363L, 374L, 376L, 57L, 382L, 24L, 25L,
10L, 8L, 103L, 551L, 301L, 320L, 276L, 364L, 340L, 49L, 272L,
171L, 195L, 24L, 180L, 161L, 11L, 254L, 663L, 188L, 229L, 158L,
26L, 3L, 3L, 6L, 10L, 6L, 708L, 0L, 9L, 0L, 3L, 0L, 1L, 0L, 2L,
0L, 0L, 1L, 9L, 9L, 9L, 10L, 10L, 6L, 6L, 1L, 6L, 2L, 0L, 5L,
3L, 2L, 3L, 4L, 2L, 3L, 2L, 2L, 1L, 3L, 0L, 0L, 4L, 1L, 0L, 1L,
5L, 2L, 0L, 1L, 2L, 0L, 2L, 5L, 1L, 3L, 3L, 43L, 50L, 78L, 75L,
87L, 78L, 59L), k6 = c(0L, 14L, 14L, 14L, 1L, 0L, 0L, 8L, 7L,
5L, 5L, 5L, 0L, 3L, 0L, 8L, 2L, 3L, 18L, 15L, 0L, 2L, 38L, 3L,
5L, 1L, 18L, 1L, 2L, 2L, 3L, 21L, 2L, 15L, 1L, 26L, 22L, 17L,
27L, 33L, 41L, 39L, 42L, 45L, 51L, 14L, 50L, 31L, 82L, 84L, 108L,
55L, 24L, 16L, 51L, 33L, 44L, 55L, 54L, 87L, 15L, 20L, 27L, 285L,
297L, 151L, 293L, 343L, 363L, 374L, 376L, 57L, 382L, 24L, 25L,
10L, 8L, 103L, 551L, 301L, 320L, 276L, 364L, 340L, 49L, 272L,
171L, 195L, 24L, 180L, 161L, 11L, 254L, 663L, 188L, 229L, 158L,
26L, 3L, 3L, 6L, 10L, 6L, 708L, 0L, 9L, 0L, 3L, 0L, 1L, 0L, 2L,
0L, 0L, 1L, 9L, 9L, 9L, 10L, 10L, 6L, 6L, 1L, 6L, 2L, 0L, 5L,
3L, 2L, 3L, 4L, 2L, 3L, 2L, 2L, 1L, 3L, 0L, 0L, 4L, 1L, 0L, 1L,
5L, 2L, 0L, 1L, 2L, 0L, 2L, 5L, 1L, 3L, 3L, 43L, 50L, 78L, 75L,
87L, 78L, 59L), k7 = c(0L, 36L, 42L, 44L, 0L, 0L, 0L, 3L, 3L,
49L, 50L, 51L, 0L, 0L, 0L, 0L, 0L, 0L, 31L, 158L, 0L, 1L, 28L,
14L, 11L, 9L, 27L, 14L, 12L, 14L, 14L, 28L, 14L, 32L, 19L, 41L,
37L, 26L, 39L, 57L, 85L, 75L, 82L, 87L, 87L, 37L, 91L, 54L, 124L,
138L, 206L, 150L, 44L, 18L, 92L, 38L, 76L, 95L, 101L, 155L, 20L,
90L, 48L, 375L, 344L, 135L, 379L, 519L, 537L, 549L, 563L, 67L,
557L, 91L, 43L, 30L, 35L, 125L, 784L, 491L, 519L, 324L, 627L,
503L, 215L, 296L, 68L, 203L, 42L, 173L, 58L, 43L, 222L, 812L,
64L, 98L, 36L, 65L, 36L, 45L, 42L, 50L, 43L, 962L, 0L, 36L, 0L,
0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 15L, 17L, 15L, 13L, 12L, 25L,
27L, 8L, 26L, 7L, 2L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 6L, 5L, 4L,
6L, 0L, 0L, 5L, 0L, 1L, 0L, 5L, 3L, 0L, 0L, 4L, 0L, 1L, 4L, 2L,
9L, 3L, 59L, 77L, 123L, 107L, 144L, 119L, 79L), k8 = c(0L, 36L,
42L, 44L, 0L, 0L, 0L, 3L, 3L, 49L, 50L, 51L, 0L, 0L, 0L, 0L,
0L, 0L, 31L, 158L, 0L, 1L, 28L, 14L, 11L, 9L, 27L, 14L, 12L,
14L, 14L, 28L, 14L, 32L, 19L, 41L, 37L, 26L, 39L, 57L, 85L, 75L,
82L, 87L, 87L, 37L, 91L, 54L, 124L, 138L, 206L, 150L, 44L, 18L,
92L, 38L, 76L, 95L, 101L, 155L, 20L, 90L, 48L, 375L, 344L, 135L,
379L, 519L, 537L, 549L, 563L, 67L, 557L, 91L, 43L, 30L, 35L,
125L, 784L, 491L, 519L, 324L, 627L, 503L, 215L, 296L, 68L, 203L,
42L, 173L, 58L, 43L, 222L, 812L, 64L, 98L, 36L, 65L, 36L, 45L,
42L, 50L, 43L, 962L, 0L, 36L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L,
1L, 15L, 17L, 15L, 13L, 12L,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6L, 6L, 6L, 6L, 6L, 6L, 3L, 3L, 3L, 3L, 6L,
6L, 3L, 6L, 6L, 6L, 6L, 3L, 6L, 3L, 3L, 4L, 3L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 3L, 2L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 4L, 4L,
4L, 6L, 4L, 2L, 6L, 2L, 2L, 2L, 4L, 3L, 6L, 3L, 6L, 3L, 6L,
3L, 6L, 6L, 2L, 6L, 6L, 6L, 6L, 4L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 4L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 6L, 6L, 6L, 6L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 6L, 2L, 6L, 3L, 3L, 6L, 3L, 3L,
6L), .Label = c("CF", "CP", "HP", "KP", "NF", "NP"), class = "factor")), .Names = c("chr",
"pos_c", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "k1",
"k2", "k3", "k4", "k5", "k6", "k7", "k8", "b1", "b2", "b3", "b4",
"b5", "b6", "b7", "b8"), class = "data.frame", row.names = c(NA,
-161L))
You can try:
t(apply(df[,-1], 1, function(rg){
occ_rg <- table(rg)
rg[grep("HP",rg)] <- names(occ_rg)[which.max(occ_rg)]
return(rg)}))
So, to have your new df:
df <- data.frame(chr=df[, 1], t(apply(df[,-1], 1, function(rg){
occ_rg <- table(rg)
rg[grep("HP",rg)] <- names(occ_rg)[which.max(occ_rg)]
return(rg)})),
stringsAsFactors=F)
# chr b1 b2 b3 b4 b5 b6 b7 b8
#1 1 CP CP CP CP CP CP CP CP
#2 1 KP KP KP KP CP CP KP KP
#3 1 CP CP CP CP KP KP CP CP
#4 1 CP CP CP CP KP KP CP CP
#5 1 KP KP CP CP CP CP CP CP
EDIT
If you have other columns and the columns you want to change are the only ones beginning with "b", you can do :
df[, grepl("^b", colnames(df))] <- t(apply(df[, grepl("^b", colnames(df))],
1,
function(rg){
occ_rg <- table(rg)
rg[grep("HP",rg)] <- names(occ_rg)[which.max(occ_rg)]
return(rg)}))
Example:
With this df:
# chr c1 b1 b2 b3 b4 b5 b6 b7 b8 c2
#1 1 1 HP HP CP CP CP CP CP CP 11
#2 1 2 HP HP KP KP CP CP KP KP 12
#3 1 3 CP CP CP CP KP KP HP HP 13
#4 1 4 CP CP HP HP KP KP CP CP 14
#5 1 5 KP KP CP CP HP HP CP CP 15
You get:
# chr c1 b1 b2 b3 b4 b5 b6 b7 b8 c2
#1 1 1 CP CP CP CP CP CP CP CP 11
#2 1 2 KP KP KP KP CP CP KP KP 12
#3 1 3 CP CP CP CP KP KP CP CP 13
#4 1 4 CP CP CP CP KP KP CP CP 14
#5 1 5 KP KP CP CP CP CP CP CP 15
EDIT 2
If you have other values than "HP", "CP" and "KP" and want to replace "HP" by either "CP" or "KP", depending on which occurs the most, you can do:
df[, grepl("^b", colnames(df))] <- t(apply(df[, grepl("^b", colnames(df))],
1,
function(rg){
occ_rg <- table(rg)
occ_rg <- occ_rg[grepl("KP|CP", names(occ_rg))]
rg[grep("HP",rg)] <- names(occ_rg)[which.max(occ_rg)]
return(rg)}))
Explanation (for edit2):
df[, grepl("^b", colnames(df))] <- # only the columns beginning with b are considered (so the other ones will remain untouched)
t( # the results of apply will be transposed
apply(df[, grepl("^b", colnames(df))], # apply on df with only the columns beginning by b
1, # by row
function(rg){ # a function that takes a vector "rg" as input
occ_rg <- table(rg) # computes the table
occ_rg <- occ_rg[grepl("KP|CP", names(occ_rg))] # keep only the occurrences of either "KP" or "CP"
rg[grep("HP",rg)] <- names(occ_rg)[which.max(occ_rg)] # replace in the vector rg the "HP" elements by "KP" or "CP" depending on which occurs the most
return(rg) # finally returns the vector rg
}))
I am trying to plot the graph that shows change in value indicated by color under two scenarios. I looked several postings in the forum but could not exactly figured out. Some of the methods I have tried so far
ggplot(regime_shift_part1, aes(x = TStep, y = KgBiomass, **colour=factor(Ecoregion)**)) +
geom_point(size=2.2)+
scale_x_discrete(name="Time Step", breaks=(0:6)*50, labels=c("2000", "2050", "2100", "2150", "2200", "2250", "2300"))+
labs(title="Maximum Biomass Producing Region") +
theme(text = element_text(size=12),axis.text.x = element_text(vjust=1))+
scale_y_continuous(name=expression(paste("Maximum Biomass (Kg m"^"-2",")")))+
geom_line(data=regime_shift_part1, aes(x=TStep, y=KgBiomass, lty=factor(Scenario)))+
facet_wrap(~Species)
This code does some part of what I wanted but the line gets disconnected when it changes from one group to another. For example, see the gap between blue and green color in "abiebals", green and orange color in "Acerrubr" and so on.
"Sorry, I can't post the images, so you may have to use the code to display"
I need the lines to be smooth. So, I tried another approach. The only editing I made in my code is highlighted by bold color. The edited code now looks as follows:
ggplot(regime_shift_part1, aes(x = TStep, y = KgBiomass)) +
**geom_point(aes(color = factor(Ecoregion)**), size=2.2)+
scale_x_discrete(name="Time Step", breaks=(0:6)*50, labels=c("2000", "2050", "2100", "2150", "2200", "2250", "2300"))+
labs(title="Maximum Biomass Producing Region") +
theme(text = element_text(size=12),axis.text.x = element_text(vjust=1))+
scale_y_continuous(name=expression(paste("Maximum Biomass (Kg m"^"-2",")")))+
geom_line(data=regime_shift_part1, aes(x=TStep, y=KgBiomass, lty=factor(Scenario)))+
facet_wrap(~Species)
" Please see the image using the code. I could not post it"
This code make line continuous but the line colors are now black. I would appreciate if you could suggest the way to make lines continuous with different colors. Also, I want to manually assign color to different ecoregions. It is difficult to differentiate ecoregion 6-7, 8-9, 13-14. I tried several ways but none of them worked.
My data is:
dput(regime_shift_part1)
structure(list(Ecoregion = c(9L, 9L, 13L, 13L, 9L, 13L, 9L, 9L,
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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 7L, 11L, 7L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
8L, 8L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 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, 8L, 8L, 8L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 12L, 12L, 12L, 12L, 12L,
12L, 5L, 5L, 5L, 8L, 8L, 8L, 8L, 14L, 14L, 14L, 14L, 8L, 8L,
8L, 8L, 7L, 7L, 7L, 9L, 8L, 8L, 8L, 8L, 8L, 8L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L,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 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), .Label = c("abiebals", "acerrubr", "acersacc",
"betualle", "betupapy", "carycord", "fagugran", "fraxamer", "fraxnigr",
"fraxpenn", "piceglau", "picemari", "pinubank", "pinuresi", "pinustro",
"popubals", "popugran", "poputrem", "prunsero", "queralba", "querelli",
"quermacr", "querrubr", "quervelu", "thujocci", "tiliamer", "tsugcana"
), class = "factor"), TStep = c(0L, 10L, 20L, 30L, 40L, 50L,
60L, 70L, 80L, 90L, 100L, 110L, 120L, 130L, 140L, 150L, 160L,
170L, 180L, 190L, 200L, 210L, 220L, 230L, 240L, 250L, 260L, 270L,
280L, 290L, 300L, 0L, 10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L,
90L, 100L, 110L, 120L, 130L, 140L, 150L, 160L, 170L, 180L, 190L,
200L, 210L, 220L, 230L, 240L, 250L, 260L, 270L, 280L, 290L, 300L,
0L, 10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 110L,
120L, 130L, 140L, 150L, 160L, 170L, 180L, 190L, 200L, 210L, 220L,
230L, 240L, 250L, 260L, 270L, 280L, 290L, 300L, 0L, 10L, 20L,
30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 110L, 120L, 130L, 140L,
150L, 160L, 170L, 180L, 190L, 200L, 210L, 220L, 230L, 240L, 250L,
260L, 270L, 280L, 290L, 300L, 0L, 10L, 20L, 30L, 40L, 50L, 60L,
70L, 80L, 90L, 100L, 110L, 120L, 130L, 140L, 150L, 160L, 170L,
180L, 190L, 200L, 210L, 220L, 230L, 240L, 250L, 260L, 270L, 280L,
290L, 300L, 0L, 10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L,
100L, 110L, 120L, 130L, 140L, 150L, 160L, 170L, 180L, 190L, 200L,
210L, 220L, 230L, 240L, 250L, 260L, 270L, 280L, 290L, 300L, 0L,
10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 110L, 120L,
130L, 140L, 150L, 160L, 170L, 180L, 190L, 200L, 210L, 220L, 230L,
240L, 250L, 260L, 270L, 280L, 290L, 300L, 0L, 10L, 20L, 30L,
40L, 50L, 60L, 70L, 80L, 90L, 100L, 110L, 120L, 130L, 140L, 150L,
160L, 170L, 180L, 190L, 200L, 210L, 220L, 230L, 240L, 250L, 260L,
270L, 280L, 290L, 300L, 0L, 10L, 20L, 30L, 40L, 50L, 60L, 70L,
80L, 90L, 100L, 110L, 120L, 130L, 140L, 150L, 160L, 170L, 180L,
190L, 200L, 210L, 220L, 230L, 240L, 250L, 260L, 270L, 280L, 290L,
300L, 0L, 10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L,
110L, 120L, 130L, 140L, 150L, 160L, 170L, 180L, 190L, 200L, 210L,
220L, 230L, 240L, 250L, 260L, 270L, 280L, 290L, 300L, 0L, 10L,
20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 110L, 120L, 130L,
140L, 150L, 160L, 170L, 180L, 190L, 200L, 210L, 220L, 230L, 240L,
250L, 260L, 270L, 280L, 290L, 300L, 0L, 10L, 20L, 30L, 40L, 50L,
60L, 70L, 80L, 90L, 100L, 110L, 120L, 130L, 140L, 150L, 160L,
170L, 180L, 190L, 200L, 210L, 220L, 230L, 240L, 250L, 260L, 270L,
280L, 290L, 300L, 0L, 10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L,
90L, 100L, 110L, 120L, 130L, 140L, 150L, 160L, 170L, 180L, 190L,
200L, 210L, 220L, 230L, 240L, 250L, 260L, 270L, 280L, 290L, 300L,
0L, 10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 110L,
120L, 130L, 140L, 150L, 160L, 170L, 180L, 190L, 200L, 210L, 220L,
230L, 240L, 250L, 260L, 270L, 280L, 290L, 300L, 0L, 10L, 20L,
30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 110L, 120L, 130L, 140L,
150L, 160L, 170L, 180L, 190L, 200L, 210L, 220L, 230L, 240L, 250L,
260L, 270L, 280L, 290L, 300L, 0L, 10L, 20L, 30L, 40L, 50L, 60L,
70L, 80L, 90L, 100L, 110L, 120L, 130L, 140L, 150L, 160L, 170L,
180L, 190L, 200L, 210L, 220L, 230L, 240L, 250L, 260L, 270L, 280L,
290L, 300L, 0L, 10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L,
100L, 110L, 120L, 130L, 140L, 150L, 160L, 170L, 180L, 190L, 200L,
210L, 220L, 230L, 240L, 250L, 260L, 270L, 280L, 290L, 300L, 0L,
10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 110L, 120L,
130L, 140L, 150L, 160L, 170L, 180L, 190L, 200L, 210L, 220L, 230L,
240L, 250L, 260L, 270L, 280L, 290L, 300L), Spnum = 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, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L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factor"), KgBiomass = c(1.5531, 1.7151,
1.8749, 2.0402, 2.2104, 2.379, 2.6025, 2.8299, 3.1307, 3.5236,
3.9801, 4.6243, 5.2637, 6.0671, 6.9228, 7.6249, 8.3212, 8.7372,
9.0633, 9.3077, 9.3587, 9.3707, 9.3207, 9.211, 9.2828, 9.3028,
9.3942, 9.2435, 9.1243, 9.2784, 9.2439, 5.0689, 5.0771, 5.09,
4.9926, 4.8535, 4.8401, 4.717, 4.573, 4.2985, 3.9545, 3.5446,
3.0536, 2.7713, 2.5378, 2.2779, 2.0237, 1.7753, 1.578, 1.4553,
1.3934, 1.381, 1.3822, 1.392, 1.4187, 1.4356, 1.4571, 1.4912,
1.4921, 1.4781, 1.3755, 1.31, 9.218, 9.4652, 9.4584, 9.5645,
9.773, 9.9784, 9.8208, 9.8178, 10.1043, 10.293, 10.8209, 11.3321,
11.7042, 12.2588, 12.9198, 13.288, 14.036, 14.9031, 15.725, 16.1998,
16.6214, 16.9101, 17.1422, 17.2767, 17.4555, 17.6199, 17.5614,
17.7563, 17.6413, 17.7908, 17.8249, 2.4078, 2.3768, 2.371, 2.3227,
2.2606, 2.2555, 2.1939, 2.1402, 2.0406, 1.9384, 1.8373, 1.7111,
1.5859, 1.4751, 1.4032, 1.327, 1.2602, 1.1885, 1.1452, 1.1336,
1.1565, 1.2179, 1.3024, 1.3954, 1.4924, 1.572, 1.6506, 1.7287,
1.8076, 1.871, 1.8792, 2.0129, 2.1072, 2.2432, 2.3797, 2.4395,
2.4376, 2.4098, 2.3166, 2.1962, 2.0202, 1.8732, 1.6444, 1.4815,
1.2857, 1.0847, 0.8942, 0.7205, 0.6031, 0.5231, 0.465, 0.421,
0.3895, 0.3814, 0.3711, 0.3692, 0.3734, 0.3781, 0.3805, 0.3834,
0.404, 0.4218, 0.3286, 0.3419, 0.3501, 0.3561, 0.3558, 0.3588,
0.3652, 0.3694, 0.3716, 0.3731, 0.3698, 0.3575, 0.3416, 0.3129,
0.2738, 0.2259, 0.1927, 0.1586, 0.1243, 0.1045, 0.0913, 0.0843,
0.0813, 0.0844, 0.0879, 0.0905, 0.0929, 0.0946, 0.0947, 0.0931,
0.0912, 0.045, 0.0493, 0.0556, 0.0618, 0.0699, 0.0797, 0.0905,
0.1028, 0.1175, 0.1342, 0.1562, 0.1837, 0.2195, 0.2615, 0.3142,
0.3705, 0.4169, 0.4557, 0.4927, 0.5289, 0.5647, 0.6016, 0.6313,
0.663, 0.6813, 0.6988, 0.7338, 0.762, 0.7931, 0.8182, 0.8325,
0.3546, 0.3558, 0.3642, 0.3728, 0.3708, 0.373, 0.3617, 0.3409,
0.3146, 0.2955, 0.2765, 0.2705, 0.2696, 0.2707, 0.2667, 0.2695,
0.2977, 0.3219, 0.3445, 0.366, 0.3838, 0.3962, 0.4006, 0.4019,
0.3889, 0.3765, 0.3685, 0.3572, 0.3497, 0.3448, 0.336, 0.5913,
0.6196, 0.6679, 0.6906, 0.6966, 0.6929, 0.6738, 0.6553, 0.6173,
0.5868, 0.5531, 0.5175, 0.4779, 0.4365, 0.3909, 0.3385, 0.2914,
0.2422, 0.1949, 0.1512, 0.1126, 0.096, 0.0886, 0.0818, 0.0722,
0.0708, 0.0749, 0.0795, 0.084, 0.0884, 0.0916, 1.7225, 1.9116,
2.0701, 2.2643, 2.3661, 2.4276, 2.4563, 2.4979, 2.5428, 2.5349,
2.5297, 2.4619, 2.3602, 2.2084, 2.036, 1.8794, 1.6888, 1.4636,
1.2234, 0.9425, 0.6853, 0.4694, 0.2897, 0.1764, 0.1071, 0.0755,
0.059, 0.0482, 0.0426, 0.0388, 0.0361, 4.5689, 4.5308, 4.5132,
4.5107, 4.5039, 4.4862, 4.5083, 4.5585, 4.5235, 4.3895, 4.2981,
4.156, 3.9117, 3.7771, 3.671, 3.5786, 3.5541, 3.5463, 3.5859,
3.556, 3.5479, 3.5365, 3.5967, 3.7016, 3.7099, 3.8148, 3.9268,
4.0046, 4.0296, 3.9728, 3.9141, 7.0826, 7.1996, 7.3625, 7.529,
7.6878, 7.9937, 8.1965, 8.6984, 9.3071, 9.9103, 10.936, 11.8715,
12.9045, 13.5709, 14.6169, 15.4318, 16.0884, 16.6487, 16.9708,
17.0343, 17.2035, 16.9599, 16.8804, 16.5998, 16.9403, 17.0379,
16.551, 16.5844, 16.4058, 16.4294, 16.6504, 2.2918, 2.2735, 2.2641,
2.275, 2.2696, 2.2673, 2.2362, 2.2028, 2.1566, 2.1272, 2.0136,
1.9496, 1.8169, 1.7454, 1.5744, 1.3721, 1.1806, 1.0561, 0.9277,
0.8247, 0.8209, 0.886, 0.9396, 0.9964, 1.0496, 1.0703, 1.1155,
1.1545, 1.1761, 1.1896, 1.1758, 2.2673, 2.3979, 2.5272, 2.5888,
2.6485, 2.5902, 2.5422, 2.4076, 2.1818, 1.9597, 1.68, 1.4059,
1.1581, 0.8921, 0.6965, 0.5331, 0.3875, 0.2485, 0.1643, 0.1037,
0.0613, 0.0316, 0.0166, 0.0085, 0.0054, 0.0031, 0.0015, 5e-04,
2e-04, 1e-04, 0, 0.2281, 0.2319, 0.2431, 0.2573, 0.2708, 0.2835,
0.2953, 0.3084, 0.3218, 0.3303, 0.3486, 0.3595, 0.3703, 0.3814,
0.3905, 0.4093, 0.4247, 0.457, 0.5003, 0.5475, 0.6024, 0.6748,
0.7713, 0.8899, 1.0086, 1.1211, 1.224, 1.3282, 1.4284, 1.5409,
1.638, 0.053, 0.0587, 0.0682, 0.0823, 0.1014, 0.1373, 0.1856,
0.2564, 0.3446, 0.4744, 0.6597, 0.9041, 1.2193, 1.6114, 2.0252,
2.5264, 3.0744, 3.588, 4.1038, 4.5957, 5.0674, 5.5739, 6.0489,
6.4466, 6.8976, 7.1861, 7.5265, 7.8668, 8.0832, 8.3965, 8.6394,
0.4436, 0.4583, 0.478, 0.5078, 0.5288, 0.5403, 0.5301, 0.5092,
0.4734, 0.4298, 0.3994, 0.3737, 0.3495, 0.3526, 0.4004, 0.477,
0.5702, 0.6644, 0.7582, 0.8512, 0.9291, 1.0014, 1.0422, 1.0965,
1.148, 1.1433, 1.157, 1.1468, 1.0928, 1.066, 1.0166, 0.7116,
0.7522, 0.8112, 0.8562, 0.8681, 0.8774, 0.9048, 0.9367, 0.939,
0.983, 0.9863, 0.9787, 0.966, 0.8786, 0.8127, 0.7215, 0.6064,
0.4709, 0.3694, 0.2835, 0.2197, 0.1813, 0.1529, 0.1301, 0.1141,
0.0956, 0.0818, 0.0735, 0.0608, 0.049, 0.0383)), .Names = c("Ecoregion",
"Species", "TStep", "Spnum", "Scenario", "KgBiomass"), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 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, 89L, 90L, 91L, 92L, 93L,
94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L,
106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 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, 151L, 152L, 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, 180L, 181L, 182L,
183L, 184L, 185L, 186L, 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, 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, 838L, 839L,
840L, 841L, 842L, 843L, 844L, 845L, 846L, 847L, 848L, 849L, 850L,
851L, 852L, 853L, 854L, 855L, 856L, 857L, 858L, 859L, 860L, 861L,
862L, 863L, 864L, 865L, 866L, 867L, 868L, 869L, 870L, 871L, 872L,
873L, 874L, 875L, 876L, 877L, 878L, 879L, 880L, 881L, 882L, 883L,
884L, 885L, 886L, 887L, 888L, 889L, 890L, 891L, 892L, 893L, 894L,
895L, 896L, 897L, 898L, 899L, 900L, 901L, 902L, 903L, 904L, 905L,
906L, 907L, 908L, 909L, 910L, 911L, 912L, 913L, 914L, 915L, 916L,
917L, 918L, 919L, 920L, 921L, 922L, 923L, 924L, 925L, 926L, 927L,
928L, 929L, 930L, 931L, 932L, 933L, 934L, 935L, 936L, 937L, 938L,
939L, 940L, 941L, 942L, 943L, 944L, 945L, 946L, 947L, 948L, 949L,
950L, 951L, 952L, 953L, 954L, 955L, 956L, 957L, 958L, 959L, 960L,
961L, 962L, 963L, 964L, 965L, 966L, 967L, 968L, 969L, 970L, 971L,
972L, 973L, 974L, 975L, 976L, 977L, 978L, 979L, 980L, 981L, 982L,
983L, 984L, 985L, 986L, 987L, 988L, 989L, 990L, 991L, 992L, 993L,
994L, 995L, 996L, 997L, 998L, 999L, 1000L, 1001L, 1002L, 1003L,
1004L, 1005L, 1006L, 1007L, 1008L, 1009L, 1010L, 1011L, 1012L,
1013L, 1014L, 1015L, 1016L, 1017L, 1018L, 1019L, 1020L, 1021L,
1022L, 1023L, 1024L, 1025L, 1026L, 1027L, 1028L, 1029L, 1030L,
1031L, 1032L, 1033L, 1034L, 1035L, 1036L, 1037L, 1038L, 1039L,
1040L, 1041L, 1042L, 1043L, 1044L, 1045L, 1046L, 1047L, 1048L,
1049L, 1050L, 1051L, 1052L, 1053L, 1054L, 1055L, 1056L, 1057L,
1058L, 1059L, 1060L, 1061L, 1062L, 1063L, 1064L, 1065L, 1066L,
1067L, 1068L, 1069L, 1070L, 1071L, 1072L, 1073L, 1074L, 1075L,
1076L, 1077L, 1078L, 1079L, 1080L, 1081L, 1082L, 1083L, 1084L,
1085L, 1086L, 1087L, 1088L, 1089L, 1090L, 1091L, 1092L, 1093L,
1094L, 1095L, 1096L, 1097L, 1098L, 1099L, 1100L, 1101L, 1102L,
1103L, 1104L, 1105L, 1106L, 1107L, 1108L, 1109L, 1110L, 1111L,
1112L, 1113L, 1114L, 1115L, 1116L), class = "data.frame")
You seem to want a continuous segue from one discrete color to another in geom_line(), but that isn't going to happen in ggplot2. You're going to have to live with the space between colors because the palette is discrete and you can only get different colors on the same line with a discrete color palette. The only way I can think of to bridge the gaps between colors would be to increase the number of TSteps. Since you're using a dashed linetype in one of the Scenarios, I don't quite understand why you "need" to have smooth lines.
To increase the number of colors available for Ecoregion, I used the qualitative rainbow() palette from base graphics, but you could also use the colorspace package to define the palette, where a variety of choices are available.
The selected palette:
# Function taken from example(colorspace::rainbow_hcl)
wheel <- function(col, radius = 1, ...)
pie(rep(1, length(col)), col = col, radius = radius, ...)
wheel(rainbow(13))
This is code for the graphic I came up with based on the data you provided (13 unique Ecoregions):
ggplot(regime_shift_part1,
aes(x = TStep, y = KgBiomass, colour=factor(Ecoregion))) +
# geom_point(size=2.2) +
theme_bw() +
scale_x_discrete(name="Time Step", breaks=(0:2)*100 + 50,
labels=seq(2050, 2250, by = 100))+
labs(title="Maximum Biomass Producing Region",
colour = "Ecoregion", linetype = "Scenario") +
theme(text = element_text(size=12), axis.text.x = element_text(vjust=1)) +
scale_y_continuous(name=expression(paste("Maximum Biomass (Kg m"^"-2",")"))) +
geom_line(aes(linetype = factor(Scenario)), size = 1.5)+
facet_wrap(~ Species) +
scale_colour_manual(breaks = levels(factor(regime_shift_part1$Ecoregion)),
values = rainbow(13))
If you try to create a continuous color gradient for Ecoregion, you get an error message from geom_line():
Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line
I have successfullly created a histogram using a date field.
hist(df.sat$created_at, breaks="hours", freq=T, xlab="Time",
main="Sat Volume")
My issue is that when I attempt to fill in the bars using col="red" both the bars and both the x/y axes change to red, when I only want the bars. What is the best way way only fill in the bars?
Here are some data:
> dput(df.sat$created_at[sample(c(1:9000), 50)])
structure(list(sec = c(41, 3, 13, 11, 49, 55, 19, 21, 6, 15,
54, 45, 45, 39, 50, 27, 35, 25, 22, 35, 42, 31, 45, 29, 1, 3,
8, 47, 38, 2, 13, 29, 34, 42, 15, 19, 3, 39, 41, 12, 34, 50,
15, 27, 0, 29, 47, 26, 21, 5), min = c(46L, 38L, 4L, 35L, 26L,
56L, 9L, 52L, 51L, 15L, 49L, 3L, 41L, 59L, 30L, 30L, 30L, 53L,
25L, 51L, 23L, 38L, 30L, 3L, 43L, 33L, 36L, 52L, 0L, 21L, 27L,
22L, 51L, 31L, 0L, 37L, 3L, 2L, 12L, 3L, 45L, 13L, 59L, 10L,
11L, 7L, 41L, 21L, 5L, 20L), hour = c(14L, 16L, 18L, 15L, 15L,
16L, 16L, 18L, 18L, 13L, 18L, 16L, 14L, 13L, 16L, 15L, 18L, 17L,
18L, 18L, 16L, 17L, 17L, 19L, 15L, 18L, 17L, 18L, 19L, 17L, 16L,
17L, 18L, 20L, 18L, 15L, 14L, 14L, 18L, 18L, 19L, 19L, 16L, 15L,
17L, 17L, 15L, 17L, 17L, 17L), mday = c(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, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), mon = 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, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), year = c(111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L,
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L,
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L,
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L,
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L), wday = 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, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L), yday = c(98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L,
98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L,
98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L,
98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L,
98L), isdst = 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)), .Names = c("sec", "min", "hour", "mday",
"mon", "year", "wday", "yday", "isdst"), class = c("POSIXlt",
"POSIXt"), tzone = c("America/New_York", "EST", "EDT"))
You'll have to get around it a bit by plotting the histogram first and the axes later :
hist(Data, breaks="hours", freq=T, xlab="Time", col="red",
main="Sat Volume",axes=F)
Axis(Data,col="black",side=1)
axis(2,col="black")
Reason to use the generic Axis(), is that it takes into account that your variable is a TimeDate class. The default axis() doesnt.
EDIT :
FYI, this behaviour is only to be seen with histograms where DateTime classes are used on the X axis. The default hist() function doesn't change the color of the axis when using a fill color for the bars.
Plot the histogram without axes and then add them in later:
hist(dat, breaks="hours", freq=TRUE, col = "red", axes = FALSE)
axis.POSIXct(side = 1, dat)
axis(2)