Related
I'm trying to make a heatmap of locations using latitude and longitude. My map works but the points in an area are being grouped together too much. What I want is something like what I have but with the heat traced much more granularly so that you can see smaller differences between places.
This is the relevant code:
bbox <- c(left = -79.1, bottom = 38, right = -71.8, top = 41.5)
coords.map <- get_stamenmap(bbox, zoom = 9, maptype = "toner-lite")
coords.map <- ggmap(coords.map, extent="device", legend="none")
coords.map <- coords.map + stat_density2d(data=recruits2,
aes(x=MailingLong, y=MailingLat, fill=..level.., alpha=..level..),
bins = 20, geom = "polygon")
coords.map <- coords.map + scale_fill_gradientn(colours=rev(brewer.pal(20, "Spectral")))
coords.map <- coords.map + theme_bw()
coords.map
This is what I'm getting
Edit: Here is the first 200 rows of recruits from dput
structure(list(MailingLong = c(-77.024196799, -77.003914977,
-76.800101376, -77.116797729, -77.018188811, -77.120614114, -76.975844627,
-77.147034967, -77.089368856, -76.97864858, -76.844030329, -76.9639802739999,
-77.257533391, -76.822314419, -80.0753704429999, -76.551298185,
-76.767346001, -78.7422733669999, -77.2918568269999, -74.252185664,
-77.035277127, -76.957338864, -77.403195991, -77.6319986669999,
-73.944649177, -76.5599475129999, -73.571443862, -76.989599958,
-76.8866457089999, -76.8900467889999, -77.027862935, -76.763716223,
-74.035259712, -77.075101833, -76.9065139969999, -77.009444624,
-76.991265512, -76.998362508, -77.0138546809999, -76.92147388,
-76.937957008, -73.897316132, -77.2021361069999, -76.9865863379999,
-77.057626187, -77.2698420199999, -73.962312531, -77.061793597,
-74.118483634, -73.8812691919999, -84.1753095879999, -76.849177244,
-76.9494415629999, -77.011200646, -77.12310071, -76.8884595649999,
-77.0061180499999, -77.017453829, -76.946342907, -76.980566735,
-76.923624123, -77.010469798, -77.02638151, -76.918852741, -76.9224746959999,
-87.802292575, -76.813914677, -73.9355411179999, -77.0086792399999,
-76.9349190729999, -77.020158344, -76.9487103719999, -73.846362546,
-77.015177632, -80.255301241, -77.428318142, -77.0007635819999,
-76.929540264, -76.947718204, -76.93487089, -77.019630846, -77.018265258,
-73.893035738, -76.887356744, -74.746807223, -76.825205743, -76.8953029259999,
-76.962229153, -77.0435782929999, -73.844867129, -76.926558429,
-76.8879371259999, -73.8547164669999, -76.920915296, -81.556675863,
-77.088045767, -76.8971086939999, -73.2218500929999, -77.283520263,
-77.0097035049999, -73.995325534, -82.7629230599999, -76.952207819,
-77.012592416, -77.0260711729999, -76.942692268, -76.981824075,
-73.261707374, -77.0867976709999, -76.904238182, -77.172019128,
-76.9965300979999, -77.0255964739999, -76.984859179, -76.9812736649999,
-77.081601215, -76.759961416, -74.6169999639999, -77.043506263,
-77.202603773, -76.949909772, -76.9530278129999, -76.7527797119999,
-75.053749344, -76.746774219, -71.778088926, -77.02110899, -76.854547506,
-77.084631879, -76.997437527, -76.901572162, -73.870879122, -76.878514023,
-74.1344631809999, -74.318660444, -82.7075784359999, -74.7182968279999,
-76.797062055, -77.5707813399999, -77.6124343819999, -75.5027960319999,
-76.987749881, -76.9821815619999, -75.828326588, -79.783059322,
-76.9499001519999, -80.259390617, -77.117644909, -77.2343587779999,
-73.8982356609999, -77.472350213, -76.9425510419999, -76.977082436,
-76.9939519549999, -77.1868244199999, -74.7314534239999, -76.965619788,
-77.007779215, -76.967062614, -76.951343771, -77.396949305, -76.999679182,
-76.9883032339999, -77.0871543499999, -76.901727819, -77.106636225,
-75.01563442, -84.127288743, -84.4811913569999, -88.267507704,
-77.011828453, -74.081075439, -72.669365125, -73.007336905, -76.923843066,
-77.019205179, -76.9821401839999, -73.927507514, -77.0365238109999,
-76.695715095, -76.871854318, -76.831811042, -75.341965451, -74.0187008359999,
-76.851878874, -72.8766466989999, -76.9806527699999, -77.327771618,
-73.9872406649999, -73.9230185489999, -74.127273734, -74.050468364,
-77.044012991, -74.068816409, -73.950062444, -76.874810759, -76.976120023,
-77.0778578119999, -77.410060892, -104.726662525), MailingLat = c(38.9681180370001,
38.728353848, 38.795798179, 38.8259334890001, 38.7331564900001,
38.8947512510001, 38.5234046750001, 38.8229569550001, 38.85372949,
38.9289913380001, 39.2386466790001, 38.929587806, 38.7748015860001,
39.4697715190001, 40.4490489200001, 39.126109242, 38.808320123,
35.80390168, 38.605807435, 40.1699337740001, 38.6193497140001,
38.922676057, 38.6439925450001, 39.9533144690001, 40.7054857840001,
39.345963984, 41.1666994920001, 38.81984234, 38.9428468840001,
38.733140373, 38.982152528, 38.824874175, 40.1742035030001, 38.78925345,
39.0010581130001, 38.9664260480001, 38.8579487740001, 38.946905756,
38.8784059170001, 38.902325202, 38.911513062, 40.9063276460001,
38.8638673040001, 38.9097773670001, 38.9240989080001, 38.8797625300001,
40.5770966100001, 38.792493279, 40.6751541570001, 42.8175953150001,
33.5073237140001, 38.9272529080001, 38.8836017150001, 38.8243908630001,
38.8446904240001, 38.8658511960001, 38.917771981, 38.938885574,
38.8719206270001, 38.885876071, 38.8924471080001, 38.833980207,
38.9562671210001, 38.8918037330001, 38.876033218, 41.914514481,
39.3841564950001, 40.8475071350001, 38.8235965260001, 38.9344933350001,
39.0245538930001, 38.639340094, 42.666483283, 38.7850074940001,
36.093245149, 38.210712964, 38.8477400190001, 38.888638916, 38.867858267,
38.8906912090001, 38.929587746, 38.9521219760001, 40.8675224940001,
38.8787024690001, 41.140377415, 38.804541885, 38.852253413, 38.8221755930001,
38.9213773750001, 40.8910776750001, 38.88197389, 39.0817835830001,
40.8247535940001, 38.8854398610001, 30.1117637080001, 39.087920518,
38.940159973, 41.2659053740001, 38.801129729, 38.896368373, 40.8391054510001,
33.720799115, 38.8651627890001, 38.826764516, 38.9238514780001,
38.9046515320001, 38.925578864, 44.0306323230001, 39.035399668,
38.5953835010001, 39.119000919, 38.9563723100001, 38.976737762,
38.960012873, 38.934212899, 38.961016375, 38.6100870970001, 40.2422624710001,
38.9248050900001, 38.85129355, 38.90282683, 38.8914932600001,
38.576783327, 40.0710674210001, 38.62669212, 42.292809602, 38.956420038,
38.845185488, 38.859804126, 38.904392242, 38.9032118310001, 40.6809593840001,
38.887417929, 40.6499415820001, 40.9926879540001, 28.0700605050001,
40.2071700650001, 38.761238259, 38.7582776680001, 38.8111178980001,
39.956535079, 38.8784065950001, 38.903570121, 44.0488523160001,
40.4418602050001, 38.9028348410001, 26.1337374180001, 38.8889284410001,
39.185845467, 40.9479277930001, 38.769957467, 38.869303099, 38.901602515,
38.8247793570001, 38.770411516, 40.1243989540001, 38.935076042,
38.9514988700001, 38.87461904, 38.8908902330001, 38.457439761,
38.8236920620001, 38.883861181, 38.938825771, 38.854090721, 38.715420469,
40.8443550060001, 41.4013022350001, 34.0559358790001, 40.0861231000001,
38.8252308690001, 40.729698834, 42.1328508010001, 40.9432147800001,
38.8837031510001, 38.845665739, 38.84924341, 41.0833210810001,
38.911186511, 39.0930236410001, 39.0773085930001, 38.990928769,
40.633318704, 40.7794771000001, 38.946823571, 41.665512818, 38.929088312,
38.861215551, 40.588697102, 40.830763007, 40.664786738, 41.044012874,
39.077050066, 40.714393369, 40.5922452140001, 38.8516571170001,
38.85133934, 39.059085399, 38.431202824, 38.7833722510001)), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L,
44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L,
57L, 60L, 61L, 62L, 64L, 65L, 66L, 67L, 68L, 69L, 71L, 72L, 73L,
74L, 75L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L,
88L, 89L, 90L, 92L, 93L, 94L, 95L, 96L, 97L, 99L, 100L, 101L,
102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L,
113L, 114L, 115L, 116L, 117L, 118L, 120L, 121L, 122L, 123L, 124L,
125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L,
138L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 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, 189L, 190L, 191L, 192L, 193L, 195L, 197L,
198L, 200L, 201L, 202L, 203L, 204L, 205L, 207L, 208L, 209L, 212L,
213L, 214L, 215L, 216L, 217L, 218L, 219L, 220L, 221L), class = "data.frame")
I have irregular polygons defined by a set of points. I can find the the maximum chord location and length, but I'm not quite sure how to analyze the points to find the longest chord location and length that is perpendicular to the maximum chord.
Here's what I've got so far, some example data of points to define a polygon:
points_ex <- structure(list(V1 = c(68L, 67L, 66L, 66L, 65L, 65L, 64L, 63L,
62L, 61L, 61L, 60L, 59L, 58L, 57L, 56L, 56L, 55L, 55L, 55L, 55L,
54L, 54L, 54L, 54L, 54L, 54L, 54L, 53L, 53L, 53L, 52L, 52L, 52L,
51L, 51L, 50L, 50L, 49L, 49L, 49L, 48L, 48L, 47L, 47L, 46L, 46L,
45L, 45L, 45L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 43L, 43L, 42L,
42L, 41L, 41L, 41L, 41L, 40L, 40L, 40L, 40L, 40L, 40L, 39L, 39L,
38L, 38L, 38L, 38L, 37L, 37L, 36L, 36L, 36L, 35L, 35L, 35L, 35L,
35L, 34L, 34L, 34L, 33L, 33L, 33L, 32L, 32L, 32L, 32L, 32L, 32L,
32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L,
32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 33L, 33L,
33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L,
33L, 33L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 31L, 31L, 31L, 31L,
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 31L, 31L,
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L,
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 32L, 32L, 33L, 33L,
34L, 34L, 35L, 36L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 43L,
44L, 44L, 45L, 46L, 46L, 47L, 47L, 48L, 48L, 49L, 49L, 50L, 51L,
51L, 51L, 52L, 52L, 52L, 52L, 53L, 53L, 53L, 53L, 54L, 54L, 54L,
54L, 54L, 54L, 54L, 53L, 53L, 53L, 53L, 53L, 53L, 52L, 52L, 52L,
51L, 51L, 51L, 51L, 51L, 51L, 51L, 51L, 52L, 52L, 52L, 52L, 52L,
53L, 53L, 54L, 54L, 55L, 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, 84L, 85L, 85L, 86L,
86L, 86L, 87L, 87L, 88L, 88L, 88L, 89L, 89L, 90L, 90L, 91L, 91L,
92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L,
104L, 105L, 105L, 106L, 106L, 107L, 108L, 109L, 109L, 110L, 111L,
112L, 113L, 114L, 115L, 116L, 117L, 118L, 118L, 119L, 120L, 121L,
122L, 123L, 123L, 124L, 125L, 126L, 126L, 127L, 127L, 127L, 127L,
127L, 128L, 128L, 128L, 128L, 128L, 128L, 128L, 128L, 128L, 129L,
129L, 129L, 129L, 129L, 129L, 130L, 130L, 130L, 130L, 131L, 131L,
131L, 131L, 131L, 132L, 132L, 132L, 132L, 132L, 133L, 133L, 133L,
133L, 133L, 133L, 133L, 132L, 132L, 132L, 131L, 131L, 130L, 130L,
129L, 129L, 128L, 128L, 128L, 128L, 128L, 128L, 128L, 128L, 128L,
129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 128L,
127L, 126L, 125L, 125L, 124L, 123L, 123L, 122L, 121L, 120L, 120L,
119L, 119L, 118L, 117L, 117L, 116L, 115L, 115L, 115L, 114L, 114L,
113L, 113L, 112L, 111L, 111L, 110L, 110L, 109L, 109L, 109L, 109L,
108L, 108L, 108L, 108L, 107L, 107L, 107L, 107L, 106L, 106L, 106L,
106L, 105L, 105L, 105L, 105L, 104L, 104L, 104L, 104L, 103L, 103L,
103L, 103L, 103L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L,
102L, 102L, 102L, 102L, 101L, 101L, 101L, 101L, 101L, 101L, 101L,
100L, 100L, 100L, 100L, 100L, 100L, 100L, 99L, 99L, 99L, 99L,
99L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 97L, 97L,
97L, 97L, 97L, 97L, 96L, 96L, 96L, 96L, 96L, 96L, 96L, 96L, 96L,
96L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L,
94L, 94L, 94L, 94L, 93L, 93L, 92L, 92L, 91L, 90L, 90L, 89L, 89L,
89L, 88L, 88L, 88L, 88L, 88L, 87L, 87L, 87L, 86L, 86L, 86L, 85L,
84L, 83L, 82L, 81L, 80L, 79L, 78L, 77L, 76L, 75L, 74L, 73L, 72L,
71L, 70L, 69L), V2 = c(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, 280L,
281L, 282L, 283L, 284L, 285L, 286L, 287L, 288L, 289L, 290L, 291L,
292L, 293L, 294L, 295L, 296L, 297L, 298L, 299L, 300L, 301L, 302L,
303L, 304L, 305L, 306L, 307L, 308L, 308L, 308L, 309L, 309L, 309L,
310L, 310L, 310L, 311L, 311L, 312L, 312L, 312L, 312L, 312L, 312L,
312L, 311L, 311L, 310L, 310L, 310L, 310L, 309L, 308L, 307L, 306L,
305L, 304L, 303L, 302L, 301L, 300L, 299L, 298L, 297L, 296L, 295L,
294L, 293L, 292L, 291L, 290L, 289L, 288L, 287L, 286L, 285L, 284L,
283L, 282L, 281L, 280L, 279L, 279L, 278L, 277L, 276L, 275L, 274L,
273L, 272L, 271L, 270L, 269L, 268L, 267L, 266L, 265L, 264L, 263L,
262L, 261L, 260L, 259L, 258L, 257L, 256L, 255L, 254L, 253L, 252L,
252L, 251L, 250L, 249L, 248L, 247L, 246L, 245L, 244L, 243L, 242L,
241L, 240L, 239L, 238L, 237L, 236L, 235L, 234L, 233L, 232L, 231L,
230L, 229L, 228L, 227L, 226L, 225L, 224L, 223L, 222L, 221L, 220L,
219L, 218L, 217L, 216L, 215L, 214L, 213L, 212L, 211L, 210L, 209L,
208L, 207L, 206L, 205L, 204L, 203L, 202L, 201L, 200L, 199L, 198L,
197L, 196L, 195L, 194L, 193L, 192L, 191L, 190L, 189L, 188L, 187L,
186L, 185L, 184L, 183L, 182L, 181L, 180L, 179L, 178L, 177L, 176L,
175L, 174L, 173L, 172L, 171L, 170L, 169L, 168L, 167L, 166L, 165L,
164L, 163L, 162L, 161L, 160L, 159L, 158L, 157L, 156L, 155L, 154L,
153L, 152L, 151L, 150L, 149L, 148L, 147L, 146L, 145L, 144L, 143L,
142L, 141L, 140L, 139L, 138L, 137L, 136L, 135L, 134L, 133L, 132L,
131L, 130L, 129L, 128L, 127L, 126L, 125L, 124L, 123L, 122L, 121L,
120L, 119L, 118L, 117L, 116L, 115L, 114L, 113L, 112L, 111L, 110L,
109L, 108L, 107L, 106L, 105L, 104L, 103L, 102L, 101L, 100L, 99L,
98L, 97L, 96L, 95L, 94L, 93L, 92L, 91L, 90L, 89L, 88L, 87L, 86L,
85L, 84L, 83L, 82L, 81L, 80L, 79L, 78L, 77L, 76L, 75L, 74L, 73L,
72L, 71L, 70L, 69L, 68L, 67L, 66L, 65L, 64L, 63L, 62L, 61L, 60L,
59L, 58L, 57L, 56L, 55L, 54L, 53L, 52L, 51L, 50L, 49L, 48L, 47L,
46L, 45L, 44L, 43L, 42L, 41L, 40L, 39L, 38L, 37L, 36L, 35L, 34L,
33L, 32L, 31L, 30L, 29L, 28L, 27L, 26L, 26L, 26L, 25L, 25L, 24L,
24L, 23L, 23L, 22L, 22L, 21L, 21L, 20L, 20L, 20L)), class = "data.frame", row.names = c(NA,
-613L))
Looks like this:
I can find the maximum chord and draw that:
# draw max dimension points and line
suppressPackageStartupMessages(library(tidyverse))
df_dist = data.frame(as.matrix(dist(cbind(points_ex$V1,points_ex$V2))))
df_dist_x = df_dist %>%
mutate(row.1 = 1:nrow(df_dist)) %>%
mutate(Y = paste0("Y", row_number())) %>%
gather(X, distance, X1:nrow(.)) %>%
select(X, Y, distance) %>%
mutate_at(vars(X, Y), parse_number)
df_dist_x_max <-
df_dist_x %>%
dplyr::filter(distance == max(distance))
points(points_ex[df_dist_x_max$X[1],], col = "red", cex = 2)
points(points_ex[df_dist_x_max$X[2],], col = "red", cex = 2.5)
segments(points_ex[df_dist_x_max$X[1], 'V1'],
points_ex[df_dist_x_max$X[1], 'V2'],
points_ex[df_dist_x_max$X[2], 'V1'],
points_ex[df_dist_x_max$X[2], 'V2'],
col = "green")
And this is what I've been trying to get the longest chord perpendicular to the maximum length chord:
# transform the points and lines into spatial objects
library(sf)
library(sp)
library(rgeos)
points_sf <- st_as_sf(points_ex, coords = c("V1", "V2"))
newline = matrix(c(points_ex[df_dist_x_max$X[1], 'V1'],
points_ex[df_dist_x_max$X[1], 'V2'],
points_ex[df_dist_x_max$X[2], 'V1'],
points_ex[df_dist_x_max$X[2], 'V2']), byrow = T, nrow = 2)
spline <- as(st_as_sfc(st_as_text(st_linestring(newline))), "Spatial") # there is probably a more straighforward solution...
position <- gProject(spline, as(points_sf, "Spatial"))
position <- coordinates(gInterpolate(spline, position))
colnames(position) <- c("X2", "Y2")
segments <-
data.frame(st_coordinates(points_sf), position)
segments$dist <- NULL
for(i in 1:nrow(segments)){
segments$dist[i] <-
proxy::dist(data.frame(segments$X[i], segments$Y[i]),
data.frame(segments$X2[i], segments$Y2[i]))
}
# max width perpendicular to length axis
max_segment <- segments[which.max(segments$dist), ]
max_segment <- segments[segments$Y == max_segment$Y, ]
segments(max_segment$X[1], max_segment$Y[1],
max_segment$X2[1], max_segment$Y2[1],
col = "purple")
segments(max_segment$X[2], max_segment$Y[2],
max_segment$X2[2], max_segment$Y2[2],
col = "purple")
Looks roughly ok, but my problem is that this method for finding the longest chord perpendicular to maximum chord only looks at one side of the maximum chord for the longest distance to the edge of the polygon.
I don't know how to measure every distance from edge to edge of the polygon that is perpendicular to the maximum chord.
This means my method doesn't generalise well at all, here it is applied to another polygon (data here: https://pastebin.com/XpiB6UnX because the dput output made this post too long)
Obviously this is bad because the two purple segments should not be on the same side of the green segment, and it doesn't look like the right location at all for the longest chord perpendicular to the maximum chord.
How can I robustly find the longest chord that is perpendicular to the maximum chord?
Here's a full solution. First some functions:
## this returns the i,j of the largest elements in matrix `m`
findmax <- function(m){
v = which.max(m) - 1
c(v %% nrow(m)+1, v %/% nrow(m)+1)
}
## Return an sf line through a point at an angle of a given length
pline <- function(pt, angle, length){
st_linestring(
cbind(
pt[1] + c(length,-length) * sin(angle),
pt[2] + c(length,-length) * cos(angle)
)
)
}
## return the line that is the chord at angle perp.angle of length through any of the polygon vertices
max_perp_chord <- function(polygon, perp.angle, length){
## get polygon vertices
pts = st_coordinates(polygon)[,c(1,2)]
## return the perpendicular lines
perplines = lapply(1:nrow(pts), function(i){
## through the i-th vertex
xy = pts[i,,drop=FALSE]
perpline = pline(xy, perp.angle, length)
## intersect it with the polygon
inters = st_intersection(polygon, perpline)
inters
}
)
## get the vector of intersection lengths, find the largest
perplengths = unlist(lapply(perplines, st_length))
longest = which.max(perplengths)
## return the longest line
perplines[[longest]]
}
### find the max length chord across all pairs of vertex points
max_chord <- function(polygon){
## get polygon coordinates
xy = st_coordinates(polygon)[,1:2]
## compute the distance matrix and find largest element
df_dist = as.matrix(dist(xy))
maxij = findmax(df_dist)
## those elements define the largest chord
chord = rbind(
xy[maxij[1],],
xy[maxij[2],]
)
chord
}
find_max_chord <- function(spolygon, chord=max_chord(spolygon)){
## Now compute the length and angle of the longest chord
chord.length = sqrt(diff(chord[,1])^2 + diff(chord[,2])^2)
chord.theta = atan2(diff(chord[,1]), diff(chord[,2]))
## The perpendicular is at this angle plus pi/2 radians
perp = chord.theta + pi/2
max_perp_chord(spolygon, perp, chord.length)
}
Here's how this goes together. The only dependency is sf - nothing else but base R is used:
library(sf)
Next massage your data into an sf polygon object:
## construct an sf polygon from points:
polygon = st_polygon(list(as.matrix(rbind(points_ex, points_ex[1,]))))
Get the max length chord between vertices:
chord = max_chord(polygon)
Plot the polygon and the chord and the chord points:
plot(polygon)
points(chord, col="red",cex=2)
lines(chord,col="green",lwd=2)
Now the meat. Get the chord perpendicular to the max chord through one of the polygon vertices:
## get the max perpendicular chord
pchord = find_max_chord(polygon, chord)
Plot it.
plot(pchord,add=TRUE)
I've tested this on another example, but not the second one you posted... Should work but...
It does...
If your polygon is monotone, you can apply the next approach:
I assume that polygon is rotated to make the maximum length chord vertical (as your two images show).
Start from the top point. Traverse left part in CCW direction, right part in clockwise direction.
Key moment: the longest chord must touch some polygon vertex (because max cannot be reached on the line segment between two inner points at edges).
So get vertex with the highest Y-coordinate (either at the left part or at the right part). Calculate point at the edge on opposite side. Get length.
Get vertex with the next Y-coordinate. Calculate point at the edge on opposite side. Get length. Continue.
My basic idea is to compute the Means of chunks (column-wise) of a large matrix and store these Means as rows of a data frame. Note, the chunks have different sizes (number of rows) and these are stored in a vector vec1. Below is my code:
df <- setNames(data.frame(matrix(nrow = 4000, ncol = 3)),
c("Age","Weight", "height"))
#
i <- 1
j <- vec1[1] - 1
k <- 0
repeat {
elements <- as.vector(apply(mydata[i : (j + 1), 3:5], 2, mean))
df <- rbind(df, elements)
k <- k + 1
i = i + vec1[k]
j = j + vec1[k + 1]
if (j + 1 >= l){
break
}
}
N.B.: When I perform the computations manually without looping it works. But the result of the loop yields a 4000 * 3 matrix filled with NA apart from the first row.
vec1 is a vector with 4000 entries, and whose first 500 elements - head(vec1, 500) -are below:
c(15L, 45L, 111L, 32L, 25L, 13L, 144L, 31L, 150L, 124L, 22L,
94L, 60L, 156L, 4L, 30L, 12L, 12L, 16L, 23L, 242L, 58L, 65L,
17L, 63L, 193L, 148L, 162L, 79L, 6L, 22L, 30L, 188L, 44L, 7L,
130L, 49L, 10L, 87L, 11L, 6L, 113L, 113L, 100L, 42L, 5L, 64L,
127L, 73L, 36L, 13L, 120L, 44L, 34L, 153L, 10L, 35L, 205L, 31L,
102L, 181L, 26L, 105L, 75L, 42L, 122L, 42L, 221L, 216L, 120L,
50L, 171L, 56L, 1L, 89L, 11L, 103L, 167L, 96L, 31L, 67L, 182L,
114L, 45L, 4L, 118L, 19L, 243L, 241L, 48L, 36L, 64L, 94L, 63L,
16L, 8L, 213L, 26L, 127L, 139L, 71L, 91L, 133L, 23L, 88L, 31L,
28L, 70L, 112L, 6L, 25L, 82L, 17L, 24L, 196L, 39L, 78L, 23L,
73L, 110L, 64L, 87L, 84L, 11L, 101L, 19L, 6L, 25L, 39L, 59L,
68L, 31L, 183L, 52L, 142L, 63L, 41L, 214L, 19L, 120L, 85L, 104L,
3L, 8L, 38L, 11L, 12L, 21L, 12L, 53L, 37L, 85L, 106L, 12L, 31L,
106L, 75L, 10L, 121L, 60L, 137L, 96L, 177L, 102L, 97L, 145L,
52L, 11L, 112L, 73L, 67L, 8L, 235L, 203L, 182L, 168L, 101L, 144L,
238L, 73L, 38L, 85L, 56L, 14L, 162L, 131L, 14L, 154L, 28L, 30L,
75L, 88L, 268L, 169L, 255L, 127L, 111L, 63L, 42L, 156L, 12L,
22L, 71L, 140L, 110L, 33L, 99L, 79L, 47L, 7L, 131L, 69L, 10L,
61L, 2L, 57L, 96L, 111L, 41L, 250L, 77L, 22L, 198L, 187L, 15L,
108L, 130L, 76L, 190L, 249L, 68L, 117L, 79L, 2L, 13L, 108L, 9L,
39L, 42L, 43L, 149L, 62L, 47L, 66L, 85L, 197L, 109L, 21L, 263L,
54L, 13L, 61L, 72L, 73L, 80L, 46L, 7L, 110L, 128L, 236L, 27L,
240L, 61L, 23L, 82L, 157L, 92L, 95L, 6L, 137L, 237L, 2L, 20L,
45L, 48L, 200L, 20L, 127L, 21L, 64L, 49L, 38L, 108L, 11L, 16L,
108L, 18L, 62L, 15L, 61L, 81L, 28L, 20L, 33L, 50L, 222L, 267L,
29L, 3L, 44L, 46L, 3L, 212L, 53L, 67L, 131L, 43L, 3L, 123L, 134L,
106L, 91L, 194L, 2L, 97L, 43L, 39L, 65L, 96L, 233L, 36L, 81L,
6L, 57L, 29L, 10L, 17L, 10L, 92L, 28L, 168L, 78L, 52L, 227L,
86L, 134L, 58L, 65L, 175L, 20L, 113L, 33L, 143L, 11L, 87L, 101L,
19L, 106L, 63L, 68L, 38L, 263L, 140L, 45L, 169L, 268L, 182L,
114L, 88L, 39L, 6L, 53L, 244L, 84L, 99L, 46L, 53L, 1L, 111L,
88L, 115L, 93L, 35L, 124L, 145L, 262L, 47L, 10L, 84L, 20L, 159L,
207L, 102L, 48L, 79L, 28L, 51L, 77L, 3L, 58L, 20L, 81L, 54L,
46L, 29L, 12L, 74L, 28L, 4L, 18L, 18L, 38L, 29L, 157L, 108L,
94L, 56L, 23L, 92L, 60L, 86L, 39L, 59L, 85L, 14L, 53L, 23L, 88L,
130L, 8L, 149L, 65L, 71L, 88L, 31L, 67L, 83L, 106L, 44L, 35L,
23L, 76L, 90L, 271L, 12L, 167L, 30L, 87L, 3L, 7L, 15L, 159L,
199L, 7L, 35L, 193L, 207L, 6L, 98L, 61L, 81L, 95L, 66L, 2L, 65L,
242L, 221L, 51L, 6L, 5L, 265L, 119L, 126L, 7L, 159L, 74L, 63L,
188L, 15L, 42L, 26L, 41L, 116L, 50L, 62L, 121L, 67L, 1L, 10L,
192L, 59L, 42L, 84L, 187L, 26L, 32L, 35L, 60L, 117L, 227L, 20L,
20L, 125L, 191L, 24L, 270L, 13L, 14L, 59L, 214L, 96L, 100L, 15L,
22L, 100L, 49L, 146L, 137L, 257L, 93L, 91L, 23L, 234L, 108L,
52L, 7L, 124L, 48L, 2L, 42L, 82L, 99L, 85L, 11L, 141L, 185L,
30L, 1L, 269L, 83L, 25L, 187L, 122L, 222L, 11L, 201L, 95L, 40L,
146L, 75L, 218L, 3L, 39L, 76L, 205L, 21L, 23L, 36L, 43L, 105L,
89L, 10L, 155L, 32L, 144L, 160L, 181L, 144L, 139L, 5L, 2L, 26L,
48L, 55L, 177L, 178L, 108L, 221L, 149L, 32L, 77L, 29L, 160L,
115L, 23L, 193L, 113L, 1L, 154L, 87L, 239L, 221L, 36L, 100L,
34L, 42L, 77L, 62L, 20L, 73L, 81L, 17L, 21L, 33L, 3L, 33L, 84L,
92L, 31L, 9L, 65L, 187L, 62L, 87L, 48L, 218L, 6L, 41L, 90L, 102L,
67L, 27L, 1L, 270L, 159L, 46L, 31L, 50L, 19L, 2L, 30L, 35L, 211L,
103L, 12L, 99L, 75L, 37L, 99L, 83L, 49L, 38L, 125L, 53L, 29L,
11L, 23L, 50L, 41L, 114L, 72L, 44L, 32L, 105L, 25L, 67L, 203L,
24L, 82L, 167L, 205L, 28L, 89L, 75L, 52L, 36L, 29L, 16L, 137L,
95L, 230L, 43L, 4L, 194L, 12L, 21L, 25L, 6L, 176L, 48L, 6L, 142L,
24L, 15L, 101L, 160L, 43L, 9L, 125L, 122L, 53L, 55L, 226L, 241L,
259L, 150L, 142L, 47L, 89L, 13L, 2L, 173L, 147L, 5L, 15L, 159L,
7L, 27L, 117L, 97L, 38L, 71L, 7L, 35L, 91L, 172L, 149L, 103L,
51L, 117L, 67L, 142L, 63L, 53L, 87L, 105L, 2L, 1L, 17L, 30L,
114L, 55L, 202L, 34L, 70L, 50L, 37L, 167L, 45L, 7L, 102L, 238L,
176L, 27L, 7L, 86L, 43L, 269L, 88L, 1L, 18L, 41L, 14L, 71L, 88L,
144L, 44L, 19L, 189L, 258L, 76L, 13L, 44L, 20L, 152L, 133L, 86L,
32L, 1L, 56L, 140L, 65L, 74L, 131L, 155L, 40L, 40L, 112L, 186L,
178L, 249L, 42L, 184L, 43L, 5L, 13L, 90L, 111L, 173L, 220L, 71L,
223L, 5L, 178L, 42L, 126L, 56L, 6L, 15L, 249L, 254L, 148L, 60L,
133L, 218L, 111L, 29L, 77L, 16L, 71L, 128L, 100L, 4L, 13L, 72L,
21L, 133L, 130L, 51L, 62L, 14L, 189L, 99L, 32L, 211L, 5L, 15L,
35L, 72L, 153L, 59L, 85L, 165L, 18L, 51L, 21L, 123L, 15L, 93L,
53L, 2L, 210L, 126L, 196L, 62L, 156L, 57L, 179L, 79L, 27L, 22L,
52L, 167L, 33L, 150L, 72L, 30L, 3L, 65L, 36L, 89L, 54L, 18L,
55L, 137L, 119L, 258L, 33L, 21L, 32L, 116L, 12L, 176L, 91L, 168L,
74L, 6L, 4L, 138L, 149L, 39L, 47L, 49L, 81L, 35L, 61L, 4L, 58L,
31L, 172L, 30L, 27L, 184L, 41L, 51L, 24L, 115L, 81L, 71L, 61L,
154L, 206L, 182L, 149L, 42L, 49L, 6L, 104L, 2L, 217L, 27L, 148L,
37L, 159L, 182L, 139L, 49L, 30L, 41L, 20L, 2L, 15L, 35L, 157L,
86L, 261L, 161L, 145L, 105L, 87L, 220L, 12L, 99L, 233L, 190L,
59L, 95L, 151L, 38L, 46L, 32L, 56L, 48L, 71L, 22L, 44L, 143L,
34L, 34L, 7L, 20L, 87L, 106L, 114L, 26L, 7L, 110L, 93L, 113L,
83L, 76L, 43L, 22L, 2L, 101L, 22L, 65L, 17L, 112L, 116L, 138L,
122L, 68L, 5L, 247L, 155L, 149L, 4L, 49L, 130L, 46L, 13L, 223L,
74L, 15L, 175L, 24L, 2L, 96L, 114L, 125L, 56L, 27L, 67L, 30L,
206L, 38L, 42L, 9L, 118L, 24L, 11L, 156L, 109L, 154L, 40L, 175L,
107L, 193L, 30L, 75L, 72L, 44L, 232L, 37L, 130L, 47L, 81L, 18L,
120L, 126L, 93L, 51L, 138L, 6L, 47L, 76L, 65L, 91L, 14L, 92L,
45L, 73L, 107L, 42L, 87L, 158L, 124L, 14L, 151L, 11L, 148L, 122L,
36L, 169L, 149L, 41L, 152L, 116L, 122L, 39L, 196L, 124L, 142L,
12L, 21L, 107L, 4L, 236L, 18L, 193L, 225L, 31L, 147L, 151L, 14L,
63L, 12L, 79L, 55L, 198L, 7L, 84L, 101L, 22L, 194L, 150L, 5L,
20L, 153L, 45L, 231L, 33L, 44L, 174L, 171L, 74L, 9L, 114L, 97L,
107L, 7L, 87L, 113L, 49L, 14L, 32L, 1L, 43L, 131L, 43L, 22L,
32L, 36L, 201L, 206L, 18L, 170L, 79L, 55L, 218L, 198L, 10L, 51L,
35L, 144L, 163L, 255L, 23L, 180L, 20L, 40L, 89L, 107L, 82L, 67L,
115L, 255L, 14L, 155L, 9L, 53L, 55L, 16L, 38L, 16L, 26L, 155L,
4L, 154L, 147L, 223L, 57L, 75L, 54L, 50L, 104L, 79L, 145L, 71L,
39L, 110L, 20L, 23L, 10L, 110L, 67L, 171L, 16L, 5L, 28L, 163L,
204L, 250L, 144L, 101L, 18L, 36L, 139L, 10L, 102L, 57L, 125L,
66L, 33L, 20L, 188L, 15L, 41L, 20L, 112L, 109L, 64L, 28L, 10L,
149L, 196L, 108L, 26L, 173L, 1L, 58L, 185L, 35L, 44L, 37L, 106L,
45L, 58L, 162L, 34L, 151L, 122L, 48L, 8L, 9L, 33L, 4L, 21L, 105L,
36L, 32L, 133L, 55L, 87L, 18L, 18L, 6L, 46L, 79L, 113L, 17L,
70L, 138L, 22L, 42L, 104L, 43L, 9L, 24L, 94L, 142L, 31L, 241L,
23L, 2L, 86L, 62L, 36L, 80L, 2L, 76L, 89L, 160L, 13L, 12L, 4L,
57L, 25L, 85L, 22L, 88L, 170L, 120L, 218L, 14L, 75L, 12L, 9L,
198L, 225L, 139L, 75L, 1L, 6L, 35L, 23L, 67L, 19L, 157L, 68L,
69L, 9L, 6L, 57L, 18L, 169L, 255L, 3L, 20L, 8L, 54L, 94L, 154L,
34L, 151L, 52L, 68L, 85L, 107L, 9L, 232L, 165L, 50L, 153L, 14L,
200L, 78L, 94L, 140L, 222L, 143L, 56L, 37L, 101L, 83L, 48L, 53L,
38L, 155L, 8L, 132L, 148L, 39L, 53L, 151L, 3L, 5L, 59L, 3L, 56L,
100L, 37L, 65L, 192L, 30L, 212L, 70L, 149L, 10L, 43L, 92L, 28L,
97L, 20L, 105L, 133L, 134L, 4L, 65L, 83L, 16L, 158L, 168L, 119L,
47L, 55L, 51L, 38L, 80L, 16L, 124L, 105L, 68L, 178L, 23L, 15L,
177L, 146L, 71L, 7L, 2L, 36L, 7L, 3L, 89L, 54L, 42L, 67L, 133L,
64L, 44L, 39L, 119L, 64L, 15L, 44L, 73L, 41L, 49L, 92L, 8L, 110L,
167L, 59L, 224L, 102L, 23L, 6L, 69L, 126L, 97L, 240L, 21L, 32L,
52L, 59L, 34L, 17L, 12L, 270L, 60L, 119L, 103L, 92L, 218L, 62L,
127L, 15L, 65L, 64L, 63L, 17L, 135L, 67L, 49L, 149L, 24L, 24L,
24L, 54L, 27L, 167L, 7L, 8L, 53L, 72L, 85L, 47L, 92L, 36L, 158L,
113L, 26L, 126L, 3L, 127L, 19L, 27L, 98L, 34L, 82L, 217L, 44L,
105L, 104L, 65L, 35L, 63L, 82L, 41L, 167L, 12L, 136L, 52L, 205L,
18L, 96L, 136L, 74L, 163L, 52L, 194L, 32L, 74L, 217L, 11L, 54L,
228L, 33L, 22L, 51L, 42L, 52L, 8L, 235L, 250L, 38L, 130L, 126L,
57L, 18L, 53L, 108L, 126L, 54L, 128L, 17L, 230L, 40L, 49L, 31L,
38L, 42L, 18L, 14L, 203L, 114L, 73L, 226L, 4L, 4L, 271L, 48L,
86L, 221L, 18L, 55L, 176L, 119L, 255L, 18L, 124L, 63L, 58L, 77L,
159L, 118L, 116L, 71L, 123L, 22L, 38L, 61L, 114L, 114L, 1L, 104L,
115L, 9L, 192L, 4L, 199L, 118L, 199L, 4L, 13L, 114L, 175L, 11L,
39L, 189L, 30L, 113L, 112L, 13L, 102L, 11L, 26L, 130L, 2L, 47L,
90L, 77L, 184L, 76L, 15L, 116L, 166L, 20L, 21L, 3L, 136L, 108L,
106L, 87L, 60L, 78L, 106L, 18L, 45L, 85L, 41L, 11L, 85L, 46L,
33L, 244L, 26L, 35L, 14L, 8L, 45L, 98L, 7L, 203L, 9L, 118L, 70L,
85L, 178L, 23L, 8L, 29L, 221L, 171L, 67L, 106L, 118L, 95L, 216L,
32L, 177L, 72L, 16L, 21L, 161L, 49L, 52L, 80L, 174L, 5L, 70L,
41L, 43L, 13L, 238L, 5L, 70L, 128L, 152L, 53L, 128L, 18L, 19L,
107L, 70L, 94L, 119L, 63L, 2L, 7L, 2L, 208L, 128L, 37L, 73L,
8L, 166L, 243L, 216L, 137L, 115L, 178L, 32L, 31L, 49L, 13L, 4L,
217L, 4L, 40L, 48L, 24L, 127L, 25L, 46L, 238L, 107L, 28L, 76L,
54L, 97L, 104L, 9L, 142L, 4L, 32L, 21L, 46L, 36L, 11L, 75L, 175L,
46L, 109L, 25L, 106L, 115L, 78L, 69L, 152L, 2L, 51L, 10L, 63L,
142L, 66L, 168L, 78L, 11L, 147L, 271L, 90L, 88L, 10L, 143L, 71L,
202L, 259L, 133L, 23L, 71L, 238L, 37L, 38L, 24L, 64L, 133L, 8L,
194L, 24L, 92L, 25L, 230L, 195L, 34L, 162L, 18L, 69L, 75L, 18L,
20L, 34L, 99L, 24L, 152L, 83L, 24L, 4L, 41L, 103L, 77L, 86L,
23L, 46L, 53L, 63L, 98L, 54L, 17L, 122L, 9L, 25L, 237L, 71L,
82L, 42L, 259L, 37L, 35L, 21L, 77L, 2L, 5L, 2L, 41L, 46L, 26L,
100L, 265L, 224L, 45L, 68L, 263L, 136L, 243L, 109L, 122L, 25L,
186L, 1L, 7L, 135L, 116L, 18L, 32L, 94L, 192L, 29L, 184L, 174L,
41L, 71L, 14L, 125L, 61L, 70L, 178L, 90L, 7L, 14L, 194L, 167L,
5L, 2L, 21L, 100L, 60L, 230L, 66L, 10L, 162L, 39L, 99L, 91L,
65L, 22L, 162L, 139L, 43L, 230L, 59L, 61L, 168L, 14L, 23L, 73L,
35L, 141L, 73L, 71L, 44L, 59L, 131L, 127L, 68L, 122L, 164L, 2L,
17L, 111L, 4L, 34L, 147L, 33L, 11L, 33L, 54L, 48L, 235L, 136L,
27L, 57L, 8L, 86L, 63L, 86L, 24L, 212L, 92L, 131L, 113L, 47L,
132L, 5L, 175L, 12L, 51L, 81L, 29L, 232L, 126L, 20L, 157L, 158L,
17L, 16L, 62L, 25L, 74L, 58L, 25L, 35L, 85L, 61L, 112L, 241L,
135L, 183L, 77L, 41L, 12L, 101L, 12L, 25L, 113L, 38L, 28L, 95L,
232L, 6L, 98L, 67L, 13L, 46L, 9L, 107L, 88L, 164L, 79L, 18L,
13L, 200L, 20L, 152L, 107L, 40L, 31L, 146L, 121L, 75L, 6L, 237L,
153L, 150L, 161L, 198L, 174L, 167L, 15L, 154L, 160L, 171L, 169L,
23L, 22L, 187L, 226L, 40L, 213L, 87L, 269L, 136L, 153L, 103L,
141L, 21L, 79L, 22L, 144L, 119L, 1L, 11L, 13L, 7L, 128L, 43L,
77L, 50L, 142L, 79L, 5L, 182L, 19L, 39L, 5L, 63L, 228L, 13L,
5L, 49L, 58L, 14L, 145L, 129L, 102L, 211L, 152L, 43L, 269L, 67L,
36L, 10L, 103L, 98L, 83L, 13L, 25L, 155L, 11L, 33L, 127L, 79L,
46L, 64L, 40L, 88L, 23L, 52L, 204L, 125L, 39L, 10L, 184L, 38L,
113L, 123L, 68L, 69L, 126L, 7L, 36L, 43L, 3L, 243L, 82L, 50L,
109L, 122L, 44L, 40L, 41L, 140L, 134L, 168L, 122L, 16L, 2L, 61L,
37L, 73L, 163L, 70L, 18L, 9L, 205L, 12L, 89L, 1L, 17L, 119L,
17L, 54L, 31L, 13L, 185L, 157L, 113L, 53L, 156L, 157L, 72L, 61L,
29L, 52L, 69L, 23L, 261L, 51L, 118L, 48L, 98L, 49L, 250L, 29L,
222L, 55L, 14L, 130L, 72L, 27L, 23L, 45L, 27L, 5L, 62L, 46L,
208L, 183L, 32L, 37L, 168L, 39L, 47L, 3L, 88L, 74L, 40L, 254L,
5L, 28L, 165L, 109L, 181L, 209L, 142L, 107L, 21L, 14L, 42L, 58L,
198L, 30L, 91L, 175L, 108L, 18L, 60L, 86L, 6L, 82L, 26L, 8L,
85L, 202L, 261L, 113L, 142L, 19L, 67L, 96L, 116L, 262L, 60L,
55L, 47L, 56L, 33L, 39L, 196L, 77L, 10L, 86L, 142L, 11L, 49L,
7L, 56L, 38L, 26L, 180L, 74L, 60L, 236L, 7L, 37L, 81L, 119L,
26L, 7L, 103L, 38L, 6L, 184L, 153L, 90L, 42L, 22L, 140L, 57L,
50L, 97L, 14L, 42L, 3L, 14L, 16L, 66L, 56L, 89L, 21L, 58L, 7L,
101L, 16L, 125L, 224L, 64L, 110L, 20L, 5L, 67L, 57L, 161L, 271L,
13L, 18L, 51L, 119L, 42L, 122L, 51L, 116L, 41L, 2L, 89L, 229L,
2L, 45L, 22L, 180L, 3L, 127L, 195L, 8L, 230L, 203L, 72L, 203L,
61L, 7L, 61L, 253L, 37L, 46L, 59L, 161L, 110L, 5L, 223L, 195L,
45L, 1L, 48L, 163L, 3L, 56L, 76L, 77L, 107L, 183L, 7L, 30L, 145L,
4L, 26L, 174L, 76L, 83L, 73L, 172L, 226L, 2L, 18L, 1L, 8L, 90L,
36L, 8L, 44L, 36L, 90L, 64L, 89L, 127L, 24L, 67L, 7L, 263L, 71L,
178L, 21L, 21L, 28L, 236L, 116L, 46L, 82L, 79L, 17L, 18L, 131L,
49L, 90L, 65L, 168L, 93L, 2L, 267L, 59L, 35L, 126L, 35L, 185L,
6L, 45L, 31L, 42L, 71L, 67L, 85L, 11L, 9L, 30L, 22L, 24L, 123L,
119L, 14L, 98L, 31L, 101L, 137L, 81L, 47L, 79L, 4L, 167L, 78L,
11L, 30L, 9L, 115L, 32L, 12L, 80L, 33L, 68L, 36L, 130L, 31L,
7L, 169L, 54L, 9L, 155L, 61L, 250L, 89L, 149L, 2L, 101L, 66L,
166L, 41L, 4L, 62L, 9L, 160L, 189L, 144L, 101L, 190L, 129L, 11L,
124L, 22L, 13L, 151L, 1L, 58L, 173L, 195L, 47L, 3L, 3L, 24L,
26L, 27L, 177L, 43L, 29L, 27L, 7L, 3L, 154L, 100L, 125L, 91L,
212L, 224L, 77L, 53L, 135L, 2L, 11L, 65L, 60L, 115L, 78L, 55L,
66L, 31L, 88L, 72L, 87L, 181L, 198L, 75L, 239L, 111L, 10L, 128L,
103L, 68L, 27L, 127L, 4L, 24L, 102L, 3L, 19L, 103L, 268L, 5L,
153L, 216L, 9L, 56L, 154L, 3L, 13L, 128L, 252L, 17L, 10L, 78L,
65L, 245L, 53L, 166L, 11L, 28L, 43L, 85L, 11L, 179L, 200L, 127L,
235L, 61L, 7L, 4L, 35L, 28L, 85L, 118L, 69L, 92L, 158L, 40L,
91L, 104L, 165L, 135L, 30L, 230L, 121L, 204L, 44L, 106L, 5L,
51L, 19L, 145L, 34L, 184L, 16L, 217L, 62L, 67L, 44L, 16L, 5L,
39L, 13L, 16L, 95L, 158L, 43L, 93L, 37L, 47L, 33L, 18L, 178L,
13L, 65L, 123L, 54L, 165L, 265L, 9L, 118L, 93L, 10L, 3L, 114L,
13L, 8L, 48L, 103L, 160L, 92L, 135L, 50L, 7L, 38L, 16L, 64L,
85L, 215L, 13L, 251L, 41L, 10L, 67L, 13L, 56L, 202L, 72L, 156L,
249L, 56L, 38L, 27L, 15L, 177L, 39L, 36L, 62L, 53L, 86L, 62L,
126L, 177L, 46L, 30L, 81L, 6L, 74L, 37L, 65L, 54L, 67L, 123L,
66L, 144L, 90L, 48L, 173L, 47L, 49L, 108L, 22L, 103L, 22L, 144L,
23L, 233L, 78L, 181L, 136L, 27L, 3L, 135L, 46L, 34L, 30L, 42L,
6L, 53L, 49L, 180L, 247L, 106L, 22L, 124L, 9L, 161L, 43L, 82L,
112L, 225L, 153L, 124L, 53L, 90L, 64L, 86L, 35L, 121L, 118L,
129L, 39L, 3L, 16L, 24L, 224L, 128L, 145L, 108L, 124L, 32L, 9L,
7L, 22L, 16L, 207L, 51L, 27L, 22L, 6L, 132L, 154L, 26L, 223L,
145L, 105L, 78L, 44L, 171L, 29L, 53L, 229L, 89L, 47L, 41L, 81L,
62L, 169L, 102L, 241L, 35L, 6L, 174L, 51L, 181L, 83L, 52L, 92L,
31L, 110L, 148L, 52L, 7L, 73L, 136L, 25L, 29L, 42L, 84L, 190L,
49L, 139L, 62L, 7L, 86L, 13L, 182L, 203L, 68L, 127L, 13L, 27L,
244L, 69L, 65L, 92L, 14L, 257L, 7L, 49L, 20L, 44L, 17L, 13L,
73L, 20L, 43L, 33L, 242L, 4L, 66L, 70L, 99L, 193L, 12L, 179L,
63L, 14L, 53L, 49L, 105L, 59L, 113L, 79L, 124L, 35L, 9L, 7L,
44L, 6L, 21L, 8L, 114L, 36L, 90L, 121L, 113L, 96L, 26L, 253L,
14L, 53L, 10L, 25L, 18L, 18L, 10L, 87L, 4L, 159L, 179L, 17L,
9L, 222L, 68L, 268L, 120L, 197L, 21L, 67L, 59L, 250L, 221L, 233L,
41L, 114L, 20L, 136L, 136L, 94L, 19L, 29L, 11L, 81L, 179L, 154L,
20L, 29L, 148L, 249L, 34L, 246L, 212L, 46L, 4L, 33L, 118L, 47L,
246L, 116L, 42L, 91L, 60L, 49L, 186L, 37L, 85L, 8L, 26L, 5L,
30L, 44L, 22L, 28L, 48L, 144L, 200L, 33L, 29L, 77L, 15L, 120L,
33L, 27L, 53L, 126L, 183L, 79L, 62L, 102L, 61L, 112L, 56L, 77L,
201L, 74L, 7L, 99L, 120L, 110L, 148L, 35L, 48L, 18L, 4L, 16L,
84L, 53L, 39L, 20L, 36L, 159L, 30L, 3L, 46L, 247L, 31L, 127L,
61L, 127L, 238L, 109L, 154L, 178L, 78L, 31L, 5L, 77L, 69L, 3L,
49L, 165L, 91L, 29L, 72L, 24L, 30L, 105L, 55L, 225L, 28L, 36L,
13L, 18L, 106L, 56L, 143L, 105L, 55L, 33L, 4L, 100L, 215L, 59L,
169L, 103L, 70L, 76L, 189L, 42L, 94L, 101L, 41L, 83L, 52L, 231L,
120L, 111L, 37L, 198L, 69L, 57L, 51L, 13L, 14L, 55L, 24L, 74L,
136L, 1L, 218L, 110L, 125L, 26L, 106L, 203L, 46L, 57L, 16L, 90L,
186L, 209L, 64L, 254L, 1L, 103L, 175L, 3L, 5L, 41L, 51L, 232L,
89L, 73L, 67L, 260L, 85L, 189L, 249L, 166L, 72L, 250L, 56L, 2L,
66L, 232L, 33L, 259L, 12L, 47L, 7L, 106L, 193L, 63L, 132L, 3L,
21L, 76L, 195L, 15L, 43L, 171L, 29L, 108L, 84L, 199L, 189L, 98L,
43L, 83L, 28L, 67L, 47L, 195L, 62L, 57L, 53L, 163L, 48L, 65L,
188L, 3L, 52L, 257L, 62L, 62L, 114L, 38L, 128L, 26L, 205L, 100L,
75L, 104L, 56L, 146L, 105L, 35L, 26L, 18L, 46L, 25L, 96L, 61L,
1L, 91L, 13L, 169L, 35L, 54L, 77L, 35L, 9L, 213L, 124L, 22L,
29L, 52L, 203L, 98L, 61L, 8L, 33L, 14L, 11L, 13L, 48L, 105L,
76L, 22L, 136L, 123L, 18L, 39L, 39L, 9L, 212L, 11L, 37L, 9L,
59L, 254L, 18L, 85L, 38L, 180L, 159L, 94L, 42L, 15L, 230L, 38L,
35L, 19L, 98L, 185L, 10L, 24L, 103L, 67L, 8L, 63L, 200L, 135L,
34L, 39L, 19L, 62L, 175L, 13L, 9L, 1L, 37L, 116L, 41L, 42L, 105L,
54L, 17L, 90L, 47L, 38L, 34L, 23L, 105L, 23L, 57L, 115L, 107L,
58L, 50L, 121L, 123L, 23L, 99L, 31L, 148L, 9L, 106L, 4L, 76L,
55L, 102L, 66L, 135L, 43L, 73L, 7L, 255L, 15L, 24L, 229L, 115L,
55L, 52L, 18L, 22L, 39L, 181L, 1L, 135L, 45L, 103L, 24L, 180L,
118L, 228L, 219L, 116L, 90L, 154L, 35L, 73L, 65L, 48L, 58L, 35L,
26L, 166L, 66L, 128L, 15L, 28L, 109L, 154L, 3L, 24L, 52L, 89L,
50L, 53L, 69L, 17L, 15L, 124L, 50L, 134L, 267L, 11L, 194L, 6L,
143L, 40L, 35L, 223L, 12L, 27L, 45L, 181L, 60L, 37L, 19L, 6L,
24L, 57L, 75L, 12L, 93L, 38L, 27L, 140L, 32L, 57L, 115L, 82L,
262L, 5L, 185L, 223L, 10L, 72L, 7L, 110L, 12L, 81L, 61L, 29L,
91L, 12L, 85L, 62L, 34L, 73L, 27L, 16L, 85L, 216L, 228L, 157L,
66L, 73L, 38L, 88L, 26L, 83L, 184L, 10L, 108L, 43L, 11L, 3L,
47L, 61L, 139L, 10L, 8L, 69L, 11L, 63L, 224L, 82L, 5L, 22L, 3L,
51L, 39L, 5L, 232L, 150L, 93L, 89L, 174L, 5L, 85L, 159L, 49L,
150L, 187L, 101L, 29L, 20L, 48L, 4L, 142L, 44L, 57L, 105L, 79L,
51L, 91L, 89L, 115L, 14L, 67L, 2L, 165L, 114L, 2L, 17L, 67L,
38L, 108L, 23L, 103L, 223L, 1L, 34L, 21L, 41L, 73L, 186L, 55L,
14L, 61L, 81L, 75L, 15L, 95L, 85L, 145L, 222L, 139L, 231L, 162L,
79L, 67L, 80L, 75L, 17L, 27L, 48L, 38L, 27L, 71L, 100L, 51L,
132L, 2L, 183L, 110L, 23L, 37L, 103L, 30L, 43L, 138L, 1L, 13L,
83L, 180L, 27L, 21L, 236L, 78L, 118L, 93L, 95L, 83L, 28L, 15L,
236L, 41L, 51L, 11L, 181L, 91L, 4L, 40L, 86L, 165L, 24L, 115L,
252L, 28L, 35L, 13L, 15L, 7L, 9L, 27L, 33L, 9L, 40L, 5L, 105L,
28L, 5L, 16L, 117L, 153L, 27L, 141L, 52L, 168L, 10L, 84L, 17L,
47L, 56L, 233L, 140L, 69L, 221L, 19L, 8L, 71L, 37L, 123L, 137L,
10L, 55L, 146L, 14L, 41L, 69L, 142L, 89L, 4L, 37L, 170L, 37L,
35L, 182L, 70L, 24L, 158L, 83L, 25L, 38L, 116L, 132L, 209L, 69L,
221L, 41L, 114L, 28L, 20L, 42L, 132L, 83L, 168L, 87L, 64L, 249L,
155L, 66L, 113L, 44L, 35L, 100L, 133L, 31L, 126L, 10L, 184L,
53L, 64L, 57L, 22L, 2L, 30L, 25L, 39L, 151L, 164L, 42L, 72L,
2L, 38L, 29L, 8L, 22L, 9L, 91L, 58L, 58L, 78L, 82L, 117L, 104L,
29L, 80L, 70L, 137L, 137L, 115L, 10L, 87L, 66L, 1L, 11L, 21L,
118L, 262L, 70L, 5L, 153L, 118L, 35L, 249L, 68L, 38L, 79L, 30L,
39L, 39L, 158L, 17L, 145L, 5L, 8L, 47L, 177L, 77L, 203L, 94L,
107L, 96L, 68L, 7L, 12L, 24L, 18L, 146L, 13L, 164L, 54L, 73L,
143L, 96L, 22L, 5L, 100L, 71L, 65L, 1L, 16L, 22L, 13L, 39L, 101L,
39L, 75L, 148L, 45L, 257L, 67L, 18L, 50L, 62L, 29L, 222L, 96L,
7L, 7L, 130L, 108L, 44L, 48L, 109L, 67L, 112L, 100L, 169L, 260L,
130L, 169L, 79L, 111L, 121L, 15L, 21L, 240L, 220L, 56L, 8L, 18L,
4L, 37L, 98L, 46L, 247L, 66L, 69L, 19L, 66L, 112L, 42L, 103L,
122L, 155L, 36L, 4L, 60L, 39L, 25L, 2L, 182L, 105L, 157L, 5L,
70L, 16L, 55L, 52L, 39L, 156L, 14L, 118L, 88L, 91L, 132L, 52L,
18L, 38L, 31L, 35L, 75L, 186L, 45L, 110L, 232L, 52L, 135L, 33L,
11L, 29L, 129L, 147L, 20L, 20L, 59L, 46L, 6L, 53L, 251L, 120L,
192L, 41L, 87L, 38L, 134L, 5L, 120L, 130L, 71L, 121L, 84L, 183L,
166L, 20L, 8L, 20L, 74L, 201L, 35L, 176L, 189L, 17L, 231L, 48L,
38L, 3L, 142L, 53L, 199L, 135L, 6L, 38L, 256L, 76L, 6L, 56L,
154L, 25L, 76L, 69L, 149L, 107L, 113L, 246L, 61L, 23L, 6L, 76L,
3L, 68L, 70L, 89L, 130L, 226L, 31L, 157L, 24L, 80L, 170L, 169L,
64L, 12L, 110L, 47L, 141L, 159L, 22L, 53L, 167L, 61L, 81L, 98L,
172L, 261L, 99L, 9L, 13L, 132L, 103L, 16L, 97L, 186L, 35L, 128L,
73L, 136L, 62L, 187L, 30L, 31L, 26L, 115L, 76L, 260L, 54L, 11L,
169L, 227L, 43L, 6L, 23L, 212L, 23L, 68L, 119L, 181L, 34L, 137L,
144L, 48L, 101L, 25L, 10L, 92L, 5L, 92L, 132L, 206L, 44L, 113L,
9L, 25L, 249L, 69L, 250L, 67L, 35L, 6L, 60L, 251L, 6L, 32L, 94L,
13L, 224L, 21L, 43L, 81L, 9L, 9L, 95L, 11L, 7L, 26L, 172L, 46L,
17L, 3L, 2L, 39L, 26L, 7L, 18L, 57L, 88L, 16L, 47L, 136L, 135L,
73L, 26L, 60L, 56L, 77L, 158L, 23L, 1L, 139L, 234L, 76L, 99L,
28L, 22L, 83L, 114L, 6L, 122L, 7L, 36L, 59L, 4L, 33L, 79L, 25L,
26L, 8L, 28L, 19L, 33L, 2L, 23L, 44L, 158L, 56L, 14L, 8L, 56L,
16L, 36L, 90L, 18L, 22L, 7L, 74L, 70L, 2L, 51L, 13L, 130L, 25L,
17L, 23L, 48L, 37L, 60L, 17L, 58L, 15L, 41L, 261L, 245L, 35L,
17L, 41L, 234L, 13L, 11L, 192L, 3L, 5L, 29L, 14L, 34L, 4L, 110L,
63L, 47L, 157L, 9L, 116L, 120L, 29L, 126L, 26L, 106L, 219L, 209L,
93L, 255L, 137L, 88L, 96L, 87L, 229L, 23L, 128L, 101L, 62L, 2L,
193L, 58L, 1L, 8L, 146L, 44L, 12L, 27L, 99L, 270L, 54L, 41L,
161L, 231L, 53L, 126L, 139L, 77L, 55L, 32L, 6L, 159L, 131L, 54L,
266L, 87L, 13L, 205L, 154L, 3L, 82L, 35L, 11L, 2L, 56L, 84L,
110L, 116L, 28L, 30L, 60L, 74L, 12L, 147L, 31L, 206L, 31L, 56L,
209L, 115L, 149L, 33L, 198L, 205L, 71L, 28L, 40L, 201L, 32L,
3L, 40L, 75L, 91L, 32L, 9L, 4L, 192L, 11L, 41L, 30L, 46L, 57L,
44L, 243L, 67L, 118L, 108L, 181L, 83L, 45L, 93L, 13L, 2L, 104L,
163L, 92L, 8L, 17L, 14L, 150L, 5L, 60L, 123L, 100L, 105L, 110L,
225L, 249L, 207L, 100L, 188L, 138L, 6L, 176L, 68L, 91L, 8L, 20L,
18L, 21L, 79L, 20L, 4L, 99L, 136L, 28L, 156L, 7L, 36L, 226L,
33L, 42L, 1L, 28L, 227L, 11L, 9L, 157L, 206L, 34L, 17L, 61L,
113L, 112L, 158L, 24L, 18L, 36L, 75L, 40L, 18L, 183L, 3L, 37L,
92L, 69L, 13L, 213L, 48L, 163L, 188L, 251L, 59L, 75L, 1L, 12L,
46L, 232L, 13L, 74L, 32L, 149L, 219L, 22L, 59L, 109L, 264L, 25L,
141L, 5L, 67L, 41L, 5L, 71L, 19L, 63L, 114L, 28L, 76L, 80L, 86L,
71L, 18L, 166L, 40L, 57L, 185L, 88L, 115L)
The problem is that you initially created 4000 * 3 data.frame filled in with NA. Please see the corrected code. I did not put your actual data vec1 (too long) and simulated vec1 with sampling from exponential distribution. Additionally I used colMeans as more effective than apply. See the code below:
# vec1, mydata, l - simulation
set.seed(123)
vec1 <- (sample(1:271, 4000, replace = TRUE, prob = dexp(1:271, rate = .01)))
mydata <- matrix(1:(300 * 300), nrow = 300)
l <- 300
# data given by OP
df <- data.frame(Age = 1, Weight = 1, height = 1 )
df <- df[-1, ]
i <- 1
j <- vec1[1] - 1
k <- 0
repeat{
elements <- as.vector(colMeans(mydata[i:(j + 1), 3:5]))
df <- rbind(df, elements)
k <- k + 1
i = i + vec1[k]
j = j + vec1[k + 1]
if (j + 1 >= l){
break
}
}
df <- setNames(df, c("Age","Weight", "height"))
df
Output:
Age Weight height
1 608.0 908.0 1208.0
2 638.0 938.0 1238.0
3 716.0 1016.0 1316.0
4 787.5 1087.5 1387.5
5 816.0 1116.0 1416.0
6 835.0 1135.0 1435.0
Goal
My data consists of numerous points which would be too much to represent as a scatter plot. I would like to plot a point density.
Specifically, I am wondering if there is a way to obtain something similar to this with ggplot2:
Current code
pEPNSABobs1<-ggplot(dataE, aes(comp1,mod1obs, group=1))+
geom_smooth(aes(color="chartreuse"), se=F, linetype="longdash", size=1)+
stat_smooth(data=dataE, fill="chartreuse", color="chartreuse4", linetype="blank")+
geom_smooth(data=dataS, aes(comp1,mod1obs, group=1, color="lightpink"), se=F, linetype="longdash", size=1)+
stat_smooth(data=dataS, fill="lightpink",color="lightpink4", linetype="blank")+
scale_colour_manual(name = 'Legend',
values =c('lightpink'='lightpink4','chartreuse'='chartreuse4'), labels = c('PIM','ABB'))+
scale_size_area() +
xlab("CDD0.year") +
ylab(expression(f[CDD0.year.obs]))+
labs(title='(A)')+
theme(plot.title = element_text(hjust = 0, vjust=1),axis.title = element_text(size = rel(1.2)), title=element_text(size = rel(1.2)),axis.text=element_text(size = rel(1.2)),
legend.text=element_text(size = rel(1.2)))
Data
DataE <- structure(list(comp1 = c(1338.2461, 1721.8119, 1878.2578, 1781.8827, 1813.2432, 1711.5277, 2033.0855, 1636.394, 1580.0748, 1834.4927, 2150.4177, 1790.7859, 1980.2718, 1610.0624, 2064.5809, 2002.82, 1652.7442, 2143.8216, 2015.1028, 2201.7947, 1610.4855, 1983.2706, 1979.9317, 2282.4141, 1763.288, 2204.3358, 1781.4969, 2114.2082, 1567.7841, 2089.6192, 1653.2401, 1709.9662, 2118.7251, 1843.5898, 1561.9472, 1839.0119, 2441.8013, 1684.3595, 1862.4287, 2043.3588, 2043.1502, 1999.5591, 1929.3686, 1897.746, 2073.494, 1345.488, 1622.3962, 1692.8681, 1847.7492, 1863.1212, 1759.8359, 2092.2891, 1671.9526, 1646.3103, 1918.8867, 2187.7568, 1833.4847, 2031.483, 1653.4428, 2118.2413, 2049.7907, 1705.1815, 2191.5739, 2054.9909, 2253.6989, 1664.6036, 2038.5609, 2021.5454, 2380.9635, 1788.6053, 2225.5046, 1834.5804, 2104.5817, 1523.2706, 2150.1426, 1722.6452, 1750.2867, 2142.4975, 1871.6772, 1562.7973, 1865.9236, 2424.8774, 1700.2864, 1850.6267, 2059.4913, 2047.4193, 2002.2637, 1941.7118, 1916.4346, 2081.8719, 1357.3087, 1645.5375, 1739.0249, 1984.4754, 2500.5373, 2126.0111, 2067.2634, 2163.0708, 2149.2563, 1939.6116, 1872.462, 2024.5612, 2158.432, 1732.0504, 2287.9286, 1640.2811, 2332.7904, 2475.8456, 2097.2091, 1942.9509, 2121.2583, 1899.7503, 2060.2692, 2459.2143, 2395.0327, 2280.1373, 2628.2383, 2118.6924, 2209.724, 2228.5074, 2008.7249, 1819.8529, 2144.0186, 2466.9707, 2479.4277, 2483.6576, 2403.0477, 2025.4349, 2150.5789, 2332.0055, 2115.4415, 1981.608, 1912.8366, 2296.8201, 1985.4613, 1875.0743, 1969.649, 2048.9522, 1900.5933, 1921.7856, 2066.8277, 1859.8671, 1698.0369, 1493.0678, 1910.5082, 2102.3289, 2048.3883, 1949.4451, 1838.7137, 2257.4143, 1958.3303, 1836.1803, 2138.1416, 2194.0097, 1919.8365, 2200.6554, 1829.6413, 2290.396, 2156.0543, 1854.5949, 2240.8976, 2060.3596, 2362.8724, 1794.141, 2183.8272, 2129.4498, 2517.4392, 1901.8863, 2440.1299, 2059.9263, 2291.6513, 1642.4418, 2281.0632, 1888.9796, 1931.7574, 2232.7776, 1953.4144, 1613.9024, 2000.765, 2485.617, 1774.7963, 1900.3834, 2192.5563, 2149.8866, 2090.371, 2076.542, 2050.3292, 2152.2821, 1482.7591, 1674.3366, 1905.3904, 1971.8098, 1596.7659, 1899.0917, 2400.7906, 2048.1242, 1968.4794, 2057.2929, 2053.9162, 1837.3839),
mod1obs = c(0.695830136555704, 1.06337206841913, 1.14861889402282, 1.02790421278896, 1.11775825622145, 1.43143032263757, 1.66436105688503, 1.79407632202943, 1.54246894155172, 1.47641083551176, 1.61115035184228, 1.68350958534902, 1.67052919588235, 1.86924870748066, 1.52139461422218, 1.7206336638354, 1.77255323580939, 1.67014367453142, 1.4828365451913, 1.30632542733184, 1.28066122419783, 1.36281355152317, 0.922105627695953, 0.485097355039981, 0.527121512464299, 0.810559323027329, 1.02715532531355, 1.14244699815587, 0.999551948498188, 0.951386076673739, 0.708742765512991, 0.591036273251615, 0.954269537513021, 1.33390119349384, 1.32340100204498, 1.48365670129187, 1.18354444471793, 1.17538278706292, 1.13658706470025, 1.09659824662134, 1.33968115088882, 0.97520848249232, 0.926838756160161, 0.916231908195398, 0.899325038616274, 1.16824595028327, 1.11899066863449, 1.11661928913426, 1.12284805168664, 1.05643303658747, 1.06710730526053, 1.22833157533542, 0.816937586646849, 1.00335563406519, 1.402274505934, 1.41743848783742, 1.14038690876476, 0.985366123273008, 0.843537703363177, 0.99753466530825, 0.956105174325841, 0.969042102699615, 1.0638789437522, 0.996797745632918, 0.941726356849215, 0.926215042532983, 1.03697021488655, 1.24561208170624, 0.903344544294698, 0.852585271143484, 1.14969403609893, 0.951461942556407, 0.969946352436395, 0.856006282102713, 1.11792476085488, 1.13412438712209, 1.15896430227978, 1.13984814117922, 1.26400232474479, 1.3157371290327, 1.10269366567886, 0.852330476973561, 1.06803837916353, 1.00555346861516, 1.07980330220743, 0.999413354748127, 0.835341074425715, 0.750343026273598, 0.750086242001393, 0.845059990359667, 1.01550610388101, 0.97183262908527, 1.02351377836664, 0.646201913922726, 0.832803589659996, 1.02112583534199, 1.05276036662667, 0.778087551043244, 1.04311980643046, 1.14374772863475, 1.26330982513545, 1.20248850820938, 1.03073057299644, 1.06049050327533, 0.834166914153454, 0.984789639654932, 1.08805486508421, 1.40007199005997, 1.33458208694833, 1.09447880143235, 1.32094094746787, 1.01033105751792, 1.15916733600421, 0.83814699368444, 0.922361964325012, 0.821150439904281, 0.757025921805821, 0.796454341025722, 0.753864630868797, 0.709679353471203, 0.947247272243527, 1.00883103860073, 0.954505648136825, 0.921522424138087, 0.845557836615405, 0.959332247443167, 0.726910705401073, 0.825894718073668, 0.849038625330317, 0.860196521211579, 0.88419468576954, 0.986401829980697, 0.844208566273473, 0.733125744746535, 0.718381697185048, 0.826648914954142, 0.787244473508777, 0.65512488225044, 0.752501264072651, 0.723469068584019, 0.743094728670719, 1.11846155736291, 1.08257557110011, 0.982979407982961, 0.885561974111481, 0.898359447667526, 0.770274340254794, 0.964105752093132, 1.419398413941, 1.06346164710011, 0.94787165982562, 1.21888848634176, 0.902929635142765, 0.924412982720593, 1.10282712434799, 0.899503164916735, 0.850644250882259, 1.01891899751745, 1.21897925575381, 1.26360292391086, 0.924187893834376, 0.930518507010402, 1.03052665863541, 1.15564902628815, 1.01660158016781, 0.904615715917612, 0.523182725557325, 0.625326640420025, 1.03256402151814, 1.19178281509474, 1.21239098183734, 1.00933857234806, 1.01170794284609, 1.02137919901009, 0.784528093252022, 0.97997559028248, 1.15073167836534, 1.09499252446889, 1.02629257850687, 0.865796462253095, 1.08500359034302, 1.14790809213261, 0.96317054146643, 1.1105434300635, 0.817447776121345, 0.888257155328381, 0.884423797252747, 0.935227482404514, 1.07740310874975, 1.02969991871418, 0.922959386699878, 0.642079018955661, 0.653388944491712, 0.597053974284795, 0.882728190259291, 0.918365607321804, 0.814327793136811, 1.00391967118938, 1.09246216046294, 1.07787732161824)),
row.names = c(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, 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, 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),
class = "data.frame")
and
DataS <- structure(list(comp1 = c(1800.2451, 1781.6087, 1737.2914, 1816.9749, 1643.348, 1438.5377, 1338.2461, 1721.8119, 1878.2578, 1781.8827, 1813.2432, 1711.5277, 2033.0855, 1636.394, 1580.0748, 1834.4927, 2150.4177, 1790.7859, 1980.2718, 1610.0624, 2064.5809, 2002.82, 1652.7442, 2143.8216, 2015.1028, 2201.7947, 1610.4855, 1983.2706, 1979.9317, 2282.4141, 1763.288, 2204.3358, 1781.4969, 2114.2082, 1567.7841, 2089.6192, 1653.2401, 1709.9662, 2118.7251, 1843.5898, 1561.9472, 1839.0119, 2441.8013, 1684.3595, 1862.4287, 2043.3588, 2043.1502, 1999.5591, 1929.3686, 1897.746, 2073.494, 1345.488, 1622.3962, 1692.8681, 1845.1739, 1661.4121, 1746.5679, 2225.7098, 1973.0317, 1877.0764, 1875.4714, 1996.5981, 1712.4015, 1715.2255, 2149.4741, 2303.7732, 1872.9362, 2279.4588, 1513.787, 2184.5552, 2361.3593, 2148.0699, 1978.381, 2008.029, 1955.9995, 1986.7266, 2079.5143, 1960.9757, 1809.0558, 2193.2401, 1729.3818, 1926.0217, 2157.6266, 1788.1556, 1475.6945, 1854.1782, 2090.9429, 2242.7276, 2120.1378, 1982.7239, 1860.3072, 1906.2126, 2115.5587, 1970.3582, 1809.3253, 1851.249, 2147.1908, 1774.673, 1816.4041, 1934.3119, 1901.6345, 1943.6281, 1793.2585, 1826.9192, 1727.9485, 1506.61, 1440.656, 1748.6882, 1956.9693, 1938.5561, 1850.1536, 1645.5995, 2086.7712, 1723.6074, 1805.0789, 2089.9296, 2139.9376, 1891.8347, 2158.9863, 1525.1479, 2132.2023, 2061.2398, 1707.3251, 2202.9106, 2017.0934, 2359.064, 1781.5352, 2081.9002, 2091.391, 2307.257, 1722.3419, 2200.4204, 1752.5609, 2110.2504, 1432.5841, 2121.8681, 1734.5244, 1912.6964, 2134.543, 1996.4716, 1720.3715, 1902.6841, 2402.3462, 1850.2001, 1919.6724, 2123.1945, 2177.1916, 2134.0072, 1969.5795, 1896.8108, 2132.3515, 1451.0973, 1612.5966, 1725.3021, 1581.0971, 2347.5604, 2483.6352, 2035.8553, 2426.3861, 1655.1293, 2382.9981, 2468.8676, 2256.6275, 2087.2235, 2124.0978, 2068.831, 2150.4903, 2262.8365, 2198.5672, 1968.5931, 2418.6407, 1953.0811, 2108.4554, 2361.584, 1979.866, 1741.6303, 2087.9856, 2286.7883, 2395.2738, 2270.3263, 2075.8854, 1874.2124, 2038.3772, 2245.1034, 2075.8632, 1946.548, 1955.202, 2355.13, 2011.3717, 1975.8039, 2099.3788, 2136.0128, 1909.835, 1965.8319, 1829.9619, 1630.4157, 1506.5606, 1897.0851, 2069.4124, 2024.9848),
mod1obs = c(1.13466310792768, 0.931923716274403, 1.48217365869915, 1.21838260146353, 1.79940276419435, 1.76643559356371, 2.45574670199418, 1.85759003858763, 1.52414873587958, 1.56924844587154, 1.75782289196183, 1.21854647206407, 1.67402271470746, 1.57661741696957, 1.54915950383801, 1.16767311295364, 1.51513098169545, 1.58721974127532, 1.48203349385421, 1.25150055063036, 1.55523168986921, 1.16757976105911, 1.06103912158879, 1.38682784393209, 1.26706181742404, 0.922200575100749, 1.15475122512565, 0.919460864722599, 0.622581998594566, 0.888857360932288, 0.561258461668506, 0.784221352878807, 1.05841796267749, 0.954913917366984, 0.864989456398651, 0.405901971360923, 0.437238711188011, 0.238832040897009, 0.398444779604082, 0.596968751447861, 0.780373761619182, 0.593964337126505, 0.674617006647725, 0.803014994811282, 0.914435218814932, 0.832419908437008, 0.870618364283852, 0.66205782789832, 0.668271801470194, 0.582218329504303, 0.551471500972662, 0.546862377936455, 0.782666777798501, 0.797397188682925, 0.233703554128143, 0.33078183414865, 0.477622787992685, 0.788352242940992, 0.655192745268702, 0.629025187982574, 0.606750329361944, 0.688876228035905, 0.811952779578203, 0.878151955673824, 0.666102340741867, 0.234485500963779, 0.377020837708222, 0.431015138976773, 0.599545478659815, 0.485341173599891, 0.54118137004067, 0.822700963102403, 0.680465320458669, 0.750819741408091, 0.914425077741122, 1.03163581838662, 0.750374660256944, 0.994337592683161, 1.00941746422589, 1.07118881485789, 1.2413361662074, 0.862210731330022, 0.807945428753615, 1.01206355371074, 1.1466781183792, 1.04520757430783, 0.906546865827641, 0.802138329712516, 0.927446849752543, 0.909606938810288, 0.726203964235328, 0.63174380758208, 0.827670510708034, 0.796140009519362, 0.908331184943455, 1.24437229369026, 1.01038032593664, 1.20106590210506, 1.47001915216196, 1.4216087908593, 1.19649249116061, 1.28729733432818, 1.050493946874, 0.490028901207541, 0.472897051757391, 0.474117219731324, 0.85500755809614, 1.16164758242536, 1.3257510136531, 1.28575789166575, 1.63477173725642, 2.45069111586777, 2.50502453325512, 1.70995863697504, 2.4729188484128, 2.65244156137921, 2.52288384634894, 2.91133218222026, 2.80631798084833, 2.85068631154525, 2.50623525545215, 2.39950652546403, 2.49930350794263, 2.47640980454363, 2.5743114316923, 2.27093700546959, 1.9784591153946, 1.794185941834, 1.59685624413893, 1.21577767933037, 1.1993875662254, 0.905547156548879, 0.791419465032495, 1.5777113878502, 1.46727507228871, 1.03812304747397, 1.04471285876448, 0.89791346550258, 1.00531560652384, 1.03963310517253, 1.55611837868102, 1.32499035130457, 1.32064172678175, 1.61378461617361, 1.77550053496517, 1.7444038412628, 1.88853641144745, 1.44554726136069, 1.52169806808946, 1.38482876983135, 1.50693746767208, 1.56467290333013, 1.55664813927757, 1.65044541980974, 1.50357779174674, 0.764054807030921, 0.495778285132959, 0.596411488294826, 0.693937433662632, 0.932011439769366, 1.10514738904478, 0.904927677550852, 0.969827490109055, 1.05101670841548, 1.12392075468322, 1.33509689588176, 1.20342821574007, 0.993967001310557, 1.20898145783709, 1.05447146612782, 0.744232955898758, 0.844458068128595, 0.780144262373508, 0.547168299748365, 2.83490197058459, 1.23839568773548, 1.10988758878693, 0.867733363063826, 0.73962833236251, 0.85416901457008, 0.784618799106827, 0.725934558423183, 0.834658973996715, 0.875948198454898, 0.881964176473614, 0.933974735332668, 0.870382237844421, 0.772181200080244, 0.829814527221943, 0.929018949289857, 0.892966747157662, 0.891484338497291, 0.942086202624782, 0.671953671900624, 0.788435092592626, 0.620007838797593, 0.87841213508972, 1.09789753017474, 0.917411139887858, 1.12120717286975)),
row.names = c(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, 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),
class = "data.frame")
Option 1: Density Polygons
ggplot(DataS, aes(comp1, mod1obs)) +
stat_density_2d(aes(fill = ..level..),
geom = "polygon",
bins = 10) +
scale_fill_gradientn(colors = c("#FFEDA0",
"#FEB24C",
"#F03B20"))
Option 2: Hexagon Bins
ggplot(DataS, aes(comp1, mod1obs)) +
geom_hex(bins = 10) +
scale_fill_gradientn(colors = c("#FFEDA0",
"#FEB24C",
"#F03B20"))
Data
DataS <- structure(list(comp1 = c(1800.2451, 1781.6087, 1737.2914, 1816.9749, 1643.348, 1438.5377, 1338.2461, 1721.8119, 1878.2578, 1781.8827, 1813.2432, 1711.5277, 2033.0855, 1636.394, 1580.0748, 1834.4927, 2150.4177, 1790.7859, 1980.2718, 1610.0624, 2064.5809, 2002.82, 1652.7442, 2143.8216, 2015.1028, 2201.7947, 1610.4855, 1983.2706, 1979.9317, 2282.4141, 1763.288, 2204.3358, 1781.4969, 2114.2082, 1567.7841, 2089.6192, 1653.2401, 1709.9662, 2118.7251, 1843.5898, 1561.9472, 1839.0119, 2441.8013, 1684.3595, 1862.4287, 2043.3588, 2043.1502, 1999.5591, 1929.3686, 1897.746, 2073.494, 1345.488, 1622.3962, 1692.8681, 1845.1739, 1661.4121, 1746.5679, 2225.7098, 1973.0317, 1877.0764, 1875.4714, 1996.5981, 1712.4015, 1715.2255, 2149.4741, 2303.7732, 1872.9362, 2279.4588, 1513.787, 2184.5552, 2361.3593, 2148.0699, 1978.381, 2008.029, 1955.9995, 1986.7266, 2079.5143, 1960.9757, 1809.0558, 2193.2401, 1729.3818, 1926.0217, 2157.6266, 1788.1556, 1475.6945, 1854.1782, 2090.9429, 2242.7276, 2120.1378, 1982.7239, 1860.3072, 1906.2126, 2115.5587, 1970.3582, 1809.3253, 1851.249, 2147.1908, 1774.673, 1816.4041, 1934.3119, 1901.6345, 1943.6281, 1793.2585, 1826.9192, 1727.9485, 1506.61, 1440.656, 1748.6882, 1956.9693, 1938.5561, 1850.1536, 1645.5995, 2086.7712, 1723.6074, 1805.0789, 2089.9296, 2139.9376, 1891.8347, 2158.9863, 1525.1479, 2132.2023, 2061.2398, 1707.3251, 2202.9106, 2017.0934, 2359.064, 1781.5352, 2081.9002, 2091.391, 2307.257, 1722.3419, 2200.4204, 1752.5609, 2110.2504, 1432.5841, 2121.8681, 1734.5244, 1912.6964, 2134.543, 1996.4716, 1720.3715, 1902.6841, 2402.3462, 1850.2001, 1919.6724, 2123.1945, 2177.1916, 2134.0072, 1969.5795, 1896.8108, 2132.3515, 1451.0973, 1612.5966, 1725.3021, 1581.0971, 2347.5604, 2483.6352, 2035.8553, 2426.3861, 1655.1293, 2382.9981, 2468.8676, 2256.6275, 2087.2235, 2124.0978, 2068.831, 2150.4903, 2262.8365, 2198.5672, 1968.5931, 2418.6407, 1953.0811, 2108.4554, 2361.584, 1979.866, 1741.6303, 2087.9856, 2286.7883, 2395.2738, 2270.3263, 2075.8854, 1874.2124, 2038.3772, 2245.1034, 2075.8632, 1946.548, 1955.202, 2355.13, 2011.3717, 1975.8039, 2099.3788, 2136.0128, 1909.835, 1965.8319, 1829.9619, 1630.4157, 1506.5606, 1897.0851, 2069.4124, 2024.9848),
mod1obs = c(1.13466310792768, 0.931923716274403, 1.48217365869915, 1.21838260146353, 1.79940276419435, 1.76643559356371, 2.45574670199418, 1.85759003858763, 1.52414873587958, 1.56924844587154, 1.75782289196183, 1.21854647206407, 1.67402271470746, 1.57661741696957, 1.54915950383801, 1.16767311295364, 1.51513098169545, 1.58721974127532, 1.48203349385421, 1.25150055063036, 1.55523168986921, 1.16757976105911, 1.06103912158879, 1.38682784393209, 1.26706181742404, 0.922200575100749, 1.15475122512565, 0.919460864722599, 0.622581998594566, 0.888857360932288, 0.561258461668506, 0.784221352878807, 1.05841796267749, 0.954913917366984, 0.864989456398651, 0.405901971360923, 0.437238711188011, 0.238832040897009, 0.398444779604082, 0.596968751447861, 0.780373761619182, 0.593964337126505, 0.674617006647725, 0.803014994811282, 0.914435218814932, 0.832419908437008, 0.870618364283852, 0.66205782789832, 0.668271801470194, 0.582218329504303, 0.551471500972662, 0.546862377936455, 0.782666777798501, 0.797397188682925, 0.233703554128143, 0.33078183414865, 0.477622787992685, 0.788352242940992, 0.655192745268702, 0.629025187982574, 0.606750329361944, 0.688876228035905, 0.811952779578203, 0.878151955673824, 0.666102340741867, 0.234485500963779, 0.377020837708222, 0.431015138976773, 0.599545478659815, 0.485341173599891, 0.54118137004067, 0.822700963102403, 0.680465320458669, 0.750819741408091, 0.914425077741122, 1.03163581838662, 0.750374660256944, 0.994337592683161, 1.00941746422589, 1.07118881485789, 1.2413361662074, 0.862210731330022, 0.807945428753615, 1.01206355371074, 1.1466781183792, 1.04520757430783, 0.906546865827641, 0.802138329712516, 0.927446849752543, 0.909606938810288, 0.726203964235328, 0.63174380758208, 0.827670510708034, 0.796140009519362, 0.908331184943455, 1.24437229369026, 1.01038032593664, 1.20106590210506, 1.47001915216196, 1.4216087908593, 1.19649249116061, 1.28729733432818, 1.050493946874, 0.490028901207541, 0.472897051757391, 0.474117219731324, 0.85500755809614, 1.16164758242536, 1.3257510136531, 1.28575789166575, 1.63477173725642, 2.45069111586777, 2.50502453325512, 1.70995863697504, 2.4729188484128, 2.65244156137921, 2.52288384634894, 2.91133218222026, 2.80631798084833, 2.85068631154525, 2.50623525545215, 2.39950652546403, 2.49930350794263, 2.47640980454363, 2.5743114316923, 2.27093700546959, 1.9784591153946, 1.794185941834, 1.59685624413893, 1.21577767933037, 1.1993875662254, 0.905547156548879, 0.791419465032495, 1.5777113878502, 1.46727507228871, 1.03812304747397, 1.04471285876448, 0.89791346550258, 1.00531560652384, 1.03963310517253, 1.55611837868102, 1.32499035130457, 1.32064172678175, 1.61378461617361, 1.77550053496517, 1.7444038412628, 1.88853641144745, 1.44554726136069, 1.52169806808946, 1.38482876983135, 1.50693746767208, 1.56467290333013, 1.55664813927757, 1.65044541980974, 1.50357779174674, 0.764054807030921, 0.495778285132959, 0.596411488294826, 0.693937433662632, 0.932011439769366, 1.10514738904478, 0.904927677550852, 0.969827490109055, 1.05101670841548, 1.12392075468322, 1.33509689588176, 1.20342821574007, 0.993967001310557, 1.20898145783709, 1.05447146612782, 0.744232955898758, 0.844458068128595, 0.780144262373508, 0.547168299748365, 2.83490197058459, 1.23839568773548, 1.10988758878693, 0.867733363063826, 0.73962833236251, 0.85416901457008, 0.784618799106827, 0.725934558423183, 0.834658973996715, 0.875948198454898, 0.881964176473614, 0.933974735332668, 0.870382237844421, 0.772181200080244, 0.829814527221943, 0.929018949289857, 0.892966747157662, 0.891484338497291, 0.942086202624782, 0.671953671900624, 0.788435092592626, 0.620007838797593, 0.87841213508972, 1.09789753017474, 0.917411139887858, 1.12120717286975)),
row.names = c(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, 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),
class = "data.frame")
When I want to change a column from factor to numeric with "as.numeric()", the final numbers are totally different from what I imported.
It is a problem due to the comma? Really strange...
Thanks!
dput(datatest)
structure(list(HOURS.at.sea = structure(c(261L, 84L, 83L, 260L,
307L, 292L, 252L, 72L, 59L, 343L, 244L, 78L, 56L, 256L, 9L, 269L,
291L, 254L, 69L, 65L, 267L, 283L, 1L, 80L, 169L, 1L, 115L, 67L,
75L, 3L, 309L, 59L, 33L, 52L, 75L, 37L, 51L, 75L, 22L, 2L, 49L,
83L, 21L, 2L, 53L, 70L, 19L, 3L, 57L, 70L, 22L, 3L, 58L, 78L,
36L, 49L, 2L, 66L, 115L, 52L, 72L, 114L, 57L, 78L, 116L, 56L,
68L, 116L, 55L, 70L, 327L, 6L, 257L, 2L, 107L, 176L, 182L, 4L,
114L, 35L, 46L, 3L, 67L, 34L, 46L, 3L, 79L, 40L, 50L, 69L, 38L,
52L, 2L, 69L, 38L, 85L, 49L, 70L, 64L, 70L, 3L, 1L, 206L, 231L,
58L, 55L, 109L, 212L, 220L, 53L, 56L, 125L, 3L, 5L, 214L, 231L,
57L, 55L, 107L, 217L, 228L, 52L, 57L, 105L, 210L, 231L, 56L,
55L, 105L, 215L, 232L, 55L, 54L, 90L, 210L, 230L, 58L, 54L, 108L,
218L, 228L, 57L, 56L, 96L, 213L, 228L, 55L, 57L, 106L, 217L,
232L, 58L, 73L, 110L, 217L, 233L, 57L, 59L, 117L, 7L, 222L, 233L,
59L, 56L, 107L, 219L, 231L, 57L, 56L, 109L, 221L, 233L, 56L,
57L, 106L, 1L, 1L, 247L, 317L, 159L, 316L, 229L, 306L, 129L,
120L, 29L, 74L, 287L, 12L, 151L, 109L, 68L, 125L, 270L, 1L, 56L,
224L, 180L, 76L, 281L, 86L, 79L, 258L, 83L, 1L, 229L, 23L, 132L,
56L, 59L, 76L, 115L, 110L, 28L, 235L, 226L, 16L, 134L, 55L, 57L,
66L, 124L, 117L, 28L, 240L, 1L, 52L, 335L, 32L, 59L, 186L, 71L,
4L, 7L, 5L, 39L, 59L, 2L, 3L, 4L, 293L, 4L, 66L, 99L, 110L, 54L,
2L, 4L, 148L, 221L, 322L, 31L, 170L, 1L, 286L, 162L, 336L, 129L,
138L, 70L, 71L, 125L, 241L, 277L, 6L, 8L, 76L, 84L, 320L, 339L,
117L, 137L, 69L, 80L, 1L, 243L, 92L, 139L, 149L, 54L, 67L, 262L,
1L, 240L, 78L, 73L, 1L, 187L, 48L, 65L, 97L, 263L, 332L, 187L,
103L, 29L, 79L, 266L, 334L, 188L, 303L, 96L, 264L, 313L, 184L,
45L, 66L, 105L, 263L, 330L, 184L, 53L, 24L, 96L, 97L, 272L, 331L,
185L, 47L, 68L, 102L, 265L, 342L, 187L, 97L, 49L, 58L, 264L,
333L, 195L, 96L, 31L, 181L, 265L, 1L, 236L, 79L, 270L, 89L, 246L,
45L, 207L, 166L, 79L, 41L, 177L, 55L, 128L, 1L, 152L, 205L, 57L,
168L, 195L, 284L, 94L, 118L, 340L, 82L, 123L, 57L, 73L, 118L,
58L, 227L, 18L, 51L, 323L, 178L, 49L, 120L, 26L, 59L, 52L, 67L,
67L, 114L, 234L, 1L, 15L, 201L, 163L, 42L, 72L, 47L, 10L, 66L,
106L, 224L, 17L, 189L, 44L, 200L, 164L, 45L, 69L, 47L, 67L, 102L,
275L, 14L, 190L, 30L, 199L, 161L, 47L, 71L, 52L, 276L, 32L, 199L,
161L, 48L, 73L, 46L, 70L, 103L, 273L, 23L, 271L, 47L, 74L, 46L,
57L, 308L, 23L, 1L, 66L, 338L, 242L, 155L, 72L, 249L, 57L, 80L,
94L, 225L, 341L, 104L, 1L, 116L, 44L, 5L, 90L, 293L, 50L, 238L,
84L, 46L, 216L, 46L, 278L, 325L, 57L, 314L, 48L, 108L, 44L, 50L,
59L, 1L, 289L, 49L, 42L, 295L, 53L, 304L, 3L, 49L, 59L, 1L, 74L,
15L, 84L, 26L, 244L, 129L, 80L, 82L, 158L, 126L, 45L, 79L, 65L,
344L, 258L, 106L, 81L, 75L, 95L, 4L, 144L, 140L, 25L, 83L, 76L,
1L, 209L, 99L, 42L, 79L, 312L, 1L, 27L, 238L, 76L, 70L, 119L,
115L, 69L, 305L, 1L, 318L, 139L, 187L, 84L, 67L, 150L, 44L, 131L,
73L, 172L, 123L, 57L, 73L, 251L, 1L, 122L, 254L, 28L, 5L, 4L,
4L, 4L, 6L, 156L, 247L, 88L, 248L, 81L, 78L, 42L, 258L, 87L,
165L, 50L, 173L, 80L, 98L, 41L, 72L, 108L, 102L, 48L, 74L, 102L,
223L, 103L, 48L, 5L, 5L, 67L, 98L, 226L, 104L, 57L, 4L, 3L, 3L,
2L, 72L, 98L, 228L, 105L, 48L, 3L, 4L, 4L, 4L, 78L, 94L, 234L,
103L, 2L, 26L, 3L, 3L, 4L, 4L, 3L, 3L, 60L, 98L, 84L, 218L, 11L,
45L, 1L, 158L, 175L, 95L, 4L, 65L, 112L, 311L, 216L, 146L, 1L,
131L, 217L, 55L, 302L, 60L, 202L, 66L, 56L, 167L, 79L, 179L,
142L, 2L, 54L, 145L, 1L, 196L, 60L, 70L, 118L, 23L, 91L, 42L,
197L, 50L, 67L, 77L, 83L, 44L, 66L, 140L, 44L, 83L, 337L, 1L,
52L, 319L, 1L, 47L, 62L, 50L, 234L, 81L, 57L, 254L, 56L, 61L,
45L, 232L, 80L, 57L, 253L, 121L, 234L, 77L, 51L, 241L, 49L, 63L,
44L, 234L, 81L, 54L, 250L, 2L, 1L, 153L, 338L, 107L, 141L, 68L,
55L, 128L, 225L, 100L, 171L, 193L, 194L, 160L, 338L, 112L, 139L,
3L, 68L, 2L, 65L, 130L, 49L, 139L, 1L, 50L, 310L, 76L, 279L,
141L, 1L, 1L, 74L, 251L, 204L, 1L, 217L, 84L, 198L, 43L, 54L,
298L, 268L, 26L, 208L, 49L, 51L, 297L, 226L, 92L, 199L, 42L,
54L, 299L, 267L, 26L, 199L, 31L, 58L, 298L, 218L, 96L, 203L,
43L, 58L, 299L, 267L, 288L, 25L, 183L, 42L, 315L, 92L, 199L,
32L, 56L, 300L, 1L, 105L, 103L, 96L, 90L, 76L, 248L, 290L, 294L,
274L, 94L, 56L, 133L, 136L, 72L, 67L, 67L, 115L, 244L, 174L,
112L, 84L, 68L, 143L, 240L, 1L, 220L, 147L, 209L, 117L, 157L,
321L, 113L, 138L, 13L, 154L, 118L, 139L, 111L, 55L, 1L, 102L,
51L, 80L, 208L, 241L, 70L, 96L, 80L, 259L, 103L, 70L, 223L, 326L,
57L, 285L, 53L, 324L, 67L, 255L, 105L, 221L, 105L, 76L, 53L,
54L, 83L, 84L, 247L, 91L, 68L, 106L, 30L, 324L, 59L, 65L, 296L,
79L, 20L, 79L, 301L, 52L, 70L, 1L, 135L, 245L, 53L, 217L, 247L,
83L, 75L, 2L, 78L, 242L, 192L, 249L, 56L, 77L, 99L, 82L, 75L,
65L, 241L, 6L, 63L, 112L, 82L, 80L, 49L, 239L, 191L, 101L, 1L,
14L, 222L, 71L, 74L, 98L, 215L, 325L, 282L, 117L, 325L, 226L,
97L, 91L, 92L, 57L, 72L, 246L, 1L, 211L, 53L, 70L, 131L, 239L,
237L, 217L, 53L, 70L, 93L, 242L, 329L, 43L, 92L, 104L, 127L,
50L, 306L, 328L, 256L, 76L, 49L, 280L, 1L, 201L, 103L, 73L, 4L,
4L, 4L, 242L, 125L), .Label = c("#VALUE!", "0,1", "0,2", "0,3",
"0,4", "0,5", "0,6", "0,8", "1,0", "1,3", "10,1", "10,2", "10,3",
"10,4", "10,5", "10,8", "10,9", "1023,7", "107,5", "108,5", "108,9",
"109,2", "11,0", "11,1", "11,2", "11,3", "11,4", "11,5", "11,6",
"11,7", "11,8", "11,9", "110,1", "110,2", "110,4", "110,7", "110,8",
"110,9", "111,1", "115,8", "12,0", "12,1", "12,2", "12,3", "12,4",
"12,5", "12,6", "12,7", "12,8", "12,9", "13,0", "13,1", "13,2",
"13,3", "13,4", "13,5", "13,6", "13,7", "13,8", "13,9", "132,9",
"133,8", "133,9", "138,1", "14,0", "14,1", "14,2", "14,3", "14,4",
"14,5", "14,6", "14,7", "14,8", "14,9", "15,0", "15,1", "15,2",
"15,3", "15,4", "15,5", "15,6", "15,7", "15,8", "15,9", "156,1",
"1569,2", "157,3", "157,6", "158,5", "16,0", "16,1", "16,2",
"16,3", "16,4", "16,5", "16,6", "16,7", "16,8", "16,9", "162,4",
"165,6", "17,0", "17,1", "17,2", "17,3", "17,4", "17,5", "17,6",
"17,7", "17,8", "17,9", "18,0", "18,2", "18,3", "18,4", "18,5",
"18,6", "18,7", "18,8", "18,9", "180,9", "181,4", "19,0", "19,1",
"19,3", "19,4", "19,5", "19,6", "19,7", "19,8", "19,9", "2,1",
"2,3", "2,5", "2,7", "2,8", "20,0", "20,1", "20,3", "20,5", "20,7",
"208,1", "21,0", "21,1", "21,3", "21,5", "21,6", "21,8", "2101,8",
"22,0", "22,1", "22,4", "22,5", "22,6", "225,8", "23,1", "23,2",
"23,3", "23,6", "23,9", "24,3", "24,9", "25,3", "25,4", "253,7",
"254,9", "255,3", "26,9", "27,7", "277,6", "278,9", "279,3",
"28,0", "288,3", "29,6", "297,3", "299,6", "3,2", "3,3", "3,8",
"30,3", "303,8", "32,1", "32,4", "32,5", "32,6", "32,7", "32,9",
"322,0", "322,2", "326,4", "326,7", "328,9", "329,4", "33,1",
"33,2", "33,3", "33,4", "33,5", "33,6", "33,7", "33,8", "33,9",
"331,4", "34,0", "34,4", "34,5", "34,7", "34,9", "35,0", "35,3",
"35,5", "35,6", "35,7", "35,8", "35,9", "36,0", "36,1", "36,2",
"36,3", "36,4", "36,5", "36,6", "36,7", "36,8", "36,9", "37,0",
"37,1", "37,2", "37,3", "37,4", "37,5", "37,6", "37,8", "37,9",
"370,8", "375,6", "38,0", "38,1", "38,2", "38,3", "38,4", "38,5",
"38,6", "38,7", "38,8", "38,9", "39,0", "39,1", "39,2", "39,3",
"39,4", "39,6", "39,7", "39,8", "39,9", "396,2", "40,1", "40,2",
"40,3", "40,7", "40,9", "41,3", "41,4", "41,6", "41,7", "41,8",
"41,9", "42,7", "42,8", "421,0", "43,5", "43,7", "433,0", "44,2",
"446,1", "448,9", "449,3", "45,4", "45,8", "47,7", "472,8", "49,2",
"492,3", "494,9", "497,9", "507,6", "520,8", "54,4", "541,6",
"575,4", "577,4", "6,6", "6,8", "60,0", "61,2", "61,4", "61,5",
"61,6", "61,7", "61,9", "619,1", "62,3", "62,9", "63,0", "63,5",
"64,0", "64,1", "64,6", "642,8", "646,2", "65,8", "663,5", "666,2",
"671,6", "7,2", "7,8", "708,7", "711,6", "8,1", "8,2", "8,3",
"8,5", "8,7", "8,8", "8,9", "804,0", "829,0", "856,0", "87,0",
"87,4", "87,5", "88,1", "88,3", "9,0", "9,1", "9,3", "9,5", "9,7",
"9,8", "9,9", "90,9", "928,2", "975,8"), class = "factor")), .Names = "HOURS.at.sea", class = "data.frame", row.names = c(NA,
-913L))
If the commas are thousands separators, a common problem when users save a formatted Excel spreadsheet as *.csv and then try to import it into R, then you have two problems. Commas are not allowed in numbers in R (or they are interpreted as decimal point - depends on your locale setting) so, e.g. as.numeric("1,000") will return NA, not 1000. You have to get rid of the commas and also convert from factor to numeric.
a <- factor(c("10","20","30","40","50"))
as.numeric(a) # returns the factor codes!!
# [1] 1 2 3 4 5
as.numeric(as.character(a)) # returns the factor levels, as numeric
# [1] 10 20 30 40 50
b <- factor(c(10,20,30,40,50))
as.numeric(b) # returns the factor codes!!
# [1] 1 2 3 4 5
c <- factor(c("1,000","2,000","3,000","4,000","5,000"))
as.numeric(c) # returns the factor codes
# [1] 1 2 3 4 5
as.numeric(as.character(c)) # returns NA - commas are NOT allowed in numbers in R
# [1] NA NA NA NA NA
# Warning message:
# NAs introduced by coercion
as.numeric(gsub(",","",c,fixed=TRUE))
# [1] 1000 2000 3000 4000 5000
In the last line, gsub(",","",c,fixed=TRUE) just removes the commas.
EDIT
As #CarlWitthoft points out in the comment, if the commas are decimal separators, you can import the data using:
df <- read.csv("mydata.csv", dec=",")
This will avoid a lot of problems later. Given the (already imported) dataset you present in the question, this will fix it:
datatest$HOURS.at.sea <- as.numeric(gsub(",",".",datatest$HOURS.at.sea,fixed=TRUE))
This replaces the "," with ".". Since gsub(...) returns a character vector, not a factor, you can use as.numeric(...) directly on that. Notice that you still get some NAs, because some of the rows have "#VALUE!" - looks like an Excel dump.