本文介绍了使用不同的颜色将多个数据与二维密度叠加到ggmap上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我觉得我一直在无休止地寻找解决方案,并找不到任何地方。基本上,我需要将彩色等高线贴图叠加到不同的图层上(使用不同的颜色)到ggmap上,并且无法让这一切适用于我的生活。我试图做的是采取在这里说明的例子:仅覆盖两个带有alpha通道的ggplot2 stat_density2d图,而不是将其映射到ggplot(),将其映射到ggmap()。我读过这里: ggmap与geom_map叠加那部分困难在于取消了从ggmap发送到ggplot的隐含美学,但是无论我做什么,我都会遇到一系列错误。我遇到的一些错误来自不承认'组'。使用geom_contour时,如果使用与以下相同的格式(z = X3),则会出现以下错误: contourLines中的错误(x = sort(unique(data $ x)),y = sort(unique(data $ y)),:没有指定适当的'z'矩阵 部分难点在于为变量指定不同的颜色。绑定列(如下面和上面链接的示例中所做的)并区分变量表单变量B in列X3在第四列'groups'中有一个a或b似乎没有用scale_color_manual()进行很好的注册。最后,我试图找到一种方法来为每个一个data.frame的列 - 任何想法也是有帮助的。 我已经包含了一个示例数据集和一个我的方法+错误 - 对此有帮助在任何情况下,将不胜感激(先感谢您的时间/考虑)!请让我知道,如果有什么e library(ggplot2) library(ggmap) $ b $由位置坐标位置< -c(-74.03990,40.52726,-73.68864,40.81141) map< - get_map(location = location,scale =auto,zoom = 1) map2 map2 + stat_density2d(data = d,geom =density2d,aes(x = d [,1],y = d [,2] ,z = d [,3],color = group,alpha = .. level ..),size = 2,contour = TRUE)+ scale_color_manual(values = c(a=#FF0000, b=#00FF00)) #接收到的错误:if(any(h 缺少TRUE / FALSE所需的值 #Dataset(d): d = read.table(header = TRUE,text = X1 X2 X3 group 40.7462 -73.71148 2291 a 40.7566 -73.71418 291 a 40.74715 -73.93975 54579 a 40.77288 -73.9263 4564 a 40.76257 -73.91345 7189 a 40.74463 -73.9202 3643 a 40.77888 -73.90677 8108 a 40.76221 -73.93153 7420 a 40.74512 -73.95693 9 a NA NA 0 a 40.78075-73.8253 9 a NA NA不适用138 a 40.76821 -73.8274 17733 a 40.75145 -73.82103 13321 a 40.78485 -73.84128 7769 a 40.78639 -73.81086 5970 a 40.76047 -73.79637 4045 a 40.79178 -73.77688 0 a 40.78038 -73.78123 2548 a 40.76419 -73.77277 6351 a 40.75657 -73.73784 4000 a 40.77262 -73.74653 1262 a 40.74529 -73.76059 2251 a 40.73963 -73.79449 4730 a 40.72815 -73.78502 2660 a 40.73014 -73.82703 3639 a 40.75172 -73.85182 8450 a 40.76336 -73.87237 3245 a 40.76539 -73.89324 3207 a 40.77389 -73.87348 11932 a 40.75169 -73.88364 6080 a 40.73884 -73.87853 14148 a 40.72642 -73.86153 7352 a 40.72093 -73.84615 15755 a 40.74482 -73.90516 13699 a 40.72474 -73.90964 19479 a 40.71675 -73.8796 3975 a 不适用11 a 40.70067 -73.88943 17790 a 不适用18 a 40.69402 -73.73622 790 a 40.6981 -73.75899 1104 a 40.67166 -73.75257 4920 a 40.6576 -73.8448 2669 a 40.70792 - 73.82821 3400 a 40.68465 -73.84955 3935 a 40.67645 -73.84444 3896 a 40.70027 -73.83597 7791 a 40.68867 -73.82292 2492 a 40.67358 -73.81773 2555 a 40.69406 -73.85863 2568 a 40.66006 -73.73601 2136 a 40.71561 -73.76847 2545 a 40.7143 -73.82726 29 a 40.60775 -74.02394 0 a 40.73642 -73.72238 2336 a 40.7309 -73.74566 2788 a 40.72102 -73.74224 3074 a 40.70977 -73.73865 1396 a 40.64696 -73.78481 36136 a NA NA 433 a 40.71536 -73.79307 20480 a 40.69816 -73.78689 4812 a 40.67681 -73.77643 10111 a 40.70126 -73.8096 5259 a 40.67581 -73.79662 734 a NA NA 0 a 40.70128 -73.79597 38 a NA NA 0 a 40.60128 -73.76165 8987 a 40.59409 -73.7929 1512 a 40.59069 -73.80975 785 a 40.57827 -73.84476 2206 a 不适用6 a 40.55569 -73.92066 246 a 40.7462 -73.71148 2662 a 40.7566 -73.71418 323 a 40.74715 -73.93975 57472 a 40.77288 -73.9263 6104 a 40.76257 -73.91345 10050 a 40.74463 -73.9202 5435 a 40.77888 -73.90677 8813 a 40.76221 -73.93153 9495 a 40.74512 -73.95693 104 a NA NA 987 a 40.78075 -73.8253 0 a NA NA 0 a 40.76821 -73.8274 22132 a 40.75145 -73.82103 15447 a 40.78485 -73.84128 7983 a 40.78639 -73.81086 9541 a 40.76047 -73.79637 5136 a 40.79178 -73.77688 232 a 40.78038 -73.78123 3259 a 40.76419 -73.77277 11225 a 40.75657 -73.73784 4118 a 40.77262 -73.74653 876 a 40.74529 -73.76059 2696 a 40.73963 -73.79449 7173 a 40.72815 -73.78502 2535 a 40.73014 -73.82703 4119 a 40.75172 -73.85182 10069 a 40.76336 -73.87237 3903 a 40.76539 -73.89324 3207 a 40.77389 -73.87348 8263 a 40.75169 -73.88364 7676 a 40.73884 -73.87853 16452 a 40.72642 -73.86153 10525 a 40.72093 -73.84615 19521 a 40.74482 -73.90516 15876 a 40.72474 -73.90964 18002 a 40.71675 -73.8796 4187 a NA NA不适用0 a 40.70067 -73.88943 13158 a NA NA 0 a NA NA 0 a 40.69402 -73.73622 1125 a 40.6981 -73.75 899 1373 a 40.67166 -73.75257 4921 a 40.6576 -73.8448 3272 a 40.70792 -73.82821 3864 a 40.68465 -73.84955 6213 a 40.67645 -73.84444 3237 a 40.70027 -73.83597 10273 a 40.68867 -73.82292 3022 a 40.67358 -73.81773 2119 a 40.69406 -73.85863 2348 a 40.66006 -73.73601 2399 a 40.71561 -73.76847 2698 a 40.7143 -73.82726 0 a 40.60775 -74.02394 6 a 40.73642 -73.72238 1644 a 40.7309 -73.74566 2662 a 40.72102 -73.74224 1840 a 40.70977 -73.73865 2159 a 40.64696 -73.78481 32803 a NA NA 0 a 40.71536 -73.79307 17141 a 40.69816 -73.78689 4413 a 40.67681 -73.77643 10162 a 40.70126 -73.8096 6113 a 40.67581 -73.79662 1150 a NA NA 0 a 40.70128 -73.79597 0 a NA NA 0 a 40.60128 -73.76165 9230 a 40.59409 -73.7929 1516 a 40.59069 -73.80975 1365 a 40.57827 -73.84476 2477 a NA NA 0 a 40.55569 -73.92066 0 a 40.65856 -73.83793 485674 a 40.65856 -73.83793 474309 a 40.65856 -73.83793 490781 a 40.65856 -73.83793 485415 a 40.7462 -73.71148 15104 b 40.7566 -73.71418 2127 b 40.74715 -73.93975 425461 b 40.77288 -73.9263 28530 b 40.76257 -73.91345 31037 b 40.74463 -73.9202 17761 b 40.77888 -73.90677 71613 b 40.76221 -73.93153 49392 b 40.74512 -73.95693 26 b NA NA 0 b 40.78075 -73.8253 22 b NA NA 422 b 40.76821 -73.8274 129835 b 40.75145 -73.82103 102112 b 40.78485 -73.84128 58960 b 40.78639 -73.81086 44983 b 40.76047 -73.79637 21056 b 40.79178 -73.77688 0 b 40 .78038 -73.78123 13793 b 40.76419 -73.77277 35714 b 40.75657 -73.73784 27032 b 40.77262 -73.74653 7736 b 40.74529 -73.76059 10625 b 40.73963 -73.79449 30687 b 40.72815 -73.78502 16195 b 40.73014 -73.82703 15304 b 40.75172 -73.85182 59640 b 40.76336 -73.87237 17290 b 40.76539 -73.89324 26305 b 40.77389 -73.87348 134868 b 40.75169 -73.88364 30477 b 40.73884 -73.87853 97516 b 40.72642 -73.86153 43091 b 40.72093 -73.84615 104323 b 40.74482 -73.90516 87453 b 40.72474 -73.90964 148989 b 40.71675 -73.8796 20918 b 不适用31 b 40.70067 -73.88943 106211 b 不适用75 b 40.69402 -73.73622 3544 b 40.6981 -73.75899 4854 b 40.67166 -73.75257 32455 b 40.6576 -73.8448 11468 b 40.70792 -73.82821 19029 b 40.68465 -73.84955 20529 b 40.67645 -73.84444 19449 b 40.70027 -73.83597 59519 b 40.68867 -73.82292 11405 b 40.67358 -73.81773 10186 b 40.69406 -73.85863 12451 b 40.66006 -73.73601 11736 b 40.71561 -73.76847 15923 b 40.7143 -73.82726 178 b 40.60775 -74.02394 0 b 40.73642 -73.72238 13449 b 40.7309 -73.74566 22605 b 40.72102 -73.74224 12583 b 40.70977 -73.73865 7087 b 40.64696 -73.78481 293941 b NA NA 1996 b 40.71536 -73.79307 134835 b 40.69816 -73.78689 35158 b 40.67681 -73.77643 71514 b 40.70126 -73.8096 31573 b 40.67581 -73.79662 2807 b NA NA 0 b 40.70128 - 73.79597 146 b NA NA 0 b 40.60128 -73.76165 64099 b 40.59409 -73.7929 10962 b 40.59069 -73.80975 4070 b 40.57827 -73.8447 6 12337 b 不适用59 b 40.55569 -73.92066 1289 b 40.7462 -73.71148 27391 b 40.7566 -73.71418 3325 b 40.74715 -73.93975 787094 b 40.77288 -73.9263 56684 b 40.76257 -73.91345 77633 b 40.74463 -73.9202 53017 b 40.77888 -73.90677 119137 b 40.76221 -73.93153 81405 b 40.74512 -73.95693 853 b 不适用36030 b 40.78075 -73.8253 0 b 不适用0 b 40.76821 -73.8274 217776 b 40.75145 -73.82103 168220 b 40.78485 - 73.84128 88312 b 40.78639 -73.81086 158064 b 40.76047 -73.79637 37400 b 40.79178 -73.77688 1694 b 40.78038 -73.78123 22329 b 40.76419 -73.77277 74178 b 40.75657 -73.73784 34693 b 40.77262 -73.74653 9633 b 40.74529 -73.76059 16537 b 40.73963 -73.79449 74371 b 40.72815 -73.78502 19425 b 40.73014 -73.82703 25734 b 40.75172 -73.85182 80863 b 40.76336 -73.87237 38098 b 40.76539 -73.89324 39765 b 40.77389 -73.87348 107770 b 40.75169 -73.88364 53202 b 40.73884 -73.87853 134436 b 40.72642 -73.86153 87298 b 40.72093 -73.84615 182240 b 40.74482 -73.90516 143954 b 40.72474 -73.90964 176588 b 40.71675 -73.8796 32620 b 不适用0 b 40.70067 -73.88943 110389 b 不适用0 b 不适用0 b 40.69402 -73.73622 7605 b 40.6981 -73.75899 7828 b 40.67166 -73.75257 43516 b 40.6576 -73.8448 19379 b 40.70792 -73.82821 27776 b 40.68465 -73.84955 62659 b 40.67645 -73.84444 23544 b 40.70027 -73.83597 111172 b 40.68867 -73.82292 18630 b 40.67358 -73.81773 13640 b 40.69406 -73.85863 14492 b 40.66006 -73 .73601 18271 b 40.71561 -73.76847 22171 b 40.7143 -73.82726 0 b 40.60775 -74.02394 45 b 40.73642 -73.72238 12110 b 40.7309 -73.74566 29922 b 40.72102 -73.74224 13098 b 40.70977 -73.73865 17941 b 40.64696 -73.78481 348916 b NA NA 282 b 40.71536 -73.79307 134967 b 40.69816 -73.78689 49745 b 40.67681 -73.77643 93472 b 40.70126 -73.8096 53065 b 40.67581 -73.79662 7434 b NA NA 0 b 40.70128 -73.79597 0 b 不适用不适用1480 b 40.60128 -73.76165 78882 b 40.59409 -73.7929 14203 b 40.59069 -73.80975 10872 b 40.57827 -73.84476 18295 b 不适用0 b 40.55569 -73.92066 0 b 40.7462 -73.71148 66084 c 40.7566 -73.71418 8573 c 40.74715 -73.93975 1843805 c 40.77288 -73.9263 133615 c 40.76257 -73.91345 137850 c 40.74463 -73.9202 81181 c 40.77888 -73.90677 302313 c 40.76221 -73.93153 220023 c 40.74512 -73.95693 301 c 不适用0 c 40.78075 -73.8253 92 c 不适用1971 c 40.76821 -73.8274 544653 c 40.75145 -73.82103 419811 c 40.78485 -73.84128 259427 c 40.78639 -73.81086 193106 c 40.76047 -73.79637 93157 c 40.79178 -73.77688 0 c 40.78038 -73.78123 60286 c 40.76419 -73.77277 156160 c 40.75657 -73.73784 111577 c 40.77262 -73.74653 31104 c 40.74529 -73.76059 47317 c 40.73963 -73.79449 130814 c 40.72815 -73.78502 67950 c 40.73014 -73.82703 69978 c 40.75172 -73.85182 282942 c 40.76336 -73.87237 77372 c 40.76539 -73.89324 109186 c 40.77389 -73.87348 517378 c 40.75169 -73.88364 134269 c 40.73884 -73.87853 40 6084 c 40.72642 -73.86153 182311 c 40.72093 -73.84615 438112 c 40.74482 -73.90516 394896 c 40.72474 -73.90964 629891 c 40.71675 -73.8796 92917 c 不适用NA 124 c 40.70067 -73.88943 460613 c 不适用538 c 40.69402 -73.73622 15340 c 40.6981 -73.75899 20866 c 40.67166 -73.75257 136822 c 40.6576 -73.8448 49428 c 40.70792 -73.82821 80721 c 40.68465 -73.84955 88995 c 40.67645 -73.84444 85021 c 40.70027 -73.83597 261390 c 40.68867 - 73.82292 53724 c 40.67358 -73.81773 41492 c 40.69406 -73.85863 49921 c 40.66006 -73.73601 51425 c 40.71561 -73.76847 70338 c 40.7143 -73.82726 818 c 40.60775 -74.02394 0 c 40.73642 -73.72238 53722 c 40.7309 -73.74566 91395 c 40.72102 -73.74224 44871 c 40.70977 -73.73865 31075 c 40.64696 -73.78481 1221994 c NA NA 7483 c 40.71536 -73.79307 556231 c 40.69816 -73.78689 152664 c 40.67681 -73.77643 302567 c 40.70126 -73.8096 133303 c 40.67581 -73.79662 12025 c NA NA 0 c 40.70128 -73.79597 632 c NA NA 0 c 40.60128 -73.76165 268753 c 40.59409 -73.7929 51702 c 40.59069 -73.80975 19536 c 40.57827 -73.84476 55319 c 不适用441 c 40.55569 -73.92066 6625 c 40.7462 -73.71148 112122 c 40.7566 -73.71418 13378 c 40.74715 -73.93975 3241581 c 40.77288 -73.9263 261306 c 40.76257 -73.91345 345772 c 40.74463 -73.9202 197763 c 40.77888 -73.90677 493083 c 40.76221 -73.93153 349103 c 40.74512 -73.95693 4638 c NA NA 105752 c 40.78075 -73.8253 0 c NA NA 0 c 40.76821 -73.8274 917321 c 40.75145 -73.82103 744570 c 40.78485 -73.84128 393565 c 40.78639 -73.81086 636438 c 40.76047 -73.79637 167972 c 40.79178 -73.77688 7060 c 40.78038 -73.78123 104627 c 40.76419 -73.77277 336880 c 40.75657 -73.73784 152396 c 40.77262 -73.74653 41396 c 40.74529 -73.76059 75838 c 40.73963 -73.79449 313518 c 40.72815 -73.78502 85678 c 40.73014 -73.82703 120070 c 40.75172 -73.85182 514110 c 40.76336 -73.87237 173600 c 40.76539 -73.89324 164016 c 40.77389 - 73.87348 427447 c 40.75169 -73.88364 232448 c 40.73884 -73.87853 592870 c 40.72642 -73.86153 360698 c 40.72093 -73.84615 766798 c 40.74482 -73.90516 638015 c 40.72474 -73.90964 800645 c 40.71675 -73.8796 141729 c NA NA 0 c 40.70067 -73.88943 482254 c N A NA 0 c NA NA 0 c 40.69402 -73.73622 33628 c 40.6981 -73.75899 36630 c 40.67166 -73.75257 190158 c 40.6576 -73.8448 85747 c 40.70792 -73.82821 121903 c 40.68465 -73.84955 272359 c 40.67645 -73.84444 86102 c 40.70027 -73.83597 490335 c 40.68867 -73.82292 83703 c 40.67358 -73.81773 62921 c 40.69406 -73.85863 65541 c 40.66006 -73.73601 83175 c 40.71561 -73.76847 96254 c 40.7143 -73.82726 1126 c 40.60775 -74.02394 185 c 40.73642 -73.72238 52068 c 40.7309 -73.74566 96879 c 40.72102 -73.74224 58815 c 40.70977 -73.73865 78567 c 40.64696 -73.78481 1454109 c NA NA 1223 c 40.71536 -73.79307 582318 c 40.69816 -73.78689 200462 c 40.67681 -73.77643 402264 c 40.70126 -73.8096 231929 c 40.67581 -73.79662 30674 c 不适用0 c c 40.70128 -73.79597 0 c 不适用6474 c 40.60128 -73.76165 338663 c 40.59409 -73.7929 61092 c 40.59069 -73.80975 47794 c 40.57827 -73.84476 77847 c NA NA 0 c 40.55569 -73.92066 12386 c 40.65856 -73.83793 5476703540 c 40.65856 -73.83793 5342856179 c 40.65856 -73.83793 6195156100 c 40.65856 -73.83793 5515386851 c 40.7462 -73.71148 192 d 40.7566 -73.71418 32 d 40.74715 -73.93975 2496 d 40.77288 -73.9263 569 d 40.76257 -73.91345 932 d 40.74463 -73.9202 462 d 40.77888 -73.90677 743 d 40.76221 -73.93153 857 d 40.74512 -73.95693 3 d 不适用28 d 40.78075 -73.8253 4 d 不适用25 d 40.76821 -73.8274 1772 d 40.75145 -73.82103 1187 d 40.78485 -73.84128 499 d 40.78639 -73.81086 7 90 d 40.76047 -73.79637 824 d 40.79178 -73.77688 2 d 40.78038 -73.78123 345 d 40.76419 -73.77277 831 d 40.75657 -73.73784 364 d 40.77262 -73.74653 182 d 40.74529 -73.76059 458 d 40.73963 -73.79449 522 d 40.72815 -73.78502 372 d 40.73014 -73.82703 471 d 40.75172 -73.85182 927 d 40.76336 -73.87237 189 d 40.76539 -73.89324 222 d 40.77389 -73.87348 75 d 40.75169 -73.88364 1309 d 40.73884 -73.87853 1189 d 40.72642 -73.86153 833 d 40.72093 -73.84615 1926 d 40.74482 -73.90516 1367 d 40.72474 -73.90964 904 d 40.71675 -73.8796 428 d NA NA 4 d 40.70067 -73.88943 1426 d NA NA 6 d 40.69402 -73.73622 111 d 40.6981 -73.75899 174 d 40.67166 -73.75257 342 d 40.6576 -73.8448 435 d 40.70792 -73.82821 348 d 40.68465 -73.84955 337 d 40.67645 -73.84444 347 d 40.70027 -73.83597 549 d 40.68867 -73.82292 511 d 40.67358 -73.81773 248 d 40.69406 -73.85863 369 d 40.66006 -73.73601 265 d 40.71561 -73.76847 300 d 40.7143 -73.82726 6 d 40.60775 -74.02394 1 d 40.73642 -73.72238 198 d 40.7309 -73.74566 187 d 40.72102 -73.74224 228 d 40.70977 -73.73865 160 d 40.64696 -73.78481 314 d 不适用25 d 40.71536 -73.79307 1092 d 40.69816 -73.78689 252 d 40.67681 -73.77643 817 d 40.70126 -73.8096 599 d 40.67581 -73.79662 51 d 不适用8 d 40.70128 -73.79597 3 d 不适用4 d 40.60128 -73.76165 399 d 40.59409 -73.7929 73 d 40.59069 -73.80975 83 d 40.57827 -73.84476 294 d 不适用5 d 40.55569 -73.92066 38 d 40.7462 -73.7 1148 229 d 40.7566 -73.71418 34 d 40.74715 -73.93975 2744 d 40.77288 -73.9263 679 d 40.76257 -73.91345 1108 d 40.74463 -73.9202 538 d 40.77888 -73.90677 903 d 40.76221 -73.93153 1028 d 40.74512 -73.95693 34 d NA NA 33 d 40.78075 -73.8253 1 d NA NA 8 d 40.76821 -73.8274 2704 d 40.75145 -73.82103 1832 d 40.78485 -73.84128 702 d 40.78639 -73.81086 917 d 40.76047 -73.79637 1160 d 40.79178 -73.77688 11 d 40.78038 -73.78123 361 d 40.76419 -73.77277 1010 d 40.75657 -73.73784 442 d 40.77262 -73.74653 165 d 40.74529 -73.76059 584 d 40.73963 -73.79449 643 d 40.72815 -73.78502 449 d 40.73014 -73.82703 582 d 40.75172 -73.85182 1277 d 40.76336 -73.87237 280 d 40.76539 - 73.89324 297 d 40.773 89 -73.87348 95 d 40.75169 -73.88364 1629 d 40.73884 -73.87853 1498 d 40.72642 -73.86153 1089 d 40.72093 -73.84615 2060 d 40.74482 -73.90516 1492 d 40.72474 -73.90964 1101 d 40.71675 -73.8796 543 d NA NA 2 d 40.70067 -73.88943 1742 d NA NA 2 d NA NA 2 d 40.69402 -73.73622 140 d 40.6981 -73.75899 239 d 40.67166 -73.75257 433 d 40.6576 -73.8448 438 d 40.70792 -73.82821 419 d 40.68465 -73.84955 393 d 40.67645 -73.84444 391 d 40.70027 -73.83597 718 d 40.68867 -73.82292 682 d 40.67358 -73.81773 373 d 40.69406 -73.85863 443 d 40.66006 -73.73601 307 d 40.71561 -73.76847 359 d 40.7143 -73.82726 7 d 40.60775 -74.02394 3 d 40.73642 -73.72238 248 d 40.7309 -73.74566 237 d 40.72102 -73.74224 310 d 40.70977 -73.73865 222 d 40.64696 -73.78481 342 d 不适用13 d 40.71536 -73.79307 1220 d 40.69816 -73.78689 298 d 40.67681 -73.77643 939 d 40.70126 -73.8096 823 d 40.67581 -73.79662 98 d 不适用27 d 40.70128 -73.79597 3 d 不适用9 d 40.60128 -73.76165 490 d 40.59409 -73.7929 90 d 40.59069 -73.80975 132 d 40.57827 -73.84476 330 d 不适用2 d 40.55569 -73.92066 48 d ) 解决方案主要问题似乎可以通过使用x和y美学的正确列: pre $ map stat_density2d(aes(x = X2,y = X1,z = X3,color = group,alpha = .. level ..), data = d,size = 2,contour = TRUE) ggsave(map.png ,plot = p,height = 7,width = 7) I feel like I've been endlessly searching for a solution to this and cannot find one anywhere. Basically, I need to overlay coloured contour maps as different layers (with different colours) onto a ggmap and cannot get this to work for my life. What I'm trying to do is take the example illustrated here: Overlay two ggplot2 stat_density2d plots with alpha channels only instead of mapping this onto a ggplot(), map it onto a ggmap(). I've read here: ggmap with geom_map superimposed that part of the difficulty in this is nullifying the implicit aesthetics sent from ggmap to ggplot, but I always encounter a whole series of errors regardless of what I do. Some of the errors I've encountered come from not recognizing 'group'. Use of geom_contour gives the following error when using the same format as below (z=X3): Error in contourLines(x = sort(unique(data$x)), y = sort(unique(data$y)), : no proper 'z' matrix specified Part of the difficulty has also been with assigning different colours to the variables. Binding the columns (as done below and in the example linked above) and distinguishing variable a form variable B in column X3 by having an "a" or a "b" in the fourth column 'groups' does not seem to register that well with the scale_color_manual(). Ultimately, I'm trying to find a way of assigning a different color to each column of a data.frame - any thoughts on that would also be helpful.I've included an example dataset and one of my approaches + errors - any help with this at all would be greatly appreciated (thank you in advance for your time/consideration)! Please let me know if there's anything else I can do to assist!library(ggplot2)library(ggmap)#location defined by lon/lat coordinateslocation <- c(-74.03990, 40.52726, -73.68864, 40.81141) map <- get_map(location = location, scale="auto", zoom=11) map2 <- ggmap(map) map2 + stat_density2d(data=d, geom="density2d", aes(x=d[,1],y=d[,2], z=d[,3], color = group,alpha=..level..),size=2, contour=TRUE) + scale_color_manual(values=c("a"="#FF0000", "b"="#00FF00"))#Following error received: Error in if (any(h <= 0)) stop("bandwidths must be strictly positive") : missing value where TRUE/FALSE needed #Dataset (d): d = read.table(header=TRUE, text="X1 X2 X3 group40.7462 -73.71148 2291 a40.7566 -73.71418 291 a40.74715 -73.93975 54579 a40.77288 -73.9263 4564 a40.76257 -73.91345 7189 a40.74463 -73.9202 3643 a40.77888 -73.90677 8108 a40.76221 -73.93153 7420 a40.74512 -73.95693 9 aNA NA 0 a40.78075-73.8253 9 aNA NA 138 a40.76821 -73.8274 17733 a40.75145 -73.82103 13321 a40.78485 -73.84128 7769 a40.78639 -73.81086 5970 a40.76047 -73.79637 4045 a40.79178 -73.77688 0 a40.78038 -73.78123 2548 a40.76419 -73.77277 6351 a40.75657 -73.73784 4000 a40.77262 -73.74653 1262 a40.74529 -73.76059 2251 a40.73963 -73.79449 4730 a40.72815 -73.78502 2660 a40.73014 -73.82703 3639 a40.75172 -73.85182 8450 a40.76336 -73.87237 3245 a40.76539 -73.89324 3207 a40.77389 -73.87348 11932 a40.75169 -73.88364 6080 a40.73884 -73.87853 14148 a40.72642 -73.86153 7352 a40.72093 -73.84615 15755 a40.74482 -73.90516 13699 a40.72474 -73.90964 19479 a40.71675 -73.8796 3975 aNA NA 11 a40.70067 -73.88943 17790 aNA NA 18 a40.69402 -73.73622 790 a40.6981 -73.75899 1104 a40.67166 -73.75257 4920 a40.6576 -73.8448 2669 a40.70792 -73.82821 3400 a40.68465 -73.84955 3935 a40.67645 -73.84444 3896 a40.70027 -73.83597 7791 a40.68867 -73.82292 2492 a40.67358 -73.81773 2555 a40.69406 -73.85863 2568 a40.66006 -73.73601 2136 a40.71561 -73.76847 2545 a40.7143 -73.82726 29 a40.60775 -74.02394 0 a40.73642 -73.72238 2336 a40.7309 -73.74566 2788 a40.72102 -73.74224 3074 a40.70977 -73.73865 1396 a40.64696 -73.78481 36136 aNA NA 433 a40.71536 -73.79307 20480 a40.69816 -73.78689 4812 a40.67681 -73.77643 10111 a40.70126 -73.8096 5259 a40.67581 -73.79662 734 aNA NA 0 a40.70128 -73.79597 38 aNA NA 0 a40.60128 -73.76165 8987 a40.59409 -73.7929 1512 a40.59069 -73.80975 785 a40.57827 -73.84476 2206 aNA NA 6 a40.55569 -73.92066 246 a40.7462 -73.71148 2662 a40.7566 -73.71418 323 a40.74715 -73.93975 57472 a40.77288 -73.9263 6104 a40.76257 -73.91345 10050 a40.74463 -73.9202 5435 a40.77888 -73.90677 8813 a40.76221 -73.93153 9495 a40.74512 -73.95693 104 aNA NA 987 a40.78075 -73.8253 0 aNA NA 0 a40.76821 -73.8274 22132 a40.75145 -73.82103 15447 a40.78485 -73.84128 7983 a40.78639 -73.81086 9541 a40.76047 -73.79637 5136 a40.79178 -73.77688 232 a40.78038 -73.78123 3259 a40.76419 -73.77277 11225 a40.75657 -73.73784 4118 a40.77262 -73.74653 876 a40.74529 -73.76059 2696 a40.73963 -73.79449 7173 a40.72815 -73.78502 2535 a40.73014 -73.82703 4119 a40.75172 -73.85182 10069 a40.76336 -73.87237 3903 a40.76539 -73.89324 3207 a40.77389 -73.87348 8263 a40.75169 -73.88364 7676 a40.73884 -73.87853 16452 a40.72642 -73.86153 10525 a40.72093 -73.84615 19521 a40.74482 -73.90516 15876 a40.72474 -73.90964 18002 a40.71675 -73.8796 4187 aNA NA 0 a40.70067 -73.88943 13158 aNA NA 0 aNA NA 0 a40.69402 -73.73622 1125 a40.6981 -73.75899 1373 a40.67166 -73.75257 4921 a40.6576 -73.8448 3272 a40.70792 -73.82821 3864 a40.68465 -73.84955 6213 a40.67645 -73.84444 3237 a40.70027 -73.83597 10273 a40.68867 -73.82292 3022 a40.67358 -73.81773 2119 a40.69406 -73.85863 2348 a40.66006 -73.73601 2399 a40.71561 -73.76847 2698 a40.7143 -73.82726 0 a40.60775 -74.02394 6 a40.73642 -73.72238 1644 a40.7309 -73.74566 2662 a40.72102 -73.74224 1840 a40.70977 -73.73865 2159 a40.64696 -73.78481 32803 aNA NA 0 a40.71536 -73.79307 17141 a40.69816 -73.78689 4413 a40.67681 -73.77643 10162 a40.70126 -73.8096 6113 a40.67581 -73.79662 1150 aNA NA 0 a40.70128 -73.79597 0 aNA NA 0 a40.60128 -73.76165 9230 a40.59409 -73.7929 1516 a40.59069 -73.80975 1365 a40.57827 -73.84476 2477 aNA NA 0 a40.55569 -73.92066 0 a40.65856 -73.83793 485674 a40.65856 -73.83793 474309 a40.65856 -73.83793 490781 a40.65856 -73.83793 485415 a40.7462 -73.71148 15104 b40.7566 -73.71418 2127 b40.74715 -73.93975 425461 b40.77288 -73.9263 28530 b40.76257 -73.91345 31037 b40.74463 -73.9202 17761 b40.77888 -73.90677 71613 b40.76221 -73.93153 49392 b40.74512 -73.95693 26 bNA NA 0 b40.78075 -73.8253 22 bNA NA 422 b40.76821 -73.8274 129835 b40.75145 -73.82103 102112 b40.78485 -73.84128 58960 b40.78639 -73.81086 44983 b40.76047 -73.79637 21056 b40.79178 -73.77688 0 b40.78038 -73.78123 13793 b40.76419 -73.77277 35714 b40.75657 -73.73784 27032 b40.77262 -73.74653 7736 b40.74529 -73.76059 10625 b40.73963 -73.79449 30687 b40.72815 -73.78502 16195 b40.73014 -73.82703 15304 b40.75172 -73.85182 59640 b40.76336 -73.87237 17290 b40.76539 -73.89324 26305 b40.77389 -73.87348 134868 b40.75169 -73.88364 30477 b40.73884 -73.87853 97516 b40.72642 -73.86153 43091 b40.72093 -73.84615 104323 b40.74482 -73.90516 87453 b40.72474 -73.90964 148989 b40.71675 -73.8796 20918 bNA NA 31 b40.70067 -73.88943 106211 bNA NA 75 b40.69402 -73.73622 3544 b40.6981 -73.75899 4854 b40.67166 -73.75257 32455 b40.6576 -73.8448 11468 b40.70792 -73.82821 19029 b40.68465 -73.84955 20529 b40.67645 -73.84444 19449 b40.70027 -73.83597 59519 b40.68867 -73.82292 11405 b40.67358 -73.81773 10186 b40.69406 -73.85863 12451 b40.66006 -73.73601 11736 b40.71561 -73.76847 15923 b40.7143 -73.82726 178 b40.60775 -74.02394 0 b40.73642 -73.72238 13449 b40.7309 -73.74566 22605 b40.72102 -73.74224 12583 b40.70977 -73.73865 7087 b40.64696 -73.78481 293941 bNA NA 1996 b40.71536 -73.79307 134835 b40.69816 -73.78689 35158 b40.67681 -73.77643 71514 b40.70126 -73.8096 31573 b40.67581 -73.79662 2807 bNA NA 0 b40.70128 -73.79597 146 bNA NA 0 b40.60128 -73.76165 64099 b40.59409 -73.7929 10962 b40.59069 -73.80975 4070 b40.57827 -73.84476 12337 bNA NA 59 b40.55569 -73.92066 1289 b40.7462 -73.71148 27391 b40.7566 -73.71418 3325 b40.74715 -73.93975 787094 b40.77288 -73.9263 56684 b40.76257 -73.91345 77633 b40.74463 -73.9202 53017 b40.77888 -73.90677 119137 b40.76221 -73.93153 81405 b40.74512 -73.95693 853 bNA NA 36030 b40.78075 -73.8253 0 bNA NA 0 b40.76821 -73.8274 217776 b40.75145 -73.82103 168220 b40.78485 -73.84128 88312 b40.78639 -73.81086 158064 b40.76047 -73.79637 37400 b40.79178 -73.77688 1694 b40.78038 -73.78123 22329 b40.76419 -73.77277 74178 b40.75657 -73.73784 34693 b40.77262 -73.74653 9633 b40.74529 -73.76059 16537 b40.73963 -73.79449 74371 b40.72815 -73.78502 19425 b40.73014 -73.82703 25734 b40.75172 -73.85182 80863 b40.76336 -73.87237 38098 b40.76539 -73.89324 39765 b40.77389 -73.87348 107770 b40.75169 -73.88364 53202 b40.73884 -73.87853 134436 b40.72642 -73.86153 87298 b40.72093 -73.84615 182240 b40.74482 -73.90516 143954 b40.72474 -73.90964 176588 b40.71675 -73.8796 32620 bNA NA 0 b40.70067 -73.88943 110389 bNA NA 0 bNA NA 0 b40.69402 -73.73622 7605 b40.6981 -73.75899 7828 b40.67166 -73.75257 43516 b40.6576 -73.8448 19379 b40.70792 -73.82821 27776 b40.68465 -73.84955 62659 b40.67645 -73.84444 23544 b40.70027 -73.83597 111172 b40.68867 -73.82292 18630 b40.67358 -73.81773 13640 b40.69406 -73.85863 14492 b40.66006 -73.73601 18271 b40.71561 -73.76847 22171 b40.7143 -73.82726 0 b40.60775 -74.02394 45 b40.73642 -73.72238 12110 b40.7309 -73.74566 29922 b40.72102 -73.74224 13098 b40.70977 -73.73865 17941 b40.64696 -73.78481 348916 bNA NA 282 b40.71536 -73.79307 134967 b40.69816 -73.78689 49745 b40.67681 -73.77643 93472 b40.70126 -73.8096 53065 b40.67581 -73.79662 7434 bNA NA 0 b40.70128 -73.79597 0 bNA NA 1480 b40.60128 -73.76165 78882 b40.59409 -73.7929 14203 b40.59069 -73.80975 10872 b40.57827 -73.84476 18295 bNA NA 0 b40.55569 -73.92066 0 b40.7462 -73.71148 66084 c40.7566 -73.71418 8573 c40.74715 -73.93975 1843805 c40.77288 -73.9263 133615 c40.76257 -73.91345 137850 c40.74463 -73.9202 81181 c40.77888 -73.90677 302313 c40.76221 -73.93153 220023 c40.74512 -73.95693 301 cNA NA 0 c40.78075 -73.8253 92 cNA NA 1971 c40.76821 -73.8274 544653 c40.75145 -73.82103 419811 c40.78485 -73.84128 259427 c40.78639 -73.81086 193106 c40.76047 -73.79637 93157 c40.79178 -73.77688 0 c40.78038 -73.78123 60286 c40.76419 -73.77277 156160 c40.75657 -73.73784 111577 c40.77262 -73.74653 31104 c40.74529 -73.76059 47317 c40.73963 -73.79449 130814 c40.72815 -73.78502 67950 c40.73014 -73.82703 69978 c40.75172 -73.85182 282942 c40.76336 -73.87237 77372 c40.76539 -73.89324 109186 c40.77389 -73.87348 517378 c40.75169 -73.88364 134269 c40.73884 -73.87853 406084 c40.72642 -73.86153 182311 c40.72093 -73.84615 438112 c40.74482 -73.90516 394896 c40.72474 -73.90964 629891 c40.71675 -73.8796 92917 cNA NA 124 c40.70067 -73.88943 460613 cNA NA 538 c40.69402 -73.73622 15340 c40.6981 -73.75899 20866 c40.67166 -73.75257 136822 c40.6576 -73.8448 49428 c40.70792 -73.82821 80721 c40.68465 -73.84955 88995 c40.67645 -73.84444 85021 c40.70027 -73.83597 261390 c40.68867 -73.82292 53724 c40.67358 -73.81773 41492 c40.69406 -73.85863 49921 c40.66006 -73.73601 51425 c40.71561 -73.76847 70338 c40.7143 -73.82726 818 c40.60775 -74.02394 0 c40.73642 -73.72238 53722 c40.7309 -73.74566 91395 c40.72102 -73.74224 44871 c40.70977 -73.73865 31075 c40.64696 -73.78481 1221994 cNA NA 7483 c40.71536 -73.79307 556231 c40.69816 -73.78689 152664 c40.67681 -73.77643 302567 c40.70126 -73.8096 133303 c40.67581 -73.79662 12025 cNA NA 0 c40.70128 -73.79597 632 cNA NA 0 c40.60128 -73.76165 268753 c40.59409 -73.7929 51702 c40.59069 -73.80975 19536 c40.57827 -73.84476 55319 cNA NA 441 c40.55569 -73.92066 6625 c40.7462 -73.71148 112122 c40.7566 -73.71418 13378 c40.74715 -73.93975 3241581 c40.77288 -73.9263 261306 c40.76257 -73.91345 345772 c40.74463 -73.9202 197763 c40.77888 -73.90677 493083 c40.76221 -73.93153 349103 c40.74512 -73.95693 4638 cNA NA 105752 c40.78075 -73.8253 0 cNA NA 0 c40.76821 -73.8274 917321 c40.75145 -73.82103 744570 c40.78485 -73.84128 393565 c40.78639 -73.81086 636438 c40.76047 -73.79637 167972 c40.79178 -73.77688 7060 c40.78038 -73.78123 104627 c40.76419 -73.77277 336880 c40.75657 -73.73784 152396 c40.77262 -73.74653 41396 c40.74529 -73.76059 75838 c40.73963 -73.79449 313518 c40.72815 -73.78502 85678 c40.73014 -73.82703 120070 c40.75172 -73.85182 514110 c40.76336 -73.87237 173600 c40.76539 -73.89324 164016 c40.77389 -73.87348 427447 c40.75169 -73.88364 232448 c40.73884 -73.87853 592870 c40.72642 -73.86153 360698 c40.72093 -73.84615 766798 c40.74482 -73.90516 638015 c40.72474 -73.90964 800645 c40.71675 -73.8796 141729 cNA NA 0 c40.70067 -73.88943 482254 cNA NA 0 cNA NA 0 c40.69402 -73.73622 33628 c40.6981 -73.75899 36630 c40.67166 -73.75257 190158 c40.6576 -73.8448 85747 c40.70792 -73.82821 121903 c40.68465 -73.84955 272359 c40.67645 -73.84444 86102 c40.70027 -73.83597 490335 c40.68867 -73.82292 83703 c40.67358 -73.81773 62921 c40.69406 -73.85863 65541 c40.66006 -73.73601 83175 c40.71561 -73.76847 96254 c40.7143 -73.82726 1126 c40.60775 -74.02394 185 c40.73642 -73.72238 52068 c40.7309 -73.74566 96879 c40.72102 -73.74224 58815 c40.70977 -73.73865 78567 c40.64696 -73.78481 1454109 cNA NA 1223 c40.71536 -73.79307 582318 c40.69816 -73.78689 200462 c40.67681 -73.77643 402264 c40.70126 -73.8096 231929 c40.67581 -73.79662 30674 cNA NA 0 c40.70128 -73.79597 0 cNA NA 6474 c40.60128 -73.76165 338663 c40.59409 -73.7929 61092 c40.59069 -73.80975 47794 c40.57827 -73.84476 77847 cNA NA 0 c40.55569 -73.92066 12386 c40.65856 -73.83793 5476703540 c40.65856 -73.83793 5342856179 c40.65856 -73.83793 6195156100 c40.65856 -73.83793 5515386851 c40.7462 -73.71148 192 d40.7566 -73.71418 32 d40.74715 -73.93975 2496 d40.77288 -73.9263 569 d40.76257 -73.91345 932 d40.74463 -73.9202 462 d40.77888 -73.90677 743 d40.76221 -73.93153 857 d40.74512 -73.95693 3 dNA NA 28 d40.78075 -73.8253 4 dNA NA 25 d40.76821 -73.8274 1772 d40.75145 -73.82103 1187 d40.78485 -73.84128 499 d40.78639 -73.81086 790 d40.76047 -73.79637 824 d40.79178 -73.77688 2 d40.78038 -73.78123 345 d40.76419 -73.77277 831 d40.75657 -73.73784 364 d40.77262 -73.74653 182 d40.74529 -73.76059 458 d40.73963 -73.79449 522 d40.72815 -73.78502 372 d40.73014 -73.82703 471 d40.75172 -73.85182 927 d40.76336 -73.87237 189 d40.76539 -73.89324 222 d40.77389 -73.87348 75 d40.75169 -73.88364 1309 d40.73884 -73.87853 1189 d40.72642 -73.86153 833 d40.72093 -73.84615 1926 d40.74482 -73.90516 1367 d40.72474 -73.90964 904 d40.71675 -73.8796 428 dNA NA 4 d40.70067 -73.88943 1426 dNA NA 6 d40.69402 -73.73622 111 d40.6981 -73.75899 174 d40.67166 -73.75257 342 d40.6576 -73.8448 435 d40.70792 -73.82821 348 d40.68465 -73.84955 337 d40.67645 -73.84444 347 d40.70027 -73.83597 549 d40.68867 -73.82292 511 d40.67358 -73.81773 248 d40.69406 -73.85863 369 d40.66006 -73.73601 265 d40.71561 -73.76847 300 d40.7143 -73.82726 6 d40.60775 -74.02394 1 d40.73642 -73.72238 198 d40.7309 -73.74566 187 d40.72102 -73.74224 228 d40.70977 -73.73865 160 d40.64696 -73.78481 314 dNA NA 25 d40.71536 -73.79307 1092 d40.69816 -73.78689 252 d40.67681 -73.77643 817 d40.70126 -73.8096 599 d40.67581 -73.79662 51 dNA NA 8 d40.70128 -73.79597 3 dNA NA 4 d40.60128 -73.76165 399 d40.59409 -73.7929 73 d40.59069 -73.80975 83 d40.57827 -73.84476 294 dNA NA 5 d40.55569 -73.92066 38 d40.7462 -73.71148 229 d40.7566 -73.71418 34 d40.74715 -73.93975 2744 d40.77288 -73.9263 679 d40.76257 -73.91345 1108 d40.74463 -73.9202 538 d40.77888 -73.90677 903 d40.76221 -73.93153 1028 d40.74512 -73.95693 34 dNA NA 33 d40.78075 -73.8253 1 dNA NA 8 d40.76821 -73.8274 2704 d40.75145 -73.82103 1832 d40.78485 -73.84128 702 d40.78639 -73.81086 917 d40.76047 -73.79637 1160 d40.79178 -73.77688 11 d40.78038 -73.78123 361 d40.76419 -73.77277 1010 d40.75657 -73.73784 442 d40.77262 -73.74653 165 d40.74529 -73.76059 584 d40.73963 -73.79449 643 d40.72815 -73.78502 449 d40.73014 -73.82703 582 d40.75172 -73.85182 1277 d40.76336 -73.87237 280 d40.76539 -73.89324 297 d40.77389 -73.87348 95 d40.75169 -73.88364 1629 d40.73884 -73.87853 1498 d40.72642 -73.86153 1089 d40.72093 -73.84615 2060 d40.74482 -73.90516 1492 d40.72474 -73.90964 1101 d40.71675 -73.8796 543 dNA NA 2 d40.70067 -73.88943 1742 dNA NA 2 dNA NA 2 d40.69402 -73.73622 140 d40.6981 -73.75899 239 d40.67166 -73.75257 433 d40.6576 -73.8448 438 d40.70792 -73.82821 419 d40.68465 -73.84955 393 d40.67645 -73.84444 391 d40.70027 -73.83597 718 d40.68867 -73.82292 682 d40.67358 -73.81773 373 d40.69406 -73.85863 443 d40.66006 -73.73601 307 d40.71561 -73.76847 359 d40.7143 -73.82726 7 d40.60775 -74.02394 3 d40.73642 -73.72238 248 d40.7309 -73.74566 237 d40.72102 -73.74224 310 d40.70977 -73.73865 222 d40.64696 -73.78481 342 dNA NA 13 d40.71536 -73.79307 1220 d40.69816 -73.78689 298 d40.67681 -73.77643 939 d40.70126 -73.8096 823 d40.67581 -73.79662 98 dNA NA 27 d40.70128 -73.79597 3 dNA NA 9 d40.60128 -73.76165 490 d40.59409 -73.7929 90 d40.59069 -73.80975 132 d40.57827 -73.84476 330 dNA NA 2 d40.55569 -73.92066 48 d") 解决方案 The primary problem seems to be solved by using the correct columns for x and y aesthetics:p = map2 + stat_density2d(aes(x=X2 ,y=X1, z=X3, color=group, alpha=..level..), data=d, size=2, contour=TRUE)ggsave("map.png", plot=p, height=7, width=7) 这篇关于使用不同的颜色将多个数据与二维密度叠加到ggmap上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!