6

I am trying to plot points from a df (xyz/latlonvalue) as raster over a geom_sf shapefile. I tried the answers from another post but none of them helped. The data structure is below.

First I try raster::rasterFromXYZ(df) then the code from the answer on the other page but it does not work. I get the same error message from each attempt.

d <- structure(list(x = c(-87.001233, -87.416633, -86.999683, -86.58395, 
-86.998233, -86.998233, -86.998233, -86.998233, -87.416633, -87.416633, 
-87.416633, -87.416633, -87.001233, -87.001233, -87.001233, -87.001233, 
-87.001233, -87.001233, -87.001233, -87.001233, -87.001233, -87.001233, 
-86.58395, -86.58395, -86.58395, -86.58395, -86.999683, -86.999683, 
-86.999683, -86.999683, -86.916517, -86.965333, -87.233917, -86.721362, 
-86.916517, -86.916517, -86.916517, -86.916517, -86.916517, -86.916517, 
-86.916517, -86.916517, -86.916517, -86.916517, -86.766867, -86.766867, 
-86.766867, -86.766867, -87.233917, -87.233917, -87.233917, -87.233917, 
-86.965333, -86.965333, -86.965333, -86.965333, -86.721362, -86.721362, 
-86.721362, -86.721362, -86.721362, -86.721362, -86.721362, -86.721362, 
-86.721362, -86.721362, -86.721362, -86.37455, -86.37455, -86.37455, 
-86.37455, -86.37455, -86.37545, -86.967667, -86.766717, -86.72265, 
-87.2327, -86.37545, -86.37545, -86.37545, -86.37545, -86.37545, 
-86.37545, -86.72265, -86.72265, -86.72265, -86.72265, -86.72265, 
-86.72265, -86.72265, -86.72265, -86.72265, -86.72265, -86.72265, 
-86.967667, -86.967667, -86.967667, -86.967667, -86.967667, -86.967667, 
-87.2327, -87.2327, -87.2327, -87.2327, -87.2327, -87.2327, -86.766717, 
-86.766717, -86.766717, -86.766717, -86.766717, -86.766717, -87.001817, 
-87.416083, -86.582933, -86.9173, -86.9173, -86.9173, -86.9173, 
-86.9173, -86.9173, -86.9173, -86.9173, -86.9173, -87.001817, 
-87.001817, -87.001817, -87.001817, -87.001817, -87.001817, -86.582933, 
-86.582933, -86.582933, -86.582933, -86.582933, -86.582933, -86.99865, 
-86.99865, -86.99865, -86.99865, -86.99865, -86.99865, -86.99865, 
-86.99865, -86.99865, -87.416083, -87.416083, -87.416083, -87.416083, 
-87.416083, -87.416083, -87.00025, -87.00025, -87.00025, -87.00025, 
-87.00025, -87.00025, -87.00025, -83.9164, -82.915883, -82.452167, 
-83.9164, -83.9164, -83.9164, -83.9164, -82.417183, -82.417183, 
-82.417183, -82.417183, -82.417183, -82.417183, -82.417183, -82.417183, 
-82.417183, -82.915883, -82.915883, -82.915883, -82.915883, -82.452167, 
-82.452167, -82.452167, -82.452167, -82.983533, -82.983533, -82.983533, 
-82.983533, -82.983533, -82.983533, -82.983533, -82.983533, -82.783667, 
-82.340983, -82.783667, -82.783667, -82.783667, -82.783667, -82.06085, 
-82.06085, -82.06085, -82.06085), y = c(42.731083, 42.732717, 
43.132683, 42.733483, 42.380783, 42.380783, 42.380783, 42.380783, 
42.732717, 42.732717, 42.732717, 42.732717, 42.731083, 42.731083, 
42.731083, 42.731083, 42.731083, 42.731083, 42.731083, 42.731083, 
42.731083, 42.731083, 42.733483, 42.733483, 42.733483, 42.733483, 
43.132683, 43.132683, 43.132683, 43.132683, 43.599517, 44.759817, 
44.14175, 44.737115, 43.599517, 43.599517, 43.599517, 43.599517, 
43.599517, 43.599517, 43.599517, 43.599517, 43.599517, 43.599517, 
44.09025, 44.09025, 44.09025, 44.09025, 44.14175, 44.14175, 44.14175, 
44.14175, 44.759817, 44.759817, 44.759817, 44.759817, 44.737115, 
44.737115, 44.737115, 44.737115, 44.737115, 44.737115, 44.737115, 
44.737115, 44.737115, 44.737115, 44.737115, 45.178767, 45.178767, 
45.178767, 45.178767, 45.178767, 45.177783, 44.759917, 44.090067, 
44.73645, 44.139717, 45.177783, 45.177783, 45.177783, 45.177783, 
45.177783, 45.177783, 44.73645, 44.73645, 44.73645, 44.73645, 
44.73645, 44.73645, 44.73645, 44.73645, 44.73645, 44.73645, 44.73645, 
44.759917, 44.759917, 44.759917, 44.759917, 44.759917, 44.759917, 
44.139717, 44.139717, 44.139717, 44.139717, 44.139717, 44.139717, 
44.090067, 44.090067, 44.090067, 44.090067, 44.090067, 44.090067, 
43.1333, 42.733717, 42.733333, 43.60065, 43.60065, 43.60065, 
43.60065, 43.60065, 43.60065, 43.60065, 43.60065, 43.60065, 43.1333, 
43.1333, 43.1333, 43.1333, 43.1333, 43.1333, 42.733333, 42.733333, 
42.733333, 42.733333, 42.733333, 42.733333, 42.733083, 42.733083, 
42.733083, 42.733083, 42.733083, 42.733083, 42.733083, 42.733083, 
42.733083, 42.733717, 42.733717, 42.733717, 42.733717, 42.733717, 
42.733717, 42.383767, 42.383767, 42.383767, 42.383767, 42.383767, 
42.383767, 42.383767, 45.750033, 45.449183, 45.27775, 45.750033, 
45.750033, 45.750033, 45.750033, 45.516667, 45.516667, 45.516667, 
45.516667, 45.516667, 45.516667, 45.516667, 45.516667, 45.516667, 
45.449183, 45.449183, 45.449183, 45.449183, 45.27775, 45.27775, 
45.27775, 45.27775, 45.1371, 45.1371, 45.1371, 45.1371, 45.1371, 
45.1371, 45.1371, 45.1371, 44.761967, 44.453917, 44.761967, 44.761967, 
44.761967, 44.761967, 44.740467, 44.740467, 44.740467, 44.740467
), z = c(10.81, 10.84, 10.84, 0, 10.81, 10.89, 10.76, 10.86, 
10.89, 10.83, 10.88, 10.8, 10.79, 10.79, 10.8, 10.82, 10.78, 
10.78, 10.82, 10.86, 10.8, 10.83, 10.87, 10.87, 10.88, 10.88, 
10.83, 10.8, 10.81, 10.81, 10.8, 10.83, 0, 0.02, 10.8, 10.8, 
10.78, 10.79, 10.81, 10.8, 10.81, 10.82, 10.79, 10.81, 10.88, 
10.89, 10.9, 10.88, 10.77, 10.78, 10.77, 10.78, 10.84, 10.83, 
10.83, 10.84, 10.75, 10.77, 10.76, 10.77, 10.78, 10.79, 10.76, 
10.77, 10.79, 10.78, 10.78, 10.93, 10.94, 10.91, 10.95, 10.94, 
10.73, 10.79, 10.9, 0.01, 0, 10.8, 10.9, 10.83, 10.71, 10.75, 
10.75, 10.89, 10.89, 10.89, 10.77, 10.66, 10.68, 10.67, 10.6, 
10.71, 10.68, 10.67, 10.81, 10.73, 10.74, 10.73, 10.69, 10.69, 
10.73, 10.68, 10.67, 10.77, 10.81, 10.8, 11.09, 10.94, 10.72, 
10.78, 10.83, 10.82, 10.72, 10.8, 10.8, 10.73, 10.7, 10.68, 10.75, 
10.78, 10.75, 10.74, 10.77, 10.75, 10.68, 10.69, 10.67, 10.76, 
10.8, 10.74, 11.06, 10.89, 10.73, 10.7, 10.72, 10.71, 10.78, 
10.8, 10.78, 10.7, 10.63, 10.6, 10.72, 10.7, 10.71, 10.83, 10.82, 
10.75, 10.72, 10.77, 10.73, 0, 10.84, 10.84, 10.73, 10.6, 10.74, 
10.77, 6, 6.33, 0, 6, 6, 6.06, 6, 6.19, 6.24, 6.21, 6.21, 6.21, 
6.21, 6.21, 6.22, 6.21, 6.35, 6.34, 6.33, 6.33, 6.37, 6.36, 6.36, 
6.35, 6.05, 6.01, 6.04, 6.03, 6.04, 6.04, 6.07, 6.05, 6.02, 0, 
6.08, 6.09, 6.13, 6.08, 6.18, 6.18, 6.11, 6.14)), .Names = c("x", 
"y", "z"), class = "data.frame", row.names = c(NA, -200L))
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
ecology
  • 606
  • 3
  • 9
  • 29
  • Can you include the actual code you used? You've only posed the data so far. – Z.Lin Oct 01 '18 at 03:52
  • 2
    If you want to use geom_raster, you don't need to turn the data into a `raster::raster` first. (the problem with rasterFromXYZ might be that the grid spacing is not perfectly even - perhaps a rounding error - I've had that problem) – Richard Telford Oct 01 '18 at 08:13
  • 2
    Your data contains only 30 distinct locations across the 200 rows, many of which are very close to each other and don't occur on a regular grid. You should check to make sure the x and y data hasn't been rounded or truncated somehow. Interpolating to a decent quality grid from this point data would be difficult. – obrl_soil Oct 23 '18 at 03:14

1 Answers1

0

Your data do not look very regular. See plot(d[,1:2]).

Instead of rasterFromXYZ you can use rasterize in which case you specify the required geometry and then transfer the values to it.

Depending on your goals, you may also use interpolate

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63