Mapping 2D to 3D

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
13 messages Options
H-2
Reply | Threaded
Open this post in threaded view
|

Mapping 2D to 3D

H-2
I am trying to understand how to map 2D to 3D using ggplot() and eventually plot_gg(). I am, however, stuck on understanding how to express the third variable to be mapped. This example:

ggdiamonds = ggplot(diamonds, aes(x, depth)) +
stat_density_2d(aes(fill = stat(nlevel)),
geom = "polygon", n = 100, bins = 10,contour = TRUE) +
facet_wrap(clarity~.) +
scale_fill_viridis_c(option = "A")

uses a variable nlevel that I now understand is calculated during the building of the ggplot but I have not figured out from where it is calculated or how to specify a variable of my choosing.

Does anyone have a good reference for understanding how to specify this variable? Most examples on the 'net seem to use the same dataset but do not specify this particular aspect...

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Mapping 2D to 3D

aBBy Spurdle, ⍺XY
I'm not familiar with the gg graphics system.
However, I am familiar with density estimation, and density visualization.

There is *no* third variable, as such.
But rather, density estimates, which in this context, would usually be a matrix.
(And are computed inside the plotting or density estimation functions).

The documentation for the function you've used, says it uses  MASS::kde2d().
This does just that, returns an object, which contains a density matrix.
(Refer to the help file for kde2d).

Of course, there's no reason why one can't create a third variable,
from a mathematical perspective.
e.g. d, z, h, fv, or whatever you prefer...
And then set z = fh (x, y).

But there's no reason for the user to do that when using the plotting function.

Note that there are situations where one might want to set the limits
of the plot.
And set the breaks, colors, and color key.
e.g. Creating two or more plots, and putting them next to each other,
for comparison purposes.


On Fri, Sep 18, 2020 at 2:17 PM H <[hidden email]> wrote:

>
> I am trying to understand how to map 2D to 3D using ggplot() and eventually plot_gg(). I am, however, stuck on understanding how to express the third variable to be mapped. This example:
>
> ggdiamonds = ggplot(diamonds, aes(x, depth)) +
> stat_density_2d(aes(fill = stat(nlevel)),
> geom = "polygon", n = 100, bins = 10,contour = TRUE) +
> facet_wrap(clarity~.) +
> scale_fill_viridis_c(option = "A")
>
> uses a variable nlevel that I now understand is calculated during the building of the ggplot but I have not figured out from where it is calculated or how to specify a variable of my choosing.
>
> Does anyone have a good reference for understanding how to specify this variable? Most examples on the 'net seem to use the same dataset but do not specify this particular aspect...
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Mapping 2D to 3D

aBBy Spurdle, ⍺XY
> But there's no reason for the user to do that when using the plotting function.

I should amend the above.
There's no reason for the user to do that (compute a third "variable"
representing density), if using a high level plotting function, that's
designed to compute the density for you.

It is possible to do it in two or more steps.
(Compute the density matrix, then plot it).

Again, refer to the help file for kde2d.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Mapping 2D to 3D

Jeff Newmiller
In reply to this post by H-2
No, but fortunately you are off in the weeds. Density has an internally-computed "z" coordinate... you should be looking at ?geom_contour.

On September 17, 2020 7:17:33 PM PDT, H <[hidden email]> wrote:

>I am trying to understand how to map 2D to 3D using ggplot() and
>eventually plot_gg(). I am, however, stuck on understanding how to
>express the third variable to be mapped. This example:
>
>ggdiamonds = ggplot(diamonds, aes(x, depth)) +
>stat_density_2d(aes(fill = stat(nlevel)),
>geom = "polygon", n = 100, bins = 10,contour = TRUE) +
>facet_wrap(clarity~.) +
>scale_fill_viridis_c(option = "A")
>
>uses a variable nlevel that I now understand is calculated during the
>building of the ggplot but I have not figured out from where it is
>calculated or how to specify a variable of my choosing.
>
>Does anyone have a good reference for understanding how to specify this
>variable? Most examples on the 'net seem to use the same dataset but do
>not specify this particular aspect...
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Mapping 2D to 3D

Carlos Ortega
In reply to this post by H-2
Hi,

There are some further references in the own "RStudio Community" and in
StackOverflow:

   - https://community.rstudio.com/t/options-to-stat-density-2d/792/4
   -
   https://stackoverflow.com/questions/32206623/what-does-level-mean-in-ggplotstat-density2d

Kind Regards,
Carlos.


On Fri, Sep 18, 2020 at 4:17 AM H <[hidden email]> wrote:

