在绘制等高线图时出现错误(如果(if(any(h <= 0))stop(“带宽必须严格为正”),则出错)在R中

Nan*_*ini 3 r ggplot2 ggmap

对于以下数据

       > head(df)

      Date Longitude Latitude Elevation Max.Temperature Min.Temperature Precipitation     Wind Relative.Humidity    Solar  RO
     1 2014-07-01   77.1875  7.96184     -9999          27.725          26.673  16.115560560 8.395378         0.8132272 23.08192 Yes
     2 2014-07-02   77.1875  7.96184     -9999          27.931          26.897   0.700378560 8.062267         0.8074675 21.48473 Yes
     3 2014-07-03   77.1875  7.96184     -9999          28.179          26.686   0.000000000 9.465022         0.8107901 24.14900  No
     4 2014-07-04   77.1875  7.96184     -9999          27.657          26.545   0.003433226 9.397203         0.8195020 23.42036 Yes
     5 2014-07-05   77.1875  7.96184     -9999          27.157          26.490   1.541518560 8.903047         0.8385059 23.90545 Yes
     6 2014-07-06   77.1875  7.96184     -9999          27.308          26.481   0.000000000 8.617348         0.8205267 23.96318  No
Run Code Online (Sandbox Code Playgroud)

我已经使用ggmap创建了地图

     > Precip_map<-get_map(location="india",maptype="satellite",zoom=12)

        Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=india&zoom=12&size=640x640&scale=2&maptype=satellite&language=en-EN&sensor=false

     > ggmap(Precip_map, extent = "device") + geom_point(aes(x = Longitude, y = Latitude), colour = "red", 
     +     alpha = 0.1, size = 2, data = df)

     Warning message:
    In loop_apply(n, do.ply) :
    Removed 1106 rows containing missing values (geom_point).
Run Code Online (Sandbox Code Playgroud)

绘制轮廓图

    > ggmap(Precip_map, extent = "device") + geom_density2d(data = df, 
    +     aes(x = Longitude, y = Latitude), size = 0.3) + stat_density2d(data = df, 
    +     aes(x = Longitude, y = Latitude, fill = ..level.., alpha = ..level..), size = 0.01, 
    +     bins = 16, geom = "polygon") + scale_fill_gradient(low = "green", high = "red", 
    +     guide = FALSE) + scale_alpha(range = c(0, 0.3), guide = FALSE)

     Error in if (any(h <= 0)) stop("bandwidths must be strictly positive") : 
     missing value where TRUE/FALSE needed
     Error in if (any(h <= 0)) stop("bandwidths must be strictly positive") : 
     missing value where TRUE/FALSE needed
     In addition: Warning message:
     In loop_apply(n, do.ply) :
     Removed 1106 rows containing non-finite values (stat_density2d).
     Warning message:
     In loop_apply(n, do.ply) :
     Removed 1106 rows containing non-finite values (stat_density2d).
Run Code Online (Sandbox Code Playgroud)

我不确切知道我缺少的地方..我是这个映射的新手..请帮助我。另外,我想在此轮廓图中绘制df $ Precipitation。

Dot*_*tPi 6

lat, long整个数据集中的值相同。因此,latlong方向的方差均为零,因此无法计算内核密度估计(KDE)的带宽。

因此,您得到了错误bandwidths must be strictly positive

为了计算2-D KDE,x和y方向的方差都必须为正。由于两次方差均为0,因此您会收到两次错误。