# [R] Mapping 2D to 3D

H @gent@ @end|ng |rom medd@t@|nc@com
Mon Sep 21 03:44:19 CEST 2020

```On 09/19/2020 04:33 PM, Abby Spurdle wrote:
>> Understood
> I'd recommend you try to be more precise.
>
>> I just began looking at the volcano dataset which uses geom_contour.
> The volcano dataset does *not* use geom_contour.
> However, the help file for the volcano dataset, does use the
> filled.contour function, in its example.
>
>> I now realize that the function stat_density_2d really maps a heatmap
> meant by "maps" a heatmap.
>
> The kde2d function returns a list, containing a density matrix.
> (As per my previous post).
>
> The plotting functions, compute the density via the above density
> estimation function, and then plot that density, in some form.
>
> I suppose you could say the plotting functions map observations to
> density estimates, then map the density estimates to contours and/or
> other graphic data, and then map the graphic data to a plot, which is
> seen by a user...
> ...but it's probably easier to just say plot the density.
>
>> of a computed variable.
> It's rare in probability theory to refer to density as a "variable".
> (Which is relevant because density estimates are estimates of
> probability distributions).
>
> However, it is common in computer graphics and geometry, to use "z"
> for a "third variable".
> And in applied statistics and data science, "variable" could mean anything...
> So, be careful there...
>
> Based on your posts, I take it you want to plot a function of two
> variables (or plot a matrix of values), using a 2d plot.
>
> There are a number of options here.
>
> Contour plots.
> Filled contour plots.
> Heatmaps.
> Plots using hex/other binning.
> Maybe others...?
>
> Additionally, there are 3d plots, such as surface plots.
>
> And I note that it's possible to plot contour lines on top of
> color-filled contours or heatmaps.

I was looking at this example which uses geom_contour():

ggvolcano = volcano %>%
reshape2::melt() %>%
ggplot() +
geom_tile(aes(x=Var1,y=Var2,fill=value)) +
geom_contour(aes(x=Var1,y=Var2,z=value),color="black") +
scale_x_continuous("X",expand = c(0,0)) +
scale_y_continuous("Y",expand = c(0,0)) +