> I am trying to understand how to map 2D to 3D using ggplot() and
> eventually plot_gg(). I am, however, stuck on understanding how to express
> the third variable to be mapped. This example:
>
> ggdiamonds = ggplot(diamonds, aes(x, depth)) +
> stat_density_2d(aes(fill = stat(nlevel)),
> geom = "polygon", n = 100, bins = 10,contour = TRUE) +
> facet_wrap(clarity~.) +
> scale_fill_viridis_c(option = "A")
>
> uses a variable nlevel that I now understand is calculated during the
> building of the ggplot but I have not figured out from where it is
> calculated or how to specify a variable of my choosing.
>
> Does anyone have a good reference for understanding how to specify this
> variable? Most examples on the 'net seem to use the same dataset but do not
> specify this particular aspect...
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Mapping 2D to 3D

H-2
On 09/18/2020 03:08 AM, Carlos Ortega wrote:

> Hi,
>
> There are some further references in the own "RStudio Community" and in StackOverflow:
>
>   * https://community.rstudio.com/t/options-to-stat-density-2d/792/4
>   * https://stackoverflow.com/questions/32206623/what-does-level-mean-in-ggplotstat-density2d
>
> Kind Regards,
> Carlos.
>
>
> On Fri, Sep 18, 2020 at 4:17 AM H <[hidden email] <mailto:[hidden email]>> wrote:
>
>     I am trying to understand how to map 2D to 3D using ggplot() and eventually plot_gg(). I am, however, stuck on understanding how to express the third variable to be mapped. This example:
>
>     ggdiamonds = ggplot(diamonds, aes(x, depth)) +
>     stat_density_2d(aes(fill = stat(nlevel)),
>     geom = "polygon", n = 100, bins = 10,contour = TRUE) +
>     facet_wrap(clarity~.) +
>     scale_fill_viridis_c(option = "A")
>
>     uses a variable nlevel that I now understand is calculated during the building of the ggplot but I have not figured out from where it is calculated or how to specify a variable of my choosing.
>
>     Does anyone have a good reference for understanding how to specify this variable? Most examples on the 'net seem to use the same dataset but do not specify this particular aspect...
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> mailing list -- To UNSUBSCRIBE and more, see
>     https://stat.ethz.ch/mailman/listinfo/r-help
>     PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>     and provide commented, minimal, self-contained, reproducible code.
>
Yes, these are two of the links I found but unfortunately they do not explain enough. In the second link there is the reference to an internal dataframe etc. but I can still not figure out how to specify a z-variable of my choosing when I am creating this type of plot...


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Mapping 2D to 3D

H-2
In reply to this post by Jeff Newmiller
On 09/18/2020 02:26 AM, Jeff Newmiller wrote:

> No, but fortunately you are off in the weeds. Density has an internally-computed "z" coordinate... you should be looking at ?geom_contour.
>
> On September 17, 2020 7:17:33 PM PDT, H <[hidden email]> wrote:
>> I am trying to understand how to map 2D to 3D using ggplot() and
>> eventually plot_gg(). I am, however, stuck on understanding how to
>> express the third variable to be mapped. This example:
>>
>> ggdiamonds = ggplot(diamonds, aes(x, depth)) +
>> stat_density_2d(aes(fill = stat(nlevel)),
>> geom = "polygon", n = 100, bins = 10,contour = TRUE) +
>> facet_wrap(clarity~.) +
>> scale_fill_viridis_c(option = "A")
>>
>> uses a variable nlevel that I now understand is calculated during the
>> building of the ggplot but I have not figured out from where it is
>> calculated or how to specify a variable of my choosing.
>>
>> Does anyone have a good reference for understanding how to specify this
>> variable? Most examples on the 'net seem to use the same dataset but do
>> not specify this particular aspect...
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

But looking at the code in my message above, how does one know what stat(nlevel) refers to? What if I wanted to map another variable in this particular dataset??

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Mapping 2D to 3D

Jeff Newmiller
When dealing with a 2-d density plot, the z variable is a predefined function of your x and y data, it is not something you can specify. If you want to specify z, then you need to use geom_contour. You appear to need to study the theory of kernel density estimates, which is off topic here. (Technically contributed packages like ggplot2 are off topic here also, though sometimes people will answer questions about them anyway.)

On September 18, 2020 6:34:43 PM PDT, H <[hidden email]> wrote:

