How to plot a heat map with irregular data in ordinates in ggplot?
<p>Suppose I want to plot the following data:</p>

<pre><code># First set of X coordinates
x &lt;- seq(0, 10, by = 0.2)
# Angles from 0 to 90 degrees
angles &lt;- seq(0, 90, length.out = 10)
# Convert to radian
angles &lt;- deg2rad(angles)
# Create an empty data frame
my.df &lt;- data.frame()
# For each angle, populate the data frame
for (theta in angles) {
  y &lt;- sin(x + theta)
  tmp &lt;- data.frame(x = x, y = y, theta = as.factor(theta))
  my.df &lt;- rbind(my.df, tmp)
}

x1 &lt;- seq(0, 12, by = 0.3)
y1 &lt;- sin(x1 - 0.5)
tmp &lt;- data.frame(x = x1, y = y1, theta = as.factor(-0.5))
my.df &lt;- rbind(my.df, tmp)

ggplot(my.df, aes(x, y, color = theta)) + geom_line()
</code></pre>

<p>That gives me a nice plot:</p>

<p><a href="https://i.stack.imgur.com/PEjwb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PEjwb.png" alt="enter image description here"></a></p>

<p>Now I want to draw a heat map out of this data set. There are tutorials <a href="http://rud.is/b/2016/02/14/making-faceted-heatmaps-with-ggplot2/" rel="nofollow noreferrer">here</a> and <a href="https://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/" rel="nofollow noreferrer">there</a> that do it using <code>geom_tile</code> to do it.</p>

<p>So, let's try:</p>

<pre><code># Convert the angle values from factors to numerics
my.df$theta &lt;- as.numeric(levels(my.df$theta))[my.df$theta]
ggplot(my.df, aes(theta, x)) + geom_tile(aes(fill = y)) + scale_fill_gradient(low = "blue", high = "red")
</code></pre>

<p><a href="https://i.stack.imgur.com/HAakT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HAakT.png" alt="Too bad"></a></p>

<p>That does not work, and the reason is that my x coordinates do not have the same step:</p>

<p><code>x &lt;- seq(0, 10, by = 0.2)</code> vs <code>x1 &lt;- seq(0, 12, by = 0.3)</code></p>

<p>But as soon as I use the same step <code>x1 &lt;- seq(0, 12, by = 0.2)</code>, it works:</p>

<p><a href="https://i.stack.imgur.com/h4PVc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/h4PVc.png" alt="Tada!"></a></p>

<p>I real life, my data sets are not regularly spaced (these are experimental data), but I still need to display them as a heat map. How can I do?</p>