使用不同颜色将具有2D密度的多个数据叠加到ggmap上

时间:2014-08-26 19:02:00

标签: r ggplot2 overlay contour ggmap

我觉得我一直在寻找解决方案而无法在任何地方找到解决方案。基本上,我需要将彩色等高线图作为不同的图层(具有不同的颜色)叠加到ggmap上,并且不能让它在我的生活中起作用。我要做的是采用此处所示的示例:Overlay two ggplot2 stat_density2d plots with alpha channels而不是将其映射到ggplot(),将其映射到ggmap()。我在这里读到:ggmap with geom_map superimposed这部分难度使得从ggmap发送到ggplot的隐含美学无效,但无论我做什么,我总会遇到一系列错误。我遇到的一些错误来自于不承认'群'。当使用与下面相同的格式(z = X3)时,使用geom_contour会出现以下错误:

 Error in contourLines(x = sort(unique(data$x)), y = sort(unique(data$y)),  : 
 no proper 'z' matrix specified 

部分困难还在于为变量分配不同的颜色。绑定列(如下面和上面链接的示例中所做的那样)并通过在第四列'groups'中使用“a”或“b”来区分变量形式变量B在列X3中似乎没有很好地记录scale_color_manual()。最终,我正在尝试找到一种为data.frame的每一列分配不同颜色的方法 - 对此的任何想法也会有所帮助。

我已经包含了一个示例数据集和我的一个方法+错误 - 非常感谢任何帮助(非常感谢您的时间/考虑)!如果我能做任何其他事情来帮助我,请告诉我。

library(ggplot2)
library(ggmap)

#location defined by lon/lat coordinates
location <- 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  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
NA  NA  11  a
40.70067    -73.88943   17790   a
NA  NA  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
NA  NA  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.75899   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
NA  NA  31  b
40.70067    -73.88943   106211  b
NA  NA  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.84476   12337   b
NA  NA  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
NA  NA  36030   b
40.78075    -73.8253    0   b
NA  NA  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
NA  NA  0   b
40.70067    -73.88943   110389  b
NA  NA  0   b
NA  NA  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
NA  NA  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
NA  NA  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
NA  NA  0   c
40.78075    -73.8253    92  c
NA  NA  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   406084  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  NA  124 c
40.70067    -73.88943   460613  c
NA  NA  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
NA  NA  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
NA  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
NA  NA  0   c
40.70128    -73.79597   0   c
NA  NA  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
NA  NA  28  d
40.78075    -73.8253    4   d
NA  NA  25  d
40.76821    -73.8274    1772    d
40.75145    -73.82103   1187    d
40.78485    -73.84128   499 d
40.78639    -73.81086   790 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
NA  NA  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
NA  NA  8   d
40.70128    -73.79597   3   d
NA  NA  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
NA  NA  5   d
40.55569    -73.92066   38  d
40.7462 -73.71148   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.77389    -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
NA  NA  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
NA  NA  27  d
40.70128    -73.79597   3   d
NA  NA  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
NA  NA  2   d
40.55569    -73.92066   48  d")

1 个答案:

答案 0 :(得分:1)

使用x和y美学的正确列似乎可以解决主要问题:

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)

enter image description here