A proper way to plot climate data on an irregular grid : <p>I have asked this question as part of the <a href="https://stackoverflow.com/questions/48952435/efficient-way-to-plot-data-on-an-irregular-grid">Efficient way to plot data on an irregular grid</a> question, but the general feedback was to split the original question in more manageable chunks. Hence, this new question.</p>

<p>I work with satellite data organized on an irregular two-dimensional grid whose dimensions are scanline (along track dimension, i.e. Y axis) and ground pixel (across track dimension, i.e. X axis). Latitude and longitude information for each centre pixel are stored in auxiliary coordinate variables, as well as the four corners coordinate pairs (latitude and longitude coordinates are given on the WGS84 reference ellipsoid).</p>

<p>Let's build a toy data set, consisting in a 12x10 potentially irregular grid and associated surface temperature measurements. </p>

<pre><code>library(pracma) # for the meshgrid function
library(ggplot2)

num_sl &lt;- 12 # number of scanlines
num_gp &lt;- 10 # number of ground pixels
l &lt;- meshgrid(seq(from=-20, to=20, length.out = num_gp), 
              seq(from=30, to=60, length.out = num_sl))

lon &lt;- l[[1]] + l[[2]]/10
lat &lt;- l[[2]] + l[[1]]/10

data &lt;- matrix(seq(from = 30, to = 0, length.out = num_sl*num_gp), 
               byrow = TRUE, nrow = num_sl, ncol = num_gp) +
  matrix(runif(num_gp*num_sl)*6, nrow = num_sl, ncol = num_gp)

df &lt;- data.frame(lat=as.vector(lat), lon=as.vector(lon), temp=as.vector(data))
</code></pre>

<p>The <code>lon</code> and <code>lat</code> data contain the centre pixel coordinates as provided in the original product I'm working with, stored as two-dimensional matrix, whose axes are ground_pixel (X axis) and scanline (Y axis). The <code>data</code> matrix—same dimensions—contains my measurements. I then <em>flatten</em> the three matrix and store them in a data frame.</p>

<p>I would like to plot the ground pixels (as quadrilaterals) on a map, filled accordingly with the temperature measurement. </p>

<p>Using tiles I get:</p>

<pre><code>ggplot(df, aes(y=lat, x=lon, fill=temp)) + 
  geom_tile(width=2, height=2) +
  geom_point(size=.1) +
  borders('world', colour='gray50', size=.2) + 
  coord_quickmap(xlim=range(lon), ylim=range(lat)) +
  scale_fill_distiller(palette='Spectral') +
  theme_minimal()
</code></pre>

<p><a href="https://i.stack.imgur.com/zTkfW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/zTkfW.png" alt="Using tiles"></a></p>

<p>But that's not what I am after. I could play with <code>width</code> and <code>height</code> to make the tiles "touch" each other, but of course that wouldn't even come close to my desired goal, which is to plot the actual <em>projected</em> ground pixels on the map.<br>
Python's xarray can, for example, automatically infer the pixel boundaries given the pixel centre coordinates:</p>

<p><a href="https://i.stack.imgur.com/6iQa6.png" rel="noreferrer"><img src="https://i.stack.imgur.com/6iQa6.png" alt="Xarray solution"></a> </p>

<h2>Question</h2>

<p>Is there a way to achieve the same results in R, that is: having the pixel boundaries automatically inferred from the pixel centres, and plotting the pixels as filled polygons on a map? Maybe using the <code>sf</code> package? </p>

<p>I can see it done in the answer to this <a href="https://stackoverflow.com/questions/43612903/how-to-properly-plot-projected-gridded-data-in-ggplot2">question</a> but the answer that refers to using <code>sf</code> is a bit unclear to me, as it deals with different projections and potentially regular grids, whereas in my case I suppose I don't have to re-project my data, and, furthermore, my data is not on a regular grid.</p>

<p>If this is not possible, I suppose I can resort to use the pixel boundaries information in my products, but maybe that's a topic for another question should this one prove to be not easy to tackle. </p>
