Search code examples
rggplot2geospatialspatial

Interpolating irregular spatial data - land mass only


I have irregular spatial data I want to plot, and it's on a somewhat coarse grid of points, so I'm using linear interpolation from the interp library in order to do so. However, I only want to visualize the data on land, not on water (water should just be white space). And the interp function is interpolating over some of the water, which I don't want to know. Any ideas on how to avoid this, or how to override this orange region with white space? Here is the code I have tried and the accompanying plot:

data_for_plot <- interp(df$x, df$y, df$z, nx = 500, ny = 500) |> 
  interp2xyz() |> 
  as.data.frame()

ggplot(data = data_for_plot , aes(x, y)) +
  geom_raster(aes(fill = z)) +
  scale_fill_distiller(palette = "Spectral", na.value = NA) + 
  theme_classic() 

enter image description here

Also, here is the code used to generate the data frame df:

df <- structure(list(x = c(-82.4858388134, -82.3769388134, -82.3703388134, 
-82.3626388134, -82.3901388134, -82.3637388134, -82.3747388134, 
-82.3758388134, -82.4000388134, -82.3395388134, -82.4847388134, 
-82.3428388134, -82.3736388134, -82.3758388134, -82.3450388134, 
-82.3824388134, -82.3736388134, -82.3648388134, -82.3516388134, 
-82.4286388134, -82.3780388134, -82.3637388134, -82.4264388134, 
-82.3615388134, -82.3956388134, -82.3494388134, -82.3461388134, 
-82.3406388134, -82.3604388134, -82.3813388134, -82.4594388134, 
-82.3901388134, -82.3648388134, -82.3472388134, -82.4495388134, 
-82.3681388134, -82.4198388134, -82.3769388134, -82.4022388134, 
-82.4363388134, -82.4000388134, -82.3582388134, -82.3615388134, 
-82.3747388134, -82.3439388134, -82.3373388134, -82.3549388134, 
-82.3450388134, -82.4154388134, -82.3351388134, -82.4693388134, 
-82.3637388134, -82.3637388134, -82.3692388134, -82.3758388134, 
-82.3637388134, -82.3681388134, -82.3714388134, -82.4693388134, 
-82.3912388134, -82.3912388134, -82.3945388134, -82.4253388134, 
-82.3879388134, -82.3659388134, -82.3769388134, -82.3890388134, 
-82.3725388134, -82.3934388134, -82.3659388134, -82.3736388134, 
-82.3725388134, -82.3637388134, -82.4231388134, -82.3450388134, 
-82.3439388134, -82.3934388134, -82.3560388134, -82.4022388134, 
-82.3813388134, -82.3747388134, -82.3692388134, -82.4132388134, 
-82.4110388134, -82.3362388134, -82.3417388134, -82.3813388134, 
-82.3516388134, -82.4528388134, -82.3802388134, -82.3802388134, 
-82.4385388134, -82.3681388134, -82.3703388134, -82.4396388134, 
-82.3615388134, -82.3912388134, -82.3780388134, -82.4704388134, 
-82.3692388134, -82.3912388134, -82.3703388134, -82.3450388134, 
-82.3703388134, -82.3406388134, -82.3670388134, -82.4396388134, 
-82.3670388134, -82.3725388134, -82.3483388134, -82.3516388134, 
-82.3428388134, -82.3791388134, -82.4462388134, -82.3648388134, 
-82.3758388134, -82.3417388134, -82.4385388134, -82.3604388134, 
-82.3626388134, -82.3736388134, -82.3813388134, -82.3549388134, 
-82.4231388134, -82.3780388134, -82.3362388134, -82.4011388134, 
-82.3417388134, -82.3483388134, -82.3351388134, -82.3615388134, 
-82.3582388134, -82.3560388134, -82.4451388134, -82.3395388134, 
-82.4737388134, -82.4605388134, -82.3824388134, -82.3615388134, 
-82.3538388134, -82.4638388134, -82.3769388134, -82.4550388134, 
-82.3769388134, -82.3494388134, -82.4209388134, -82.3472388134, 
-82.3978388134, -82.3615388134, -82.4484388134, -82.3791388134, 
-82.3670388134, -82.4033388134, -82.4231388134, -82.3571388134, 
-82.3725388134, -82.3582388134, -82.3780388134, -82.3879388134, 
-82.3659388134, -82.3824388134, -82.4176388134, -82.3648388134, 
-82.3681388134, -82.3593388134, -82.3604388134, -82.3824388134, 
-82.3395388134, -82.4308388134, -82.3648388134, -82.3890388134, 
-82.3758388134, -82.3879388134, -82.3439388134, -82.3351388134, 
-82.3769388134, -82.3769388134, -82.3538388134, -82.3725388134, 
-82.4110388134, -82.3879388134, -82.3692388134, -82.3439388134, 
-82.3736388134, -82.4275388134, -82.3406388134, -82.3615388134, 
-82.4792388134, -82.3703388134, -82.3659388134, -82.3791388134, 
-82.3648388134, -82.3879388134, -82.4099388134, -82.3725388134, 
-82.3780388134, -82.3659388134, -82.3967388134, -82.3505388134, 
-82.3736388134, -82.3516388134, -82.3461388134, -82.3472388134, 
-82.3923388134, -82.3868388134, -82.3692388134, -82.3428388134, 
-82.3769388134, -82.3670388134, -82.3549388134, -82.4825388134, 
-82.3516388134, -82.4418388134, -82.3857388134, -82.4539388134, 
-82.3505388134, -82.4671388134, -82.4275388134, -82.3967388134, 
-82.3736388134, -82.4055388134, -82.3615388134, -82.3659388134, 
-82.3890388134, -82.3791388134, -82.3868388134, -82.3780388134, 
-82.3780388134, -82.4033388134, -82.3450388134, -82.3736388134, 
-82.4748388134, -82.4165388134, -82.3560388134, -82.4517388134, 
-82.3780388134, -82.3780388134, -82.3923388134, -82.3934388134, 
-82.3703388134, -82.4836388134, -82.3637388134, -82.3439388134, 
-82.3571388134, -82.3769388134, -82.3758388134, -82.3736388134, 
-82.3406388134, -82.3483388134, -82.3483388134, -82.3571388134, 
-82.4110388134, -82.3747388134, -82.3824388134, -82.3725388134, 
-82.3659388134, -82.3494388134, -82.3890388134, -82.3593388134, 
-82.4946388134, -82.3582388134, -82.3769388134, -82.3439388134, 
-82.3725388134, -82.3604388134, -82.3538388134, -82.3879388134, 
-82.3637388134, -82.3648388134, -82.4341388134, -82.4099388134, 
-82.3802388134, -82.4077388134, -82.3483388134, -82.3450388134, 
-82.3582388134, -82.3648388134, -82.3824388134, -82.3439388134, 
-82.4781388134, -82.3417388134, -82.3637388134, -82.3516388134, 
-82.3538388134, -82.3472388134, -82.3681388134, -82.3791388134, 
-82.3428388134, -82.4660388134, -82.3681388134, -82.4429388134, 
-82.3373388134, -82.3912388134, -82.3494388134, -82.4176388134, 
-82.3417388134, -82.4154388134, -82.3670388134, -82.3758388134, 
-82.3714388134, -82.3417388134, -82.3406388134, -82.3945388134, 
-82.3956388134, -82.3736388134, -82.4330388134, -82.3780388134, 
-82.3428388134, -82.4088388134, -82.3813388134, -82.4044388134, 
-82.3593388134, -82.3637388134, -82.3538388134, -82.3373388134, 
-82.3472388134, -82.4121388134, -82.3901388134, -82.3670388134, 
-82.3450388134, -82.3483388134, -82.3868388134, -82.3571388134, 
-82.3461388134, -82.4363388134, -82.3483388134, -82.3538388134, 
-82.4319388134, -82.4220388134, -82.3989388134, -82.3857388134, 
-82.4077388134, -82.3472388134, -82.3626388134, -82.3494388134, 
-82.3439388134, -82.3868388134, -82.4495388134, -82.3505388134, 
-82.3417388134, -82.3835388134, -82.4000388134, -82.3736388134, 
-82.4000388134, -82.3824388134, -82.4143388134, -82.3802388134, 
-82.3615388134, -82.3648388134, -82.3967388134, -82.3637388134, 
-82.3769388134, -82.3681388134, -82.3505388134, -82.4308388134, 
-82.3681388134, -82.3637388134, -82.4022388134, -82.3956388134, 
-82.3714388134, -82.4033388134, -82.3582388134, -82.4121388134, 
-82.3582388134, -82.3461388134, -82.3604388134, -82.3659388134, 
-82.3516388134, -82.3890388134, -82.4286388134, -82.3461388134, 
-82.3813388134, -82.3659388134, -82.3626388134, -82.3472388134, 
-82.3890388134, -82.4627388134, -82.4561388134, -82.4605388134, 
-82.3857388134, -82.3758388134, -82.3472388134, -82.3538388134, 
-82.3978388134, -82.3835388134, -82.3769388134, -82.3395388134, 
-82.4451388134, -82.3758388134, -82.4352388134, -82.3692388134, 
-82.4088388134, -82.3692388134, -82.3813388134, -82.3681388134, 
-82.3483388134, -82.3879388134, -82.3736388134, -82.3780388134, 
-82.3670388134, -82.3626388134, -82.3648388134, -82.4594388134, 
-82.3758388134, -82.3648388134, -82.3956388134, -82.3450388134, 
-82.3527388134, -82.3637388134), y = c(35.2743142104, 35.2005142104, 
35.1663142104, 35.2374142104, 35.1735142104, 35.2599142104, 35.1753142104, 
35.2599142104, 35.1753142104, 35.2302142104, 35.2770142104, 35.1672142104, 
35.2095142104, 35.2041142104, 35.1708142104, 35.2662142104, 35.2662142104, 
35.1807142104, 35.2581142104, 35.2671142104, 35.1960142104, 35.2788142104, 
35.2761142104, 35.2221142104, 35.1681142104, 35.2212142104, 35.2050142104, 
35.1960142104, 35.2203142104, 35.1915142104, 35.2707142104, 35.2536142104, 
35.1771142104, 35.1717142104, 35.2743142104, 35.1933142104, 35.2734142104, 
35.1933142104, 35.2653142104, 35.2698142104, 35.1645142104, 35.2662142104, 
35.2689142104, 35.2185142104, 35.1852142104, 35.1978142104, 35.2410142104, 
35.1879142104, 35.2662142104, 35.1888142104, 35.2761142104, 35.2572142104, 
35.1951142104, 35.2338142104, 35.2752142104, 35.2149142104, 35.1843142104, 
35.1852142104, 35.2725142104, 35.1645142104, 35.2743142104, 35.2554142104, 
35.2725142104, 35.1825142104, 35.1915142104, 35.2572142104, 35.2581142104, 
35.2581142104, 35.2770142104, 35.2266142104, 35.1753142104, 35.2788142104, 
35.2401142104, 35.2608142104, 35.1672142104, 35.2473142104, 35.2608142104, 
35.1897142104, 35.1636142104, 35.2563142104, 35.1942142104, 35.1771142104, 
35.2671142104, 35.2653142104, 35.2248142104, 35.2752142104, 35.2455142104, 
35.2059142104, 35.2788142104, 35.2545142104, 35.2734142104, 35.2743142104, 
35.2428142104, 35.2113142104, 35.2662142104, 35.2329142104, 35.1753142104, 
35.2293142104, 35.2734142104, 35.2662142104, 35.1636142104, 35.2023142104, 
35.2473142104, 35.2698142104, 35.2797142104, 35.2437142104, 35.2761142104, 
35.2410142104, 35.2176142104, 35.2284142104, 35.1663142104, 35.2293142104, 
35.1978142104, 35.2734142104, 35.2518142104, 35.2581142104, 35.2788142104, 
35.2734142104, 35.1807142104, 35.1816142104, 35.1996142104, 35.2419142104, 
35.2050142104, 35.2707142104, 35.1843142104, 35.2788142104, 35.2671142104, 
35.2293142104, 35.1834142104, 35.2248142104, 35.2194142104, 35.2572142104, 
35.1753142104, 35.2725142104, 35.2752142104, 35.2725142104, 35.2698142104, 
35.2545142104, 35.2068142104, 35.2293142104, 35.2770142104, 35.2248142104, 
35.2707142104, 35.2365142104, 35.2275142104, 35.2743142104, 35.1897142104, 
35.1780142104, 35.2716142104, 35.2743142104, 35.2185142104, 35.2275142104, 
35.1708142104, 35.2743142104, 35.2194142104, 35.2401142104, 35.1978142104, 
35.1645142104, 35.2743142104, 35.2284142104, 35.2500142104, 35.2653142104, 
35.2608142104, 35.1807142104, 35.2779142104, 35.1870142104, 35.1690142104, 
35.2185142104, 35.2797142104, 35.2311142104, 35.1717142104, 35.1645142104, 
35.2545142104, 35.1825142104, 35.2221142104, 35.2329142104, 35.2203142104, 
35.2563142104, 35.2284142104, 35.2581142104, 35.2788142104, 35.1834142104, 
35.2680142104, 35.2536142104, 35.2797142104, 35.2482142104, 35.2653142104, 
35.2788142104, 35.1717142104, 35.1807142104, 35.1852142104, 35.2437142104, 
35.2662142104, 35.2590142104, 35.1825142104, 35.2797142104, 35.1690142104, 
35.2734142104, 35.2446142104, 35.2302142104, 35.1789142104, 35.2338142104, 
35.1798142104, 35.2599142104, 35.1924142104, 35.2356142104, 35.1744142104, 
35.2050142104, 35.2383142104, 35.1690142104, 35.2716142104, 35.2590142104, 
35.2788142104, 35.1681142104, 35.2698142104, 35.1726142104, 35.2779142104, 
35.2680142104, 35.2797142104, 35.2320142104, 35.2779142104, 35.2230142104, 
35.2023142104, 35.1663142104, 35.2716142104, 35.2653142104, 35.1708142104, 
35.2500142104, 35.2797142104, 35.1681142104, 35.2599142104, 35.2779142104, 
35.2608142104, 35.2338142104, 35.2761142104, 35.2320142104, 35.1861142104, 
35.1744142104, 35.1681142104, 35.2050142104, 35.2770142104, 35.2185142104, 
35.2356142104, 35.1753142104, 35.1978142104, 35.1978142104, 35.1708142104, 
35.2185142104, 35.2518142104, 35.2635142104, 35.2149142104, 35.2734142104, 
35.1816142104, 35.2689142104, 35.2482142104, 35.1699142104, 35.1996142104, 
35.1906142104, 35.2176142104, 35.2716142104, 35.2608142104, 35.2500142104, 
35.2086142104, 35.2374142104, 35.2518142104, 35.1969142104, 35.2491142104, 
35.1663142104, 35.1717142104, 35.2788142104, 35.2761142104, 35.2032142104, 
35.2725142104, 35.2050142104, 35.2311142104, 35.1699142104, 35.1672142104, 
35.2149142104, 35.2392142104, 35.2707142104, 35.2653142104, 35.2383142104, 
35.2473142104, 35.2041142104, 35.2194142104, 35.2590142104, 35.2788142104, 
35.2572142104, 35.2788142104, 35.2482142104, 35.2743142104, 35.2041142104, 
35.2527142104, 35.1753142104, 35.2635142104, 35.2617142104, 35.2599142104, 
35.2608142104, 35.2779142104, 35.2707142104, 35.2581142104, 35.1753142104, 
35.1636142104, 35.2608142104, 35.2725142104, 35.2716142104, 35.2752142104, 
35.1789142104, 35.2635142104, 35.2662142104, 35.1636142104, 35.1996142104, 
35.2257142104, 35.2131142104, 35.1789142104, 35.1861142104, 35.2770142104, 
35.2662142104, 35.1951142104, 35.2572142104, 35.1978142104, 35.1960142104, 
35.1906142104, 35.2572142104, 35.2788142104, 35.1780142104, 35.2500142104, 
35.2644142104, 35.2608142104, 35.2743142104, 35.2590142104, 35.2788142104, 
35.2248142104, 35.2185142104, 35.2365142104, 35.2635142104, 35.1681142104, 
35.2779142104, 35.1681142104, 35.2302142104, 35.2014142104, 35.2689142104, 
35.1735142104, 35.2608142104, 35.2509142104, 35.2572142104, 35.2410142104, 
35.2743142104, 35.1780142104, 35.2698142104, 35.1816142104, 35.2338142104, 
35.2329142104, 35.2302142104, 35.2779142104, 35.1951142104, 35.1762142104, 
35.2743142104, 35.1717142104, 35.2419142104, 35.2626142104, 35.2707142104, 
35.2743142104, 35.1708142104, 35.2374142104, 35.2122142104, 35.2185142104, 
35.2005142104, 35.1654142104, 35.2743142104, 35.1843142104, 35.1906142104, 
35.2131142104, 35.2338142104, 35.1681142104, 35.2536142104, 35.2725142104, 
35.2698142104, 35.2716142104, 35.1735142104, 35.2797142104, 35.1771142104, 
35.2230142104, 35.2671142104, 35.1825142104, 35.2473142104, 35.2374142104, 
35.2671142104, 35.2302142104, 35.2788142104, 35.1942142104, 35.2725142104, 
35.1852142104, 35.2194142104, 35.2203142104, 35.2536142104, 35.1879142104, 
35.1762142104, 35.1663142104, 35.1843142104, 35.2014142104, 35.2365142104, 
35.2689142104, 35.2149142104, 35.2779142104, 35.2662142104, 35.2761142104, 
35.2455142104, 35.2563142104), z = c(5.31467387080193, 4.34625959396362, 
4.58369427919388, 4.70451527833939, 4.2735670208931, 4.76539641618729, 
2.8935980796814, 4.67724108695984, 5.27303981781006, 0.630983829498291, 
4.79047000408173, 2.09721422195435, 4.52427738904953, 4.53539228439331, 
1.54016017913818, 4.76389497518539, 4.8683842420578, 0.896637439727783, 
4.89864695072174, 5.62727174907923, 4.28670394420624, 3.1602041721344, 
5.73023986816406, 4.55051273107529, 3.76310670375824, 4.59167301654816, 
3.28284633159637, 2.04889702796936, 4.73470735549927, 4.37481647729874, 
6.00706185400486, 5.57783985137939, 1.48552584648132, 1.55653357505798, 
5.05531674623489, 1.70913767814636, 5.79120016098022, 2.97318363189697, 
4.97046339511871, 4.66317200660706, 3.3507684469223, 5.12686407566071, 
4.76374268531799, 4.56264942884445, 2.79924654960632, 4.65423792600632, 
4.5135645866394, 4.23350596427917, 4.6081919670105, 0.890271663665771, 
5.76071977615356, 5.16741722822189, 1.23856163024902, 4.45028865337372, 
4.87926864624023, 4.6028990149498, 0, 0.831468105316162, 5.2080562710762, 
2.14837121963501, 5.00361204147339, 5.57783985137939, 5.73023986816406, 
4.21200549602509, 0.269620418548584, 4.7934627532959, 5.58789567276835, 
5.14037820696831, 5.08842647075653, 4.50527513027191, 4.15670019388199, 
3.2786693572998, 4.72973614931107, 5.69976043701172, 3.23668742179871, 
4.77672970294952, 5.12345796823502, 3.25704514980316, 4.6315136551857, 
4.52515780925751, 4.29206645488739, 2.15760660171509, 4.55804908275604, 
4.68271720409393, 2.66157460212708, 4.54937362670898, 5.51688003540039, 
2.79254460334778, 5.68075431883335, 4.49042046070099, 4.96170979738235, 
5.00219851732254, 4.53122925758362, 4.43848550319672, 5.8667007535696, 
4.58079540729523, 4.44558900594711, 5.42543983459473, 5.59390576183796, 
5.05257028341293, 2.28136348724365, 3.1872661113739, 4.74272590875626, 
5.04446756839752, 1.94034481048584, 4.60258036851883, 4.79565078020096, 
4.6997783780098, 4.46191477775574, 4.92535838484764, 3.30235958099365, 
0.920547962188721, 4.39491581916809, 5.71588009409606, 4.65802758932114, 
4.6885626912117, 2.06293773651123, 5.73023986816406, 0.525022029876709, 
1.13839340209961, 4.34047496318817, 5.51688003540039, 3.00805258750916, 
5.69976043701172, 3.74251461029053, 1.92474770545959, 4.7151911854744, 
1.71572232246399, 3.55604326725006, 1.52818441390991, 4.95974096655846, 
4.59859126806259, 1.45192384719849, 4.76373684406281, 0.282346725463867, 
5.73023986816406, 5.91938352584839, 4.48525714874268, 4.46559798717499, 
4.67939454317093, 4.84232640266418, 5.42543983459473, 5.41280755400658, 
5.48640012741089, 4.66426563262939, 5.79120016098022, 4.2082724571228, 
5.27303981781006, 4.98601269721985, 4.77032375335693, 5.42543983459473, 
4.4570124745369, 5.21208000183105, 5.73023986816406, 4.59184503555298, 
4.32232999801636, 0.9410080909729, 0.150956630706787, 4.8717457652092, 
4.42849510908127, 5.15674743056297, 4.47270715236664, 4.81305307149887, 
0.73227596282959, 3.16422319412231, 2.10066604614258, 0.586078643798828, 
1.92737627029419, 5.19454109668732, 4.5707870721817, 3.72820401191711, 
1.88514804840088, 5.55489002354443, 2.93064141273499, 1.66563129425049, 
5.48640012741089, 5.25005878508091, 4.77536559104919, 4.3696174621582, 
4.51080012321472, 4.87574273347855, 0.914746761322021, 4.77571392059326, 
4.57680332660675, 5.59001660346985, 4.81048429012299, 4.77745586633682, 
5.46453711390495, 3.50530898571014, 0.752546787261963, 4.26047706604004, 
4.85960680246353, 4.90869438648224, 5.15454277396202, 1.95869898796082, 
5.00777852535248, 4.27679842710495, 4.14364790916443, 4.63693344593048, 
5.42543983459473, 1.90830183029175, 4.54410570859909, 3.13861393928528, 
4.6024067401886, 5.30351972579956, 4.50214201211929, 0.791789054870605, 
4.50027042627335, 4.63502502441406, 2.97292304039001, 5.7731208242476, 
4.94388997554779, 4.76399433612823, 3.4011424779892, 5.34076273441315, 
2.22124743461609, 4.80799663066864, 5.67204789817333, 4.43646955490112, 
5.48640012741089, 4.84772700071335, 4.58422708511353, 4.7135603427887, 
3.82976019382477, 4.66448211669922, 4.54370057582855, 0.00195693969726562, 
5.54736042022705, 4.60726022720337, 3.58768892288208, 4.8764830827713, 
5.66900500655174, 5.07479965686798, 4.6387225985527, 4.93371438980103, 
5.48640012741089, 3.76119720935822, 4.4458891749382, 2.91235995292664, 
4.67050659656525, 4.85345482826233, 4.5827631354332, 4.51758623123169, 
1.75074982643127, 4.31508845090866, 4.22438013553619, 4.56293421983719, 
3.22379016876221, 4.88745713233948, 4.78673070669174, 4.52847295999527, 
4.68039166927338, 1.83934593200684, 4.73581695556641, 4.76741009950638, 
4.07559096813202, 3.17954707145691, 5.33400011062622, 4.66140818595886, 
5.86034432053566, 4.72919601202011, 5.48635681346059, 3.20941305160522, 
4.5287309885025, 5.05826050043106, 4.49347013235092, 5.51688003540039, 
4.30233198404312, 2.62060976028442, 4.66808903217316, 5.64647451043129, 
4.09487569332123, 4.88485026359558, 2.10063910484314, 2.62828969955444, 
3.02204990386963, 3.99784195423126, 5.36448001861572, 4.62278580665588, 
5.76130094006658, 4.68111699819565, 4.83797597885132, 4.66422927379608, 
4.44494259357452, 3.08159780502319, 5.1077236533165, 4.6174932718277, 
4.67420721054077, 5.614054068923, 4.76848465204239, 4.8661670088768, 
4.43934267759323, 5.57783985137939, 1.91603922843933, 4.78233218193054, 
4.85003250837326, 4.51080012321472, 4.71553140878677, 4.98112535476685, 
5.17412561178207, 5.21781948208809, 1.14746403694153, 3.32744956016541, 
4.45867002010345, 4.86955207586288, 4.99435037374496, 4.97867822647095, 
1.49956655502319, 4.38389599323273, 5.50725115835667, 4.60056006908417, 
0.112645149230957, 4.73868656158447, 5.21447616070509, 1.49178910255432, 
2.8282356262207, 4.77886211872101, 4.5970903635025, 1.20574378967285, 
4.73832929134369, 3.63953804969788, 5.30351972579956, 1.99566006660461, 
5.10843276977539, 5.33738869428635, 2.54613375663757, 4.96769207715988, 
5.67653122311458, 5.69976043701172, 3.96093916893005, 4.49153220653534, 
5.18590247631073, 4.21293151378632, 4.83920061588287, 4.56807965040207, 
4.81394976377487, 4.38528192043304, 4.9524108171463, 2.44894027709961, 
1.70766758918762, 5.30351972579956, 4.64997673034668, 4.63633984327316, 
4.39739108085632, 4.44371354579926, 5.63880014419556, 5.51688003540039, 
4.87863463163376, 2.59432148933411, 4.4809011220932, 1.32180666923523, 
5.48640012741089, 4.9579222202301, 4.17376208305359, 5.18672567605972, 
2.368168592453, 3.32596528530121, 4.23980379104614, 3.2810378074646, 
4.4826158285141, 5.63880014419556, 4.86251252889633, 4.93896371126175, 
2.19712901115417, 4.69487404823303, 4.26269292831421, 4.6703092455864, 
4.89603075385094, 3.82411479949951, 5.73023986816406, 3.07141757011414, 
4.36424767971039, 5.23844136297703, 4.4144321680069, 3.5862683057785, 
4.35215854644775, 5.46322771906853, 5.80012210458517, 5.38524529337883, 
4.54574501514435, 5.27813774347305, 2.31025171279907, 4.59240686893463, 
4.22704839706421, 4.41294676065445, 4.57621592283249, 3.81190705299377, 
5.73023986816406, 5.42543983459473, 5.04020142555237, 3.54105401039124, 
4.75436663627625, 0.635653495788574, 5.42543983459473, 4.52608078718185, 
4.78534460067749, 5.93245315551758, 2.32044458389282, 2.10873103141785, 
0, 0.978370189666748, 4.60127222537994, 5.70081361476332, 5.35931111034006, 
3.56547355651855, 4.53643822669983, 2.54905486106873, 4.87715196609497, 
5.44876557588577)), row.names = c(2093L, 20483L, 27652L, 12615L, 
26156L, 6983L, 25797L, 6972L, 25774L, 14244L, 1057L, 27493L, 
18635L, 19735L, 26753L, 5052L, 5060L, 24685L, 7513L, 4722L, 21441L, 
472L, 1456L, 15986L, 27260L, 16191L, 19578L, 21475L, 16374L, 
22401L, 3473L, 8686L, 25434L, 26566L, 2126L, 22030L, 2497L, 22022L, 
5318L, 3810L, 27992L, 5074L, 4186L, 16744L, 23764L, 21091L, 11791L, 
23195L, 5022L, 23014L, 1417L, 7757L, 21648L, 13420L, 1848L, 17517L, 
23931L, 23739L, 2795L, 28000L, 2179L, 8216L, 2835L, 24289L, 22415L, 
7745L, 7479L, 7494L, 1140L, 15008L, 25798L, 464L, 11993L, 6665L, 
27491L, 10302L, 6692L, 22805L, 28173L, 7987L, 21831L, 25430L, 
4736L, 5310L, 15426L, 1879L, 10702L, 19390L, 391L, 8464L, 2533L, 
2136L, 11357L, 18265L, 5000L, 13627L, 25782L, 14407L, 2451L, 
5064L, 28183L, 20113L, 10301L, 3870L, 144L, 11145L, 1444L, 11780L, 
16938L, 14631L, 27669L, 14439L, 21053L, 2473L, 9167L, 7491L, 
492L, 2480L, 24689L, 24499L, 20675L, 11556L, 19570L, 3506L, 23922L, 
497L, 4747L, 14440L, 24137L, 15427L, 16565L, 7762L, 25814L, 2817L, 
1881L, 2791L, 3788L, 8462L, 19198L, 14429L, 1076L, 15389L, 3477L, 
12807L, 14827L, 2152L, 22813L, 25217L, 3237L, 2127L, 16740L, 
14811L, 26700L, 2150L, 16569L, 11985L, 21072L, 28012L, 2182L, 
14615L, 9602L, 5304L, 6718L, 24682L, 824L, 23371L, 27088L, 16776L, 
62L, 14023L, 26528L, 28014L, 8457L, 24329L, 16010L, 13613L, 16359L, 
8012L, 14609L, 7459L, 450L, 24118L, 4504L, 8701L, 65L, 10085L, 
5355L, 367L, 26545L, 24684L, 23732L, 11147L, 5047L, 7202L, 24303L, 
110L, 27103L, 2518L, 10946L, 14213L, 25072L, 13441L, 24889L, 
6957L, 22205L, 13017L, 26013L, 19550L, 12405L, 27113L, 3127L, 
7255L, 401L, 27269L, 3794L, 26378L, 726L, 4428L, 93L, 13816L, 
782L, 15792L, 20117L, 27635L, 3221L, 5332L, 26723L, 9606L, 87L, 
27306L, 6974L, 719L, 6671L, 13432L, 1433L, 13812L, 23544L, 25968L, 
27262L, 19556L, 1058L, 16754L, 13040L, 25813L, 21055L, 21056L, 
26727L, 16775L, 9182L, 5925L, 17523L, 2505L, 24488L, 4167L, 10056L, 
26919L, 20697L, 22585L, 16950L, 3116L, 6724L, 9607L, 18847L, 
12606L, 9171L, 21269L, 9820L, 27658L, 26550L, 408L, 1471L, 19917L, 
2851L, 19576L, 14041L, 26926L, 27473L, 17500L, 12219L, 3456L, 
5373L, 12408L, 10295L, 19755L, 16578L, 7240L, 458L, 7776L, 379L, 
10060L, 2132L, 19770L, 8915L, 25820L, 5862L, 6473L, 6936L, 6716L, 
809L, 3553L, 7522L, 25828L, 28180L, 6690L, 2882L, 3172L, 1846L, 
25080L, 5870L, 5053L, 28171L, 20688L, 15206L, 17905L, 25085L, 
23572L, 1123L, 5045L, 21645L, 7774L, 21081L, 21433L, 22614L, 
7773L, 406L, 25262L, 9628L, 5572L, 6666L, 2172L, 7224L, 432L, 
15416L, 16755L, 12832L, 5929L, 27268L, 742L, 27301L, 14242L, 
20289L, 4151L, 26171L, 6686L, 9377L, 7711L, 11768L, 2206L, 25247L, 
3846L, 24498L, 13413L, 13621L, 14234L, 759L, 21644L, 25621L, 
2169L, 26522L, 11565L, 6148L, 3565L, 2160L, 26741L, 12630L, 18087L, 
16752L, 20506L, 27819L, 2145L, 23951L, 22592L, 17894L, 13426L, 
27304L, 8687L, 2801L, 3792L, 3147L, 26160L, 112L, 25450L, 15799L, 
4750L, 24293L, 10272L, 12636L, 4707L, 14211L, 407L, 21836L, 2850L, 
23741L, 16547L, 16367L, 8724L, 23156L, 25612L, 27645L, 23932L, 
20308L, 12818L, 4097L, 17506L, 819L, 5040L, 1530L, 10728L, 8003L
), class = "data.frame")


Solution

  • So, i have two ways to filter away or assign all the unwanted points, depending on what is availably. The trick in both is to have a definition of the unwanted area.

    1. If the water you are talking about is the ocean there is a simple if not slow approach. with the package "ggOceanMaps"
    library(ggOceanMaps)
    
    #get the distance to land, if the distanced is greater than 0 it is on water and should be NA'ed Blockquote
    
    pts <- dist2land(data_for_plot)
    pts[pts$ldist > 0, "z"] <- NA
    
    1. you need a shapefile that contains the defenition of the water
    library(sf)
    library(dplyr)
    #convert to spatial
    pts <- data_for_plot %>% 
      sf::st_as_sf(coords = c("x","y")) %>% 
      sf::st_set_crs(4326) #projection
    
    #load in your shapefile of water
    poly <- st_read(...)
    
    #finde points in polygon
    points_in_polygons <-lengths(st_intersects(pts, poly)) > 0
    pts$in_polygon <- points_in_polygons
    
    #points in the polygon set to NA
    pts[pts$in_polygon, "z"] <- NA