I have computed the distance (Bray-Curtis dissimilarity) of a dataset, and I am trying to visualize the result in a heatmap with package pheatmap. I succeed to have the heatmap. Nevertheless, the ID of the rows and columns, despite being present in the distance data, does not appear in the heat map which complexify the interpretation. I have tried to use annotation = & annotation_row = on a vector file of the extracted species, but that does not work. Giving that message:
Error in annotation_row[rownames(mat), , drop = F] :
incorrect number of dimensions
Here is my code :
pheatmap(SpeBioDiss, annotation_row = Species, display_numbers = TRUE, cutree_rows = 5)
Here is my distance result:
SpeBioDiss <- structure(c(0.827283062404109, 0.999999999787744, 0.9976722025141,
0.993374122995813, 0.998684926724875, 0.996197694728471, 0.989724955190119,
0.43469497244591, 0.998418051673928, 0.990491388512666, 0.988338518981963,
0.989979469784687, 0.879991761967327, 0.737830539150324, 0.999999999543069,
0.96686014238935, 0.999999999537163, 0.977779088460779, 0.971844000006402,
0.999999999910839, 0.998794230920093, 0.999999999860431, 0.954480706731319,
0.986383026735704, 0.999999999929866, 0.912568317170688, 0.989047686370974,
0.989834812440211, 0.995315319366, 0.989609248520695, 0.928510389713861,
0.998057874156962, 0.987854549530747, 0.992809949172826, 0.997407926107517,
0.909702450081414, 0.899798177145296, 0.99999999991479, 0.95554497428788,
0.999999999914587, 0.981235337313036, 0.967860370501579, 0.999999999951837,
0.971870341370678, 0.999999999940163, 0.980228806939195, 0.928365081882989,
0.999999999936412, 0.989205204788684, 0.984802956273953, 0.990965089341073,
0.990997808073696, 0.998899563434765, 0.868730864585977, 0.97270276778642,
0.796523461858723, 0.893406508131487, 0.859623131053019, 0.97751172307757,
0.992352942466323, 0.99270915413726, 0.999999999607947, 0.9508032302371,
0.960255464055028, 0.647809134982572, 0.965799875755295, 0.906196625100659,
0.881340898467892, 0.986324697364517, 0.984173284697104, 0.961678344499017,
0.97986620519704, 0.98690337782425, 0.999999999932508, 0.989623321891195,
0.944514676798707, 0.958806930494846, 0.98866095752338, 0.95156505020099,
0.996130071685143, 0.999247798044378, 0.965322769139301, 0.999999999924102,
0.978266121630812, 0.92761939748347, 0.999999999955018, 0.944624365903456,
0.999999999944994, 0.999041093040236, 0.927062571232258, 0.486062169541527,
0.593141761296058, 0.516292483313821, 0.99118348278939, 0.996783394765032,
0.619795864489088, 0.982232130897591, 0.992734309081685, 0.924598423484611,
0.974445210104049, 0.99982611778834, 0.69835963101293, 0.999999999996886,
0.709436124371844, 0.78995367441768, 0.971643515325166, 0.837308356943407,
0.980872135495413, 0.924572400917035, 0.653481146200731, 0.591810902482557,
0.672035195833418, 0.996330790132122, 0.997137152915808, 0.635984964998261,
0.981345155269224, 0.994376102065022, 0.930677171351289, 0.983201791967876,
0.999832370468777, 0.764184967748081, 0.999999999996999, 0.7045992031759,
0.812823313491083, 0.949771727368739, 0.834752963315194, 0.970400438275127,
0.925270479615979, 0.558014389501429, 0.672194628479746, 0.995449343202001,
0.998108902874361, 0.622403790473656, 0.987148325574506, 0.995901444878825,
0.954611105456894, 0.980411108141993, 0.999900646342368, 0.82052818896192,
0.999999999998221, 0.781202000345684, 0.847354085250656, 0.96829041721517,
0.853866125723307, 0.982350607718347, 0.938086774654263, 0.719471914702451,
0.992095819587618, 0.997430216274891, 0.809335312202303, 0.978762423998145,
0.994742394681612, 0.938296212728146, 0.97033221807679, 0.999864946331165,
0.639941090476378, 0.999999999997582, 0.719176255540318, 0.894340516954927,
0.975714604352471, 0.915544830974035, 0.988696122154319, 0.94535527415508,
0.649943501040855, 0.949697309110096, 0.990413748324289, 0.997139520382115,
0.984287631185666, 0.979522989853084, 0.590405670464466, 0.996419977905822,
0.972889572022567, 0.999999999390479, 0.968075724489297, 0.967233155902039,
0.999690652287721, 0.999835684664338, 0.998603120221952, 0.940022477459311,
0.989643067553156, 0.995045900529729, 0.882175397694128, 0.892273666456609,
0.937617479767168, 0.931552968844194, 0.918863522176468, 0.991128211890621,
0.999999998120205, 0.989542335192327, 0.993984904250088, 0.972031567351805,
0.991313450355957, 0.994205794688569, 0.977396556368122, 0.996629984503012,
0.967580146059101, 0.989476190216704, 0.890942743652863, 0.983152745974521,
0.999697074109512, 0.847897990098422, 0.999999999994575, 0.705634125835866,
0.605802301755415, 0.906351449109105, 0.695673878775299, 0.959063591120359,
0.921666363807104, 0.820618619072126, 0.865695201384915, 0.693116070958152,
0.971577127295237, 0.988542799582319, 0.93825601292248, 0.99999999978303,
0.912834299792486, 0.935523776828192, 0.892721796075054, 0.930289058045599,
0.897969222994677, 0.913359438526875, 0.973302516149733, 0.857882783195953,
0.990098891491555, 0.993017292621689, 0.982833891711216, 0.999999999145984,
0.977110625992913, 0.970718120565762, 0.965561655724669, 0.972927823056258,
0.973267020957655, 0.975663721930446, 0.99374623773649, 0.909536833391596,
0.99612122887274, 0.824680942705843, 0.999999999930411, 0.77252723021654,
0.811959026265691, 0.874889109841753, 0.81246377408837, 0.935785119858506,
0.786391028551874, 0.926360731578736, 0.997149871048469, 0.900874359222991,
0.999999999839384, 0.885658873115255, 0.965819298805294, 0.997589524147627,
0.99782220913215, 0.996929289000309, 0.822818168192554, 0.961257329845712,
0.999532349794741, 0.999999964187906, 0.999447988081021, 0.999240832367375,
0.997829075841445, 0.999171094979177, 0.997808550561867, 0.998284898285629,
0.999822823307865, 0.999999999991625, 0.462533091236181, 0.827340176688411,
0.990451540594486, 0.875073699833685, 0.981300451114506, 0.876691869899626,
0.635503894157547, 0.999999999990113, 0.999999999983617, 0.999999999889566,
0.999999999985152, 0.999999999800174, 0.99999999995788, 0.999999999996827,
0.743669325337782, 0.879399730516722, 0.717970246714169, 0.918754919809119,
0.79311340763433, 0.632769318134201, 0.935167760442773, 0.643795000909155,
0.974723602282508, 0.936924401954657, 0.874128264084042, 0.885248041356563,
0.879319154156419, 0.871935806863086, 0.963919209341847, 0.91254396820454,
0.882664703372587, 0.869095793241582, 0.938704426640601, 0.978376838321269,
0.87876180016034), maxdist = 1, Size = 25L, Labels = c("Acartia longiremis",
"Acartia spp.", "Bradyidius similis", "Calanoida indet.", "Calanus finmarchicus",
"Calanus glacialis", "Calanus hyperboreus", "Calanus spp.", "Centropages spp.",
"Copepoda", "Metridia longa", "Microcalanus pusillus", "Microcalanus pygmaeus",
"Microcalanus spp.", "Microsetella norvegica", "Oithona atlantica",
"Oithona similis", "Oithona spp.", "Oncaea borealis", "Pareuchaeta glacialis",
"Pareuchaeta norvegica", "Pareuchaeta spp.", "Pseudocalanus acuspes",
"Pseudocalanus minutus", "Pseudocalanus spp."), Diag = FALSE, Upper = FALSE, method = "bray", call = vegdist(x = Zoodata_Biom_DisSpecies,
method = "bray"), class = "dist")
The vector file, containing the extraction of the species from the dataset :
Species <- c("Acartia longiremis", "Acartia spp.", "Bradyidius similis",
"Calanoida indet.", "Calanus finmarchicus", "Calanus glacialis",
"Calanus hyperboreus", "Calanus spp.", "Centropages spp.", "Copepoda",
"Metridia longa", "Microcalanus pusillus", "Microcalanus pygmaeus",
"Microcalanus spp.", "Microsetella norvegica", "Oithona atlantica",
"Oithona similis", "Oithona spp.", "Oncaea borealis", "Pareuchaeta glacialis",
"Pareuchaeta norvegica", "Pareuchaeta spp.", "Pseudocalanus acuspes",
"Pseudocalanus minutus", "Pseudocalanus spp.")
I'm trying to calculate the distance between different points that are a subset from a data.frame.
The method I try to use is distHaversine from library geosphere.
I have my data in var st and try using the function such as:
distHaversine(st[1,c(3,2)],st[2,c(3,2)])
Where each row in st is an entry of a new place, column 3 is longitude and column 2 is latitude.
Using the function in this manner gives me the following error:
Error in .pointsToMatrix(p1) * toRad :
non-numeric argument to binary operator
Reading the error something like:
distHaversine(as.double(st[1,c(3,2)]),as.double(st[2,c(3,2)]))
sounds like it should solve the problem but that just gives med another error:
Error in .pointsToMatrix(p1) : latitude > 90
If I manually add the lat and long in to the function such as:
distHaversine(c(12.6959,60.3097),c(12.6959,60.3097))
It works and returns 0.
The output of st[1,c(3,2)] and st[2,c(3,2)] is the following:
longitude latitude
1 12.6959 60.3097
longitude latitude
2 12.6959 60.3097
Output of dput(st[1:2, 2:3]):
structure(list(latitude = structure(c(422L, 422L), .Label = c(" latitude",
"55.3376", "55.3836", "55.3838", "55.384", "55.3872", "55.4114",
"55.441", "55.47411", "55.4889", "55.4907", "55.5", "55.5231",
"55.5449", "55.597", "55.6048", "55.6049", "55.6093", "55.6333",
"55.6402", "55.65", "55.6646", "55.693", "55.7", "55.7142", "55.7833",
"55.8633", "55.8666", "55.8798", "55.9", "55.9033", "55.9081",
"55.9245", "55.9557", "55.9833", "56.0131", "56.0133", "56.02391",
"56.0304", "56.0419", "56.0431", "56.05", "56.0666", "56.0699",
"56.07", "56.0752", "56.1333", "56.1498", "56.1813", "56.1957",
"56.1977", "56.2", "56.2182", "56.2287", "56.2451", "56.2619",
"56.2799", "56.2979", "56.3012", "56.3666", "56.38", "56.40404",
"56.4495", "56.45", "56.4595", "56.4782", "56.5124", "56.5177",
"56.5256", "56.5376", "56.5546", "56.5684", "56.601", "56.6166",
"56.6333", "56.66694", "56.67", "56.6737", "56.675", "56.6784",
"56.6833", "56.7154", "56.727", "56.7429", "56.7486", "56.7849",
"56.823", "56.8311", "56.8403", "56.8427", "56.8464", "56.8526",
"56.8666", "56.8865", "56.8994", "56.9213", "56.9222", "56.9252",
"56.9303", "56.931", "56.9496", "56.9628", "56.9835", "57.0333",
"57.0666", "57.0735", "57.1084", "57.1088", "57.1136", "57.11667458",
"57.121", "57.1461", "57.1462", "57.1484", "57.1834", "57.1978",
"57.2001", "57.2166", "57.2323", "57.2438", "57.253", "57.2577",
"57.2674", "57.2833", "57.2847", "57.288", "57.2893", "57.3025",
"57.304", "57.3672", "57.3689", "57.3848", "57.3908", "57.3915",
"57.4034", "57.4042", "57.4166", "57.4208", "57.4413", "57.45",
"57.4666", "57.4833", "57.485", "57.4983", "57.4998", "57.54361",
"57.5571", "57.5653", "57.5666", "57.596", "57.6295", "57.6324",
"57.636", "57.642", "57.6431", "57.6468", "57.661", "57.6614",
"57.6678", "57.6687", "57.6898", "57.7071", "57.7084", "57.7166",
"57.7213", "57.722", "57.7242", "57.75", "57.7514", "57.7611",
"57.7666", "57.7786", "57.7848", "57.7865", "57.8052", "57.8232",
"57.8305", "57.8425", "57.8429", "57.85", "57.8701", "57.8748",
"57.8863", "57.8887", "57.8987", "57.9166", "57.9167", "57.93309233",
"57.936", "57.9666", "58.0067", "58.0166", "58.0201", "58.03",
"58.0333", "58.0389", "58.0492", "58.0582", "58.0718", "58.0729",
"58.0788", "58.0794", "58.0908", "58.094", "58.0944", "58.0951",
"58.1425", "58.1584", "58.1589", "58.19969", "58.19981831", "58.2",
"58.2148", "58.2159", "58.2212", "58.2333", "58.2502", "58.2505",
"58.2662", "58.288", "58.2886", "58.3", "58.3141", "58.3221",
"58.3331352", "58.3333", "58.3339", "58.3476", "58.355", "58.3552",
"58.3571", "58.37", "58.38", "58.3833", "58.3928", "58.3935",
"58.3943", "58.3949", "58.4", "58.4004", "58.4072", "58.4166",
"58.4274", "58.4358", "58.4397", "58.4447", "58.4529", "58.4666",
"58.472", "58.5166", "58.5185", "58.5289", "58.5491", "58.55",
"58.5586", "58.5707", "58.5719", "58.5763", "58.5833", "58.5842",
"58.5929", "58.60033", "58.606", "58.6068", "58.6082", "58.6277",
"58.6336", "58.6493", "58.65", "58.66076", "58.6777", "58.6894",
"58.6998", "58.7104", "58.7136", "58.7165", "58.7333", "58.7421",
"58.7433", "58.7588", "58.7833", "58.78468", "58.7904", "58.7906",
"58.7936", "58.8", "58.8055", "58.8063", "58.8333", "58.85",
"58.8509", "58.8772", "58.88", "58.8925", "58.898", "58.9", "58.904",
"58.9333", "58.9334", "58.9502", "58.9671", "58.9733", "58.9817",
"59.0185", "59.0297", "59.05", "59.0542", "59.0561", "59.0666",
"59.0667", "59.0688", "59.08304351", "59.0833", "59.0966", "59.1166",
"59.1412", "59.1469", "59.1547", "59.1789", "59.182", "59.1833",
"59.2032", "59.2184", "59.2217", "59.2289", "59.2333", "59.2334",
"59.2361", "59.2448", "59.2511", "59.2661", "59.2671", "59.2782",
"59.2833", "59.2891", "59.2901", "59.2996", "59.3", "59.3107",
"59.3226", "59.342", "59.3421", "59.35", "59.3537", "59.3582",
"59.361", "59.3619", "59.3658", "59.3832", "59.3833", "59.384",
"59.3867", "59.3868", "59.4", "59.402", "59.4166", "59.4428",
"59.4445", "59.4446", "59.5052", "59.5171", "59.5345", "59.55",
"59.5737", "59.5833", "59.5976", "59.6072", "59.6112", "59.6166",
"59.6269", "59.6396", "59.6557", "59.6575", "59.6616", "59.6658",
"59.6747", "59.6833", "59.7166", "59.7497", "59.75", "59.7506",
"59.7534", "59.8085", "59.8139", "59.8321", "59.8333", "59.85",
"59.8524", "59.8585", "59.8586", "59.8633", "59.8644", "59.8707",
"59.8709", "59.88", "59.8953", "59.9045", "59.9098", "59.9268",
"59.9445", "59.95", "60.0666", "60.0991", "60.1075", "60.1197",
"60.12295", "60.1419", "60.1426", "60.1514", "60.1538", "60.1595",
"60.169", "60.175", "60.2", "60.2026", "60.2357", "60.2363",
"60.2688", "60.2761", "60.2788", "60.3", "60.3097", "60.3328",
"60.3333", "60.3548", "60.3758", "60.3822", "60.4294", "60.4393",
"60.4551", "60.4889", "60.5", "60.502", "60.5074", "60.5262",
"60.5333", "60.542", "60.55", "60.6046", "60.6103", "60.619",
"60.651", "60.6537", "60.6717", "60.6773", "60.7004", "60.7166",
"60.7256", "60.7304", "60.7547", "60.75686", "60.8321", "60.88",
"60.8846", "60.9066", "60.923", "60.9607", "60.962", "60.9645",
"60.9676", "61.0025", "61.0442", "61.0666", "61.1269", "61.15829",
"61.1666", "61.1691", "61.1766", "61.2542", "61.2555", "61.2691",
"61.2797", "61.3198", "61.3613", "61.3784", "61.3882", "61.395",
"61.4446", "61.5", "61.55", "61.6022", "61.6239", "61.6408",
"61.6577", "61.6606", "61.6912", "61.7027", "61.7033", "61.7111",
"61.7167", "61.7854", "61.8271", "61.8333", "61.85", "61.8526",
"61.8547", "61.8691", "61.8892", "62.0166", "62.0291", "62.0424",
"62.0471", "62.0943", "62.0991", "62.1132", "62.1299", "62.1809",
"62.2207", "62.2314", "62.26317", "62.3809", "62.4066", "62.4112",
"62.4172", "62.43064", "62.4502", "62.4943", "62.4961", "62.5",
"62.5165", "62.5166", "62.5246", "62.5335", "62.5493", "62.5772",
"62.6183", "62.628", "62.6431", "62.65", "62.72909", "62.7503",
"62.7512", "62.7557", "62.7594", "62.76291", "62.8016", "62.8108",
"62.8166", "62.8485", "62.9948", "63.0335", "63.044", "63.0447",
"63.05", "63.0502", "63.0509", "63.0796", "63.1251", "63.1333",
"63.1466", "63.1521", "63.1618", "63.16629", "63.1667", "63.1736",
"63.1864", "63.1885", "63.1886", "63.1947", "63.1974", "63.2443",
"63.2631", "63.2831", "63.3028", "63.30543", "63.3158", "63.3166",
"63.3193", "63.3266", "63.3269", "63.37375", "63.3806", "63.4",
"63.4119", "63.4304", "63.4359", "63.4608", "63.5739", "63.5949",
"63.595", "63.6198", "63.6513", "63.6574", "63.677", "63.6849",
"63.6968", "63.7", "63.73", "63.7606", "63.7695", "63.7788",
"63.7947", "63.8082", "63.8129", "63.8302", "63.8395", "63.8548",
"63.8683", "63.9187", "63.925", "63.9515", "63.9687", "63.9812",
"63.9967", "64.0432", "64.0753", "64.0763", "64.0885", "64.1236",
"64.159", "64.1661", "64.1973", "64.2", "64.2182", "64.2684",
"64.2734", "64.3636", "64.4309", "64.43583", "64.4502", "64.4542",
"64.4571", "64.4806", "64.4812", "64.503", "64.5057", "64.5059",
"64.5492", "64.5666", "64.5669", "64.5809", "64.5871", "64.6244",
"64.6389", "64.7166", "64.7306", "64.73431", "64.75", "64.7507",
"64.7584", "64.7627", "64.8666", "64.9093", "64.9112", "64.9263",
"64.95", "64.96782", "65.0074", "65.0106", "65.0251", "65.0333",
"65.035", "65.0464", "65.062", "65.0706", "65.0942", "65.1049",
"65.1089", "65.1317", "65.1522", "65.1808", "65.23", "65.2365",
"65.2391", "65.25", "65.278", "65.2984", "65.3131", "65.3156",
"65.3235", "65.328", "65.33", "65.3373", "65.4166", "65.5085",
"65.53", "65.5354", "65.5374", "65.5434", "65.5592", "65.5762",
"65.5953", "65.62", "65.6702", "65.6772", "65.68", "65.6935",
"65.6982", "65.7", "65.7166", "65.7306", "65.75272", "65.7666",
"65.7832", "65.7968", "65.8", "65.8077", "65.8151", "65.8211",
"65.8249", "65.8333", "65.8367", "65.85", "65.8769", "65.8801",
"65.8849", "65.8952", "65.9166", "65.9446", "65.9744", "65.9867",
"66.0456", "66.0833", "66.1347", "66.263", "66.2801", "66.2833",
"66.3002", "66.3006", "66.32417", "66.3824", "66.3855", "66.3862",
"66.3888", "66.48", "66.48603", "66.499", "66.50502", "66.5333",
"66.5343", "66.5759", "66.6135", "66.6235", "66.6798", "66.6815",
"66.6932", "66.7265", "66.7374", "66.7529", "66.7586", "66.761",
"66.7666", "66.8107", "66.8542", "66.8876", "66.8888", "67.0919",
"67.1261", "67.1358", "67.1421", "67.1429", "67.1498", "67.1705",
"67.2049", "67.2101", "67.2564", "67.3094", "67.3376", "67.3833",
"67.3956", "67.4083", "67.4964", "67.5009", "67.6501", "67.6943",
"67.7261", "67.7262", "67.7315", "67.7615", "67.827", "67.8488",
"67.85", "67.8911", "67.9113", "67.9443", "68.04398", "68.0502",
"68.0745", "68.2201", "68.2833", "68.3555", "68.3557", "68.4217",
"68.4218", "68.4284", "68.4316", "68.4324", "68.4421", "68.4432",
"68.6777", "68.6779"), class = "factor"), longitude = structure(c(81L,
81L), .Label = c(" longitude", "11", "11.0062", "11.0333", "11.0685",
"11.2", "11.2166", "11.2276", "11.3333", "11.3348", "11.41775",
"11.5332", "11.5572", "11.5724", "11.6015", "11.6077", "11.6352",
"11.6528", "11.7833", "11.8075", "11.8824", "11.9038", "11.9064",
"11.92", "11.9321", "11.9333", "11.9541", "11.97", "11.9746",
"11.9858", "11.9939", "12.038", "12.0387", "12.0542", "12.078",
"12.1009", "12.1032", "12.107", "12.1246", "12.1253", "12.1454",
"12.15406", "12.1563", "12.1581", "12.1761", "12.1833", "12.1997",
"12.1999", "12.2223", "12.2689", "12.2741", "12.2775", "12.2963",
"12.3024", "12.3153", "12.3166", "12.3292", "12.3579", "12.3616",
"12.394", "12.4129", "12.41302", "12.454", "12.4798", "12.5438",
"12.547", "12.55", "12.5514", "12.5521", "12.5843", "12.591",
"12.6043", "12.6381", "12.647", "12.6491", "12.6702", "12.6739",
"12.6775", "12.6906", "12.6928", "12.6959", "12.6969", "12.7",
"12.7042", "12.7074", "12.7075", "12.7166", "12.7264", "12.7653",
"12.77226", "12.7758", "12.8129", "12.8166", "12.819", "12.8203",
"12.8226", "12.8332", "12.8333", "12.8441", "12.8538", "12.8542",
"12.8568", "12.8575", "12.8705", "12.8709", "12.9242", "12.9247",
"12.9333", "12.9359", "12.9414", "12.94361", "12.9493", "12.9834",
"12.9841", "12.9843", "12.9908", "13.0164", "13.0166", "13.0338",
"13.0406", "13.0595", "13.0607", "13.0625", "13.0666", "13.0667",
"13.0668", "13.0731", "13.0734", "13.0746", "13.0817", "13.0833",
"13.1", "13.1109", "13.11383", "13.1166", "13.1174", "13.1193",
"13.1238", "13.1247", "13.1296", "13.1554", "13.1589", "13.16067",
"13.1666", "13.1668", "13.18054263", "13.1865", "13.2152", "13.2166",
"13.229", "13.2318", "13.2369", "13.25", "13.2517", "13.2534",
"13.2593", "13.2666", "13.2833", "13.2841", "13.3157", "13.33",
"13.3374", "13.3508", "13.3563", "13.3608", "13.3787", "13.3826",
"13.3957", "13.3995", "13.4001", "13.4083", "13.4407", "13.4455",
"13.45", "13.4666", "13.4728", "13.4833", "13.5037", "13.5168",
"13.52", "13.5254", "13.5324", "13.5335", "13.5397", "13.55",
"13.5609", "13.6115", "13.6191", "13.6333", "13.6488", "13.6671",
"13.66833", "13.6689", "13.6748", "13.7058", "13.71", "13.7166",
"13.7251", "13.75", "13.7529", "13.78495", "13.7975", "13.7995",
"13.8019", "13.823", "13.8278", "13.8436", "13.8456", "13.85",
"13.87671", "13.88040497", "13.8822", "13.8833", "13.8907", "13.8985",
"13.9333", "13.9432", "13.948", "13.98", "13.986", "14.0375",
"14.0383", "14.0476", "14.0733", "14.0842", "14.0921", "14.1",
"14.1019", "14.10272", "14.1166", "14.1265", "14.1273", "14.1297",
"14.1455", "14.1465", "14.1571", "14.1666", "14.17", "14.1831",
"14.1833", "14.1847", "14.1859", "14.1947", "14.2", "14.2028",
"14.2238", "14.2354", "14.25", "14.2595", "14.2666", "14.2833",
"14.2874", "14.2896", "14.3166", "14.3172", "14.3188", "14.3508",
"14.3578", "14.3678", "14.3692", "14.3786", "14.3903", "14.3951",
"14.4", "14.401", "14.4086", "14.41", "14.4277", "14.4317", "14.4323",
"14.45", "14.451", "14.4666", "14.4672", "14.4693", "14.4863",
"14.4898", "14.507", "14.5085", "14.5126", "14.5535", "14.56389527",
"14.5722", "14.5874", "14.6063", "14.6079", "14.6333", "14.6701",
"14.6786", "14.6895", "14.6931", "14.7", "14.7166", "14.7333",
"14.7342", "14.7571", "14.7908", "14.8015", "14.8024", "14.8176",
"14.826", "14.8324", "14.8488", "14.85", "14.8517", "14.8744",
"14.8833", "14.8881", "14.891", "14.8913", "14.8986", "14.8987",
"14.9", "14.9019", "14.95", "14.9524", "14.9585", "14.97", "14.9853",
"14.986", "14.9943", "14.9948", "15.0146", "15.0167", "15.0206",
"15.03", "15.0455", "15.05", "15.0645", "15.0666", "15.0686",
"15.0854", "15.0876", "15.0929", "15.0984", "15.1045", "15.1145",
"15.12262", "15.1285", "15.1404", "15.1574", "15.164", "15.1694",
"15.2", "15.2184", "15.231", "15.2352", "15.2359", "15.2365",
"15.237", "15.25", "15.2544", "15.2553", "15.2686", "15.2742",
"15.2747", "15.2833", "15.2854", "15.3263", "15.331", "15.3666",
"15.3668", "15.3772", "15.37907", "15.3987", "15.418", "15.4324",
"15.4372", "15.45", "15.4606", "15.4655", "15.47", "15.5", "15.5079",
"15.5192", "15.5327", "15.5331", "15.5333", "15.5423", "15.55",
"15.5555", "15.5561", "15.5725", "15.5857", "15.5894", "15.5921",
"15.6235", "15.6332", "15.6333", "15.6603", "15.6666", "15.67093",
"15.6762", "15.6833", "15.6858", "15.7", "15.7077", "15.7175",
"15.7431", "15.7486", "15.7921", "15.8037", "15.8081", "15.8215",
"15.8232", "15.8245", "15.8307", "15.8333", "15.8352", "15.8518",
"15.8652", "15.9166", "15.9333", "15.9666", "15.9667", "15.9681",
"15.98018143", "15.9833", "15.9883", "15.9909", "16.0017", "16.0353",
"16.0581", "16.0833", "16.1088", "16.1137", "16.1166", "16.1212",
"16.1333", "16.15", "16.15439", "16.1667", "16.1689", "16.1733",
"16.1747", "16.1833", "16.1846", "16.2084", "16.2102", "16.2127",
"16.21345182", "16.2149", "16.2166", "16.2283", "16.2383", "16.2388",
"16.2662", "16.2774", "16.283", "16.2838", "16.2922", "16.2944",
"16.3062", "16.3103", "16.3125", "16.3141", "16.35", "16.3511",
"16.3666", "16.3667", "16.3709", "16.39", "16.3975", "16.401",
"16.4036", "16.4142", "16.4145", "16.4149", "16.41734", "16.4199",
"16.4295", "16.4311", "16.4549", "16.4585", "16.4609", "16.4631",
"16.4647", "16.4648", "16.4683", "16.5333", "16.5358", "16.5393",
"16.5418", "16.55", "16.5505", "16.5536", "16.5682", "16.5812",
"16.5828", "16.5833", "16.6326", "16.6607", "16.6799", "16.68",
"16.6833", "16.6875", "16.6919", "16.7114", "16.7337", "16.7387",
"16.7462", "16.7896", "16.8157", "16.8421", "16.8536", "16.8723",
"16.9166", "16.9168", "16.9531", "16.977", "16.9959", "17.0042",
"17.0095", "17.0106", "17.0262", "17.044", "17.05", "17.0605",
"17.0794", "17.086", "17.0879", "17.09442", "17.0967", "17.0983",
"17.0986", "17.1008", "17.1121", "17.1223", "17.1536", "17.1641",
"17.1693", "17.1748", "17.2", "17.2165", "17.2342", "17.2664",
"17.2802", "17.2854", "17.3188", "17.3333", "17.3409", "17.3447",
"17.3494", "17.3574", "17.40337", "17.4333", "17.4401", "17.441",
"17.4498", "17.4666", "17.4704", "17.4711", "17.4718", "17.5166",
"17.5225", "17.5244", "17.525", "17.5612", "17.5666", "17.5935",
"17.6186", "17.6252", "17.6253", "17.6333", "17.6469", "17.65",
"17.65431", "17.6623", "17.6666", "17.6677", "17.7034", "17.7085",
"17.7166", "17.7426", "17.7478", "17.75", "17.7658", "17.86",
"17.8671", "17.8716", "17.8833", "17.8974", "17.904", "17.9049",
"17.9072", "17.9078", "17.9125", "17.9167", "17.93", "17.9462",
"17.9471", "17.9513", "17.9545", "17.9623", "17.9972", "18.0159",
"18.0179", "18.0234", "18.0575", "18.0577", "18.0592", "18.0618",
"18.0929", "18.1", "18.1166", "18.1184", "18.1302", "18.1471",
"18.1541", "18.1666", "18.1698", "18.1719", "18.1794", "18.2222",
"18.2272", "18.2555", "18.2669", "18.2718", "18.2839", "18.2891",
"18.2943", "18.3061", "18.3308", "18.3428", "18.3656", "18.3725",
"18.3766", "18.3835", "18.3911", "18.419", "18.42533", "18.4333",
"18.4493", "18.4639", "18.5297", "18.5318", "18.55", "18.5502",
"18.5974", "18.6068", "18.6281", "18.6348", "18.6587", "18.6833",
"18.7047", "18.7091", "18.7156", "18.7169", "18.7296", "18.7431",
"18.7436", "18.8146", "18.8206", "18.8211", "18.8417", "18.8666",
"18.9011", "18.9174", "18.9179", "18.9227", "18.9236", "18.9505",
"18.9568", "18.9797", "18.9833", "18.9871", "19.0194", "19.0202",
"19.0233", "19.0532", "19.0611", "19.0929", "19.1298", "19.1592",
"19.1833", "19.197", "19.1975", "19.2", "19.2682", "19.3168",
"19.3782", "19.4087", "19.4746", "19.4833", "19.4875", "19.4976",
"19.5", "19.5058", "19.5059", "19.5666", "19.6314", "19.6673",
"19.67495", "19.6768", "19.6983", "19.6986", "19.7123", "19.7243",
"19.8316", "19.8392", "19.9", "20.02", "20.0993", "20.131", "20.1315",
"20.1543", "20.2124", "20.2333", "20.2397", "20.2747", "20.2901",
"20.2918", "20.3387", "20.3605", "20.4055", "20.424", "20.6333",
"20.6455", "20.6586", "20.6686", "20.7565", "20.7652", "20.7891",
"20.8144", "20.8692", "20.9", "20.9026", "20.9281", "20.9478",
"20.9666", "20.97", "20.9858", "21.0537", "21.0558", "21.0662",
"21.0717", "21.0846", "21.133", "21.15", "21.2263", "21.2666",
"21.27997", "21.3152", "21.4907", "21.52", "21.5274", "21.5277",
"21.55", "21.5655", "21.5666", "21.5787", "21.5791", "21.6098",
"21.6167", "21.6317", "21.6335", "21.6396", "21.75496", "21.76498",
"21.85", "21.9814", "22.109", "22.116", "22.1193", "22.1307",
"22.2178", "22.2334", "22.336", "22.3518", "22.3666", "22.3753",
"22.3961", "22.4488", "22.4502", "22.75", "22.8266", "22.8472",
"22.85", "22.9001", "23.0571", "23.0924", "23.1005", "23.2",
"23.3928", "23.3952", "23.4441", "23.4666", "23.468", "23.622",
"24.113", "24.1162"), class = "factor")), row.names = 1:2, class = "data.frame")
As #IceCreamToucan said, the issue is that your latitude and longitude are factor variables. To fix that, you may use
df$latitude <- as.numeric(as.character(df$latitude))
df$longitude <- as.numeric(as.character(df$longitude))
distHaversine(df[1,], df[2,])
# [1] 0
What's also important is why this happened. In particular, we have
levels(df$latitude)
# [1] " latitude" "55.3376" ...
So, there must be a row with " latitude" as a value in the latitude column. As a result, when loading the data this variable became a factor. Similarly,
levels(df$longitude)
# [1] " longitude" "11" ...
I'll take a wild guess that st is a matrix that includes textual data, therefore all data is coerced as char. try rebuilding your data object as data.frame (which allows multiple data types) and see if problem persists.
> st <- data.frame(name=c("A","B"), latitude = 12.6959, longitude = 60.3097)
> st
name latitude longitude
1 A 12.6959 60.3097
2 B 12.6959 60.3097
> geosphere::distHaversine(st[1,c(3,2)],st[2,c(3,2)])
[1] 0
except for that I agree with everything #julius-vainora has written.
Any help would be greatly appreciated!!
I'm trying to create a choropleth map in R that shows the counties of texas, color-coded by their population ranges.
My problem is that the range of populations is too large. The highest population is over 4 million, but most of the counties have a population under 50,000. The criteria for the fill is: (0-1mil), (1-2mil), (2-3mil), (3-4mil), (4-5mil) but almost all fall under 0-1mil.
How can I change the legend to account for different ranges of numbers? For example, maybe:
(0-1,000), (1,000-10,000), (10,000-100,000), (100,000-1mil), (1mil-5mil)
Here's the code I wrote to plot the data:
txplot <- ggplot(txczpop, aes(fill=pop2014)) + geom_map(txmap)
tm_shape(txmap) +
tm_fill("pop2014", title="TX County Population", palette = "PRGn") +
tm_borders(alpha=.5) +
tm_style_beaver()
Here's the result:
[![enter image description here][1]][1]
I'm using a census county shapefile and population also retrieved from a census file.
Here's the output of my population data:
txczpop <- structure(list(county_fips = c(48001L, 48003L, 48005L, 48007L,
48009L, 48011L, 48013L, 48015L, 48017L, 48019L, 48021L, 48023L,
48025L, 48027L, 48029L, 48031L, 48033L, 48035L, 48037L, 48039L,
48041L, 48043L, 48045L, 48047L, 48049L, 48051L, 48053L, 48055L,
48057L, 48059L, 48061L, 48063L, 48065L, 48067L, 48069L, 48071L,
48073L, 48075L, 48077L, 48079L, 48081L, 48083L, 48085L, 48087L,
48089L, 48091L, 48093L, 48095L, 48097L, 48099L, 48101L, 48103L,
48105L, 48107L, 48109L, 48111L, 48113L, 48115L, 48117L, 48119L,
48121L, 48123L, 48125L, 48127L, 48129L, 48131L, 48133L, 48135L,
48137L, 48141L, 48139L, 48143L, 48145L, 48147L, 48149L, 48151L,
48153L, 48155L, 48157L, 48159L, 48161L, 48163L, 48165L, 48167L,
48169L, 48171L, 48173L, 48175L, 48177L, 48179L, 48181L, 48183L,
48185L, 48187L, 48189L, 48191L, 48193L, 48195L, 48197L, 48199L,
48201L, 48203L, 48205L, 48207L, 48209L, 48211L, 48213L, 48215L,
48217L, 48219L, 48221L, 48223L, 48225L, 48227L, 48229L, 48231L,
48233L, 48235L, 48237L, 48239L, 48241L, 48243L, 48245L, 48247L,
48249L, 48251L, 48253L, 48255L, 48257L, 48259L, 48261L, 48263L,
48265L, 48267L, 48269L, 48271L, 48273L, 48275L, 48283L, 48277L,
48279L, 48281L, 48285L, 48287L, 48289L, 48291L, 48293L, 48295L,
48297L, 48299L, 48301L, 48303L, 48305L, 48313L, 48315L, 48317L,
48319L, 48321L, 48323L, 48307L, 48309L, 48311L, 48325L, 48327L,
48329L, 48331L, 48333L, 48335L, 48337L, 48339L, 48341L, 48343L,
48345L, 48347L, 48349L, 48351L, 48353L, 48355L, 48357L, 48359L,
48361L, 48363L, 48365L, 48367L, 48369L, 48371L, 48373L, 48375L,
48377L, 48379L, 48381L, 48383L, 48385L, 48387L, 48389L, 48391L,
48393L, 48395L, 48397L, 48399L, 48401L, 48403L, 48405L, 48407L,
48409L, 48411L, 48413L, 48415L, 48417L, 48419L, 48421L, 48423L,
48425L, 48427L, 48429L, 48431L, 48433L, 48435L, 48437L, 48439L,
48441L, 48443L, 48445L, 48447L, 48449L, 48451L, 48453L, 48455L,
48457L, 48459L, 48461L, 48463L, 48465L, 48467L, 48469L, 48471L,
48473L, 48475L, 48477L, 48479L, 48481L, 48483L, 48485L, 48487L,
48489L, 48491L, 48493L, 48495L, 48497L, 48499L, 48501L, 48503L,
48505L, 48507L), county_name = c("Anderson", "Andrews", "Angelina",
"Aransas", "Archer", "Armstrong", "Atascosa", "Austin", "Bailey",
"Bandera", "Bastrop", "Baylor", "Bee", "Bell", "Bexar", "Blanco",
"Borden", "Bosque", "Bowie", "Brazoria", "Brazos", "Brewster",
"Briscoe", "Brooks", "Brown", "Burleson", "Burnet", "Caldwell",
"Calhoun", "Callahan", "Cameron", "Camp", "Carson", "Cass", "Castro",
"Chambers", "Cherokee", "Childress", "Clay", "Cochran", "Coke",
"Coleman", "Collin", "Collingsworth", "Colorado", "Comal", "Comanche",
"Concho", "Cooke", "Coryell", "Cottle", "Crane", "Crockett",
"Crosby", "Culberson", "Dallam", "Dallas", "Dawson", "Deaf Smith",
"Delta", "Denton", "DeWitt", "Dickens", "Dimmit", "Donley", "Duval",
"Eastland", "Ector", "Edwards", "El Paso", "Ellis", "Erath",
"Falls", "Fannin", "Fayette", "Fisher", "Floyd", "Foard", "Fort Bend",
"Franklin", "Freestone", "Frio", "Gaines", "Galveston", "Garza",
"Gillespie", "Glasscock", "Goliad", "Gonzales", "Gray", "Grayson",
"Gregg", "Grimes", "Guadalupe", "Hale", "Hall", "Hamilton", "Hansford",
"Hardeman", "Hardin", "Harris", "Harrison", "Hartley", "Haskell",
"Hays", "Hemphill", "Henderson", "Hidalgo", "Hill", "Hockley",
"Hood", "Hopkins", "Houston", "Howard", "Hudspeth", "Hunt", "Hutchinson",
"Irion", "Jack", "Jackson", "Jasper", "Jeff Davis", "Jefferson",
"Jim Hogg", "Jim Wells", "Johnson", "Jones", "Karnes", "Kaufman",
"Kendall", "Kenedy", "Kent", "Kerr", "Kimble", "King", "Kinney",
"Kleberg", "Knox", "La Salle", "Lamar", "Lamb", "Lampasas", "Lavaca",
"Lee", "Leon", "Liberty", "Limestone", "Lipscomb", "Live Oak",
"Llano", "Loving", "Lubbock", "Lynn", "Madison", "Marion", "Martin",
"Mason", "Matagorda", "Maverick", "McCulloch", "McLennan", "McMullen",
"Medina", "Menard", "Midland", "Milam", "Mills", "Mitchell",
"Montague", "Montgomery", "Moore", "Morris", "Motley", "Nacogdoches",
"Navarro", "Newton", "Nolan", "Nueces", "Ochiltree", "Oldham",
"Orange", "Palo Pinto", "Panola", "Parker", "Parmer", "Pecos",
"Polk", "Potter", "Presidio", "Rains", "Randall", "Reagan", "Real",
"Red River", "Reeves", "Refugio", "Roberts", "Robertson", "Rockwall",
"Runnels", "Rusk", "Sabine", "San Augustine", "San Jacinto",
"San Patricio", "San Saba", "Schleicher", "Scurry", "Shackelford",
"Shelby", "Sherman", "Smith", "Somervell", "Starr", "Stephens",
"Sterling", "Stonewall", "Sutton", "Swisher", "Tarrant", "Taylor",
"Terrell", "Terry", "Throckmorton", "Titus", "Tom Green", "Travis",
"Trinity", "Tyler", "Upshur", "Upton", "Uvalde", "Val Verde",
"Van Zandt", "Victoria", "Walker", "Waller", "Ward", "Washington",
"Webb", "Wharton", "Wheeler", "Wichita", "Wilbarger", "Willacy",
"Williamson", "Wilson", "Winkler", "Wise", "Wood", "Yoakum",
"Young", "Zapata", "Zavala"), pop2014 = c(57627L, 17477L, 87750L,
24972L, 8811L, 1955L, 47774L, 29114L, 6910L, 20892L, 78069L,
3592L, 32863L, 329140L, 1855866L, 10812L, 652L, 17780L, 93275L,
338124L, 209152L, 9173L, 1536L, 7194L, 37653L, 17253L, 44943L,
39810L, 21797L, 13513L, 420392L, 12621L, 6013L, 30261L, 7781L,
38145L, 50902L, 7089L, 10370L, 2935L, 3254L, 8430L, 885241L,
3017L, 20719L, 123694L, 13550L, 4050L, 38761L, 75562L, 1415L,
4950L, 3812L, 5899L, 2266L, 7135L, 2518638L, 13372L, 19195L,
5238L, 753363L, 20684L, 2218L, 11089L, 3543L, 11533L, 18176L,
153904L, 1879L, 833487L, 159317L, 40147L, 16989L, 33752L, 24833L,
3831L, 5949L, 1275L, 685345L, 10600L, 19762L, 18531L, 19425L,
314198L, 6435L, 25520L, 1291L, 7549L, 20462L, 23044L, 123534L,
123204L, 27172L, 147250L, 34720L, 3147L, 8199L, 5509L, 3928L,
55621L, 4441370L, 67336L, 6089L, 5769L, 185025L, 4180L, 79290L,
831073L, 34848L, 23577L, 53921L, 35921L, 22741L, 36651L, 3211L,
88493L, 21773L, 1574L, 8855L, 14739L, 35552L, 2204L, 252235L,
5255L, 41353L, 157456L, 19936L, 14906L, 111236L, 38880L, 400L,
785L, 50562L, 4438L, 262L, 3526L, 32190L, 3858L, 7474L, 49523L,
13574L, 20156L, 19721L, 16742L, 16861L, 78117L, 23524L, 3553L,
12091L, 19510L, 86L, 293974L, 5771L, 13861L, 10149L, 5460L, 4071L,
36519L, 57023L, 8199L, 243441L, 805L, 47894L, 2147L, 155830L,
24256L, 4870L, 9076L, 19416L, 518947L, 22148L, 12743L, 1153L,
65301L, 48195L, 14138L, 15093L, 356221L, 10758L, 2070L, 83433L,
28096L, 23769L, 123164L, 9908L, 15893L, 46079L, 121627L, 6976L,
11032L, 128220L, 3755L, 3371L, 12446L, 14349L, 7302L, 928L, 16500L,
87809L, 10416L, 53923L, 10350L, 8610L, 27099L, 66915L, 5622L,
3162L, 17328L, 3343L, 25515L, 3084L, 218842L, 8694L, 62955L,
9405L, 1339L, 1403L, 3972L, 7581L, 1945360L, 135143L, 927L, 12739L,
1608L, 32506L, 116608L, 1151145L, 14224L, 21418L, 40354L, 3454L,
27117L, 48974L, 52910L, 91081L, 69789L, 46820L, 11625L, 34438L,
266673L, 41168L, 5714L, 132355L, 12973L, 21903L, 489250L, 46402L,
7821L, 61638L, 42852L, 8286L, 18350L, 14319L, 12267L)), .Names = c("county_fips",
"county_name", "pop2014"), row.names = c(5100L, 5101L, 5103L,
5106L, 5107L, 5109L, 5112L, 5114L, 5116L, 5118L, 5120L, 5121L,
5124L, 5126L, 5128L, 5129L, 5131L, 5133L, 5136L, 5137L, 5140L,
5141L, 5143L, 5146L, 5147L, 5150L, 5152L, 5153L, 5156L, 5158L,
5159L, 5161L, 5163L, 5166L, 5168L, 5170L, 5171L, 5174L, 5176L,
5178L, 5179L, 5182L, 5183L, 5185L, 5188L, 5190L, 5192L, 5194L,
5195L, 5198L, 5200L, 5201L, 5203L, 5205L, 5208L, 5209L, 5212L,
5214L, 5215L, 5218L, 5219L, 5221L, 5224L, 5226L, 5228L, 5230L,
5232L, 5233L, 5235L, 5239L, 5237L, 5242L, 5244L, 5245L, 5248L,
5249L, 5251L, 5254L, 5256L, 5257L, 5260L, 5261L, 5264L, 5265L,
5268L, 5270L, 5272L, 5274L, 5276L, 5278L, 5280L, 5281L, 5284L,
5286L, 5288L, 5290L, 5292L, 5293L, 5296L, 5298L, 5300L, 5301L,
5303L, 5306L, 5308L, 5309L, 5312L, 5314L, 5316L, 5317L, 5319L,
5321L, 5323L, 5326L, 5327L, 5330L, 5332L, 5334L, 5335L, 5337L,
5339L, 5341L, 5343L, 5346L, 5348L, 5349L, 5352L, 5354L, 5356L,
5357L, 5360L, 5362L, 5364L, 5365L, 5368L, 5369L, 5372L, 5374L,
5382L, 5376L, 5378L, 5379L, 5383L, 5385L, 5388L, 5390L, 5392L,
5394L, 5396L, 5398L, 5400L, 5401L, 5404L, 5412L, 5413L, 5416L,
5418L, 5419L, 5421L, 5406L, 5407L, 5409L, 5423L, 5425L, 5427L,
5429L, 5432L, 5434L, 5435L, 5438L, 5440L, 5442L, 5443L, 5446L,
5448L, 5449L, 5451L, 5453L, 5456L, 5457L, 5460L, 5461L, 5464L,
5465L, 5468L, 5470L, 5472L, 5474L, 5476L, 5477L, 5480L, 5482L,
5484L, 5486L, 5488L, 5489L, 5491L, 5494L, 5496L, 5498L, 5499L,
5501L, 5504L, 5505L, 5508L, 5510L, 5511L, 5514L, 5516L, 5518L,
5520L, 5522L, 5524L, 5526L, 5527L, 5530L, 5531L, 5533L, 5536L,
5537L, 5540L, 5542L, 5544L, 5546L, 5547L, 5550L, 5552L, 5554L,
5555L, 5558L, 5559L, 5562L, 5563L, 5566L, 5568L, 5569L, 5571L,
5574L, 5575L, 5578L, 5579L, 5582L, 5584L, 5585L, 5587L, 5590L,
5592L, 5594L, 5595L, 5598L, 5600L, 5602L, 5604L, 5606L), class = "data.frame")
I just created a new column in the population dataframe that summarizes the population based on the ranges that I want to use, and then use that as the criteria for the fill:
txczpop$poprange[txczpop$pop2014 >= 0 & txczpop < 1000] <- "0-1,000"
txczpop$poprange[txczpop$pop2014 >= 1000 & txczpop < 10000] <- "1-10,000"
txczpop$poprange[txczpop$pop2014 >= 10000 & txczpop$pop2014 < 100000] <- "10,000-100,000"
txczpop$poprange[txczpop$pop2014 >= 100000 & txczpop$pop2014 < 1000000] <- "100,000 - 1,000,000"
txczpop$poprange[txczpop$pop2014 >= 1000000 & txczpop$pop2014 <= 5000000] <- "1,000,000 - 5,000,000"