>On 09/18/2020 02:26 AM, Jeff Newmiller wrote:
>> No, but fortunately you are off in the weeds. Density has an
>internally-computed "z" coordinate... you should be looking at
>?geom_contour.
>>
>> On September 17, 2020 7:17:33 PM PDT, H <[hidden email]>
>wrote:
>>> I am trying to understand how to map 2D to 3D using ggplot() and
>>> eventually plot_gg(). I am, however, stuck on understanding how to
>>> express the third variable to be mapped. This example:
>>>
>>> ggdiamonds = ggplot(diamonds, aes(x, depth)) +
>>> stat_density_2d(aes(fill = stat(nlevel)),
>>> geom = "polygon", n = 100, bins = 10,contour = TRUE) +
>>> facet_wrap(clarity~.) +
>>> scale_fill_viridis_c(option = "A")
>>>
>>> uses a variable nlevel that I now understand is calculated during
>the
>>> building of the ggplot but I have not figured out from where it is
>>> calculated or how to specify a variable of my choosing.
>>>
>>> Does anyone have a good reference for understanding how to specify
>this
>>> variable? Most examples on the 'net seem to use the same dataset but
>do
>>> not specify this particular aspect...
>>>
>>> ______________________________________________
>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>
>But looking at the code in my message above, how does one know what
>stat(nlevel) refers to? What if I wanted to map another variable in
>this particular dataset??
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Mapping 2D to 3D

H-2
On 09/19/2020 12:42 AM, Jeff Newmiller wrote:

> When dealing with a 2-d density plot, the z variable is a predefined function of your x and y data, it is not something you can specify. If you want to specify z, then you need to use geom_contour. You appear to need to study the theory of kernel density estimates, which is off topic here. (Technically contributed packages like ggplot2 are off topic here also, though sometimes people will answer questions about them anyway.)
>
> On September 18, 2020 6:34:43 PM PDT, H <[hidden email]> wrote:
>> On 09/18/2020 02:26 AM, Jeff Newmiller wrote:
>>> No, but fortunately you are off in the weeds. Density has an
>> internally-computed "z" coordinate... you should be looking at
>> ?geom_contour.
>>> On September 17, 2020 7:17:33 PM PDT, H <[hidden email]>
>> wrote:
>>>> I am trying to understand how to map 2D to 3D using ggplot() and
>>>> eventually plot_gg(). I am, however, stuck on understanding how to
>>>> express the third variable to be mapped. This example:
>>>>
>>>> ggdiamonds = ggplot(diamonds, aes(x, depth)) +
>>>> stat_density_2d(aes(fill = stat(nlevel)),
>>>> geom = "polygon", n = 100, bins = 10,contour = TRUE) +
>>>> facet_wrap(clarity~.) +
>>>> scale_fill_viridis_c(option = "A")
>>>>
>>>> uses a variable nlevel that I now understand is calculated during
>> the
>>>> building of the ggplot but I have not figured out from where it is
>>>> calculated or how to specify a variable of my choosing.
>>>>
>>>> Does anyone have a good reference for understanding how to specify
>> this
>>>> variable? Most examples on the 'net seem to use the same dataset but
>> do
>>>> not specify this particular aspect...
>>>>
>>>> ______________________________________________
>>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>> But looking at the code in my message above, how does one know what
>> stat(nlevel) refers to? What if I wanted to map another variable in
>> this particular dataset??
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

Understood, I just began looking at the volcano dataset which uses geom_contour. I now realize that the function stat_density_2d really maps a heatmap of a computed variable.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Mapping 2D to 3D

aBBy Spurdle, ⍺XY
> 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

If I hadn't read the rest of this thread, I wouldn't know what you
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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Mapping 2D to 3D

H-2
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
> If I hadn't read the rest of this thread, I wouldn't know what you
> 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)) +
 scale_fill_gradientn("Z",colours = terrain.colors(10)) +
 coord_fixed()
print(ggvolcano)

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Mapping 2D to 3D

aBBy Spurdle, ⍺XY
> 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)) +
>  scale_fill_gradientn("Z",colours = terrain.colors(10)) +
>  coord_fixed()
> print(ggvolcano)

Yesturday, I watched the movie Tenet.

WARNING: SPOILER ALERT

Everything's going forwards and backwards at the same time.
This code reminds me of that movie.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Mapping 2D to 3D

aBBy Spurdle, ⍺XY
In reply to this post by H-2
Hi H,

I probably owe you an apology.
I was just reading the geom_contour documentation.
It's difficult to follow.

Base R functions, my functions, and pretty much everyone's functions,
take a matrix as input.
But as far as I can tell, geom_contour wants a data.frame with three
{x, y and z} coordinates.
It's not clear how the data in the data.frame is mapped onto the {x,
y, and z} coordinate system.

Also, it uses density estimation in *all* its examples.
Making it difficult for inexperienced users to tell the difference
between contour plots (generally) and density visualization.

I would suggest base R functions as a better starting point:

contour
filled.contour
image
heatmap
persp

I have a package named barsurf, with (almost) density-free examples:
https://cran.r-project.org/web/packages/barsurf/vignettes/barsurf.pdf

Additionally, there are functions in the lattice package, and possibly
the rgl package.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.