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...
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 <agents at meddatainc.com> 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... > > ______________________________________________ > R-help at r-project.org 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 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.
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 <agents at meddatainc.com> 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... > >______________________________________________ >R-help at r-project.org 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.
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 <agents at meddatainc.com> 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... > > ______________________________________________ > R-help at r-project.org 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]]
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 <agents at meddatainc.com <mailto:agents at meddatainc.com>> 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... > > ______________________________________________ > R-help at r-project.org <mailto:R-help at r-project.org> 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]]
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 <agents at meddatainc.com> 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... >> >> ______________________________________________ >> R-help at r-project.org 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??