Getting Error while plotting contour map as (Error in if (any(h <= 0)) stop(“bandwidths must be strictly positive”) : in R

…衆ロ難τιáo~ 提交于 2019-12-21 19:52:23

问题


For the below data

       > 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

I have created an map using 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).

drawn contour map

    > 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).

I dont know exactly where i am lacking.. i am new to this mapping.. please assist me. Also, I want to plot df$Precipitation in this contour map.


回答1:


The lat, long values are same across the entire dataset. Hence, the variance in both the lat and long direction is zero, so a bandwidth for the kernel density estimate (KDE) can't be calculated.

Hence, you are getting the error bandwidths must be strictly positive.

For calculating 2-D KDEs, the variance in both x and y direction need to positive. You are getting the error twice as in your case variance in both directions are 0.



来源:https://stackoverflow.com/questions/31118984/getting-error-while-plotting-contour-map-as-error-in-if-anyh-0-stopban

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!