[R] example of geom_contour() with function argument

Ranjan Maitra maitra at email.com
Wed Oct 11 06:06:54 CEST 2017


Here is an alternative using geom_polygon which captures the "spirit" of the fatter tails of the multivariate t (in my opinion, because the display is quantile-based). Note that I have modified the OP's question somewhat to use non-identity matrices.

I do came up with a few questions while creating this suggestions which are at the end. 

## code follows

nu <- 5  ## this is the degrees of freedom of the multivariate t. 

library(mvtnorm) 
library(ggplot2)

sig <- matrix(c(1, 0.5, 0.5, 1), ncol = 2)  ## this is the sigma parameter for the multivariate t

xx <- data.frame( rmvt(n = 100, df = c(nu, nu), sigma = sig)) ## generating the original sample

rtsq <- rowSums(x = matrix(rt(n = 2e6, df = nu)^2, ncol = 2)) ## generating the sample for the ellipse-quantiles. Note that this is a cumbersome calculation because it is the sum of two independent t-squared random variables with the same degrees of freedom so I am using simulation to get the quantiles. This is the sample from which I will create the quantiles.

g <- ggplot( data = xx
         ,  aes( x = X1
              , y = X2
                )
           ) + geom_point(colour = "red", size = 2)      ## initial setup

library(ellipse)

for (i in seq(from = 0.01, to = 0.99, length.out = 20)) {
    el.df <- data.frame(ellipse(x = sig, t = sqrt(quantile(rtsq, probs = i))))    ## create the data for the given quantile of the ellipse.
    names(el.df) <- c("x", "y")
    g <- g + geom_polygon(data=el.df, aes(x=x, y=y), fill = NA, linetype=1, colour = "blue") ## plot the ellipse
}

g + theme_bw() 

Hope this helps the OP! 

Questions and comments:

1. stat_ellipse would of course have been easier, but I could not figure out how to specify either the degrees of freedom or the correlation/variance-covariance matrix of the multivariate t. 

2. How does one reduce the line-width of the ellipses (i.e. the polygons). 

3. Also, it seems to me that the right way to draw the intensities of the contour plot in the way the OP has now asked would  be to plot the annular ellipses, each with alpha proportional to the squared difference between the two ellipse constants (t in the ellipse function above). (That would display the density correctly.) I am not sure how to draw the annular ellipse using geom_polygon or something else. 

Many thanks and best wishes,
Ranjan













On Mon, 09 Oct 2017 14:01:25 -0700 jdnewmil <jdnewmil at dcn.davis.ca.us> wrote:

> library(mvtnorm) # you were misusing "require"... only use require if 
> you plan to
> library(ggplot2) # test the return value and fail gracefully when the 
> package is missing
> set.seed( 1234 )
> xx <- data.frame( rmvt( 100, df = c( 13, 13 ) ) )
> xx2 <- expand.grid( X1 = seq( -5, 5, 0.1 ) # all combinations... could 
> be used to fill a matrix
>                    , X2 = seq( -5, 5, 0.1 )
>                    )
> # compute density as a function of the grid of points
> xx2$d <- dmvt( as.matrix( xx2[,1:2] ) ) # feels weird not specifying 
> measures of centrality or spread
> ggplot( data = xx
>        ,  aes( x = X1
>              , y = X2
>              )
>        ) +
>      geom_point() + # might want this line after the geom_contour
>      geom_contour( data = xx2 # may want to consider geom_tile as well
>                  , mapping = aes( x = X1
>                                 , y = X2
>                                 , z = d
>                                 )
>                  )
> #' ![](https://i.imgur.com/8ExFYtI.png)
> ## generated/tested with the reprex package to double check that it is 
> reproducible
> 
> On 2017-10-09 09:52, Big Floppy Dog wrote:
> > Hi,
> > 
> > This is not a HW problem, sadly: I was last in a classroom 30 years 
> > ago,
> > and can no longer run off to the instructor :-(
> > 
> > I apologize but I cut and paste the wrong snippet earlier and made a 
> > typo
> > in doing so, but the result is the same with the more appropriate  
> > snippet.
> > 
> > require(mvtnorm)
> > require(ggplot2)
> > set.seed(1234)
> > xx <- data.frame(rmvt(100, df = c(13, 13)))
> > 
> > v <- ggplot(data = xx, aes(x = X1, y = X2, z = dmvt, df = c(13,13)))
> > v + geom_contour()
> > 
> > Don't know how to automatically pick scale for object of type function.
> > Defaulting to continuous.
> > Error: Aesthetics must be either length 1 or the same as the data 
> > (100): x,
> > y, z, df
> > 
> > I do not understand how to put in a function as an argument to
> > geom_contour() and the examples in the help fileor in the link that 
> > Ulrik
> > sent are not very helpful to me. Hence, I was asking for some examples 
> > that
> > might be helpful.
> > 
> > I guess the answer is to make a second dataset that is regular and make 
> > the
> > function estimate that, but how do I combine this?
> > 
> > TIA.
> > BFD
> > 
> > 
> > On Mon, Oct 9, 2017 at 11:32 AM, David Winsemius 
> > <dwinsemius at comcast.net>
> > wrote:
> > 
> >> 
> >> > On Oct 9, 2017, at 6:03 AM, Big Floppy Dog <bigfloppydog at gmail.com>
> >> wrote:
> >> >
> >> > Hello Ulrik,
> >> >
> >> > I apologize, but I can not see how to provide a pdf in place of the
> >> density
> >> > function which calculates a KDE (that is, something from the dataset in
> >> the
> >> > example). Can you please point to the specific example that might help?
> >> >
> >> > Here is what I get:
> >> >
> >> > require(mvtnorm)
> >> > require(ggplot2)
> >> > set.seed(1234)
> >> > xx <- data.frame(rmvt(100, df = c(13, 13)))
> >> >
> >> >
> >> > v <- ggplot(faithfuld, aes(waiting, eruptions, z = drmvt, df = c(13,13)))
> >> > v + geom_contour()
> >> >
> >> > Don't know how to automatically pick scale for object of type function.
> >> > Defaulting to continuous.
> >> > Error: Aesthetics must be either length 1 or the same as the data (5625):
> >> > x, y, z, df
> >> >
> >> 
> >> That's not what I get:
> >> 
> >> > v <- ggplot(faithfuld, aes(waiting, eruptions, z = drmvt, df = c(13,13)))
> >> > v + geom_contour()
> >> Error in FUN(X[[i]], ...) : object 'drmvt' not found
> >> >
> >> > ? faithfuld
> >> > str(faithfuld)
> >> Classes ‘tbl_df’, ‘tbl’ and 'data.frame':       5625 obs. of  3 
> >> variables:
> >>  $ eruptions: num  1.6 1.65 1.69 1.74 1.79 ...
> >>  $ waiting  : num  43 43 43 43 43 43 43 43 43 43 ...
> >>  $ density  : num  0.00322 0.00384 0.00444 0.00498 0.00542 ...
> >> 
> >> So you are apparently trying to throw together code and data that you
> >> don't understand. The data you are using is already a density estimate
> >> designed to simply be plotted. It is not the original data. 
> >> Furthermore you
> >> are passing drmvt that is apparently not in either the mvtnorm nor the
> >> ggplot2 packages.
> >> 
> >> You should determine where that function is and then determine how to 
> >> do a
> >> 2d estimate on the original data. I'm guessing this is homework so not
> >> inclined to offer a complete solution.
> >> 
> >> --
> >> David.
> >> 
> >> 
> >> >
> >> > Can you please tell me how to use this here? Or is some other example
> >> more
> >> > appropriate?
> >> >
> >> > TIA,
> >> > BFD
> >> >
> >> >
> >> >
> >> > On Mon, Oct 9, 2017 at 2:22 AM, Ulrik Stervbo <ulrik.stervbo at gmail.com>
> >> > wrote:
> >> >
> >> >> Hi BFD,
> >> >>
> >> >> ?geom_contour() *does* have helpful examples. Your Google-foo is weak:
> >> >> Searching for geom_contour brought me: http://ggplot2.tidyverse.
> >> >> org/reference/geom_contour.html as the first result.
> >> >>
> >> >> HTH
> >> >> Ulrik
> >> >>
> >> >> On Mon, 9 Oct 2017 at 08:04 Big Floppy Dog <bigfloppydog at gmail.com>
> >> wrote:
> >> >>
> >> >>> Can someone please point me to an example with geom_contour() that
> >> uses a
> >> >>> function? The help does not have an example of a function, and also  I
> >> did
> >> >>> not find anything from online searches.
> >> >>>
> >> >>> TIA,
> >> >>> BFD
> >> >>>
> >> >>>
> >> >>> ------------------------------------------------------------
> >> >>> -----------------------------------
> >> >>>
> >> >>> How about geom_contour()?
> >> >>>
> >> >>> Am So., 8. Okt. 2017, 20:52 schrieb Ranjan Maitra <maitra at email.com>:
> >> >>>
> >> >>>> Hi,
> >> >>>>
> >> >>>> I am no expert on ggplot2 and I do not know the answer to your
> >> >>> question. I
> >> >>>> looked around a bit but could not find an answer right away. But one
> >> >>>> possibility could be, if a direct approach is not possible, to draw
> >> >>>> ellipses corresponding to the confidence regions of the multivariate t
> >> >>>> density and use geom_polygon to draw this successively?
> >> >>>>
> >> >>>> I will wait for a couple of days to see if there is a better answer
> >> >>> posted
> >> >>>> and then write some code, unless you get to it first.
> >> >>>>
> >> >>>> Thanks,
> >> >>>> Ranjan
> >> >>>>
> >> >>>>
> >> >>>> On Sun, 8 Oct 2017 09:30:30 -0500 Big Floppy Dog <
> >> >>> bigfloppydog at gmail.com>
> >> >>>> wrote:
> >> >>>>
> >> >>>>> Note: I have posted this on SO also but while the question has been
> >> >>>>> upvoted, there has been no answer yet.
> >> >>>>>
> >> >>>>>
> >> >>>>
> >> >>> https://stackoverflow.com/questions/46622243/ggplot-
> >> >>> plot-2d-probability-density-function-on-top-of-points-on-ggplot
> >> >>>>>
> >> >>>>> Apologies for those who have seen it there also but I thought that
> >> >>> this
> >> >>>>> list of experts may have someone who knows the answer.
> >> >>>>>
> >> >>>>> I have the following example code:
> >> >>>>>
> >> >>>>>
> >> >>>>>
> >> >>>>> require(mvtnorm)
> >> >>>>> require(ggplot2)
> >> >>>>> set.seed(1234)
> >> >>>>> xx <- data.frame(rmvt(100, df = c(13, 13)))
> >> >>>>> ggplot(data = xx,  aes(x = X1, y= X2)) + geom_point() +
> >> >>> geom_density2d()
> >> >>>>>
> >> >>>>>
> >> >>>>>
> >> >>>>> It yields a scatterplot of X2 against X1 and a KDE contour plot of
> >> the
> >> >>>>> density (as it should).
> >> >>>>>
> >> >>>>> My question is: is it possible to change the contour plot to display
> >> >>>>> the contours
> >> >>>>>
> >> >>>>> of a two-dimensional density function (say dmvt), using ggplot2?
> >> >>>>>
> >> >>>>> The remaining figures in my document are in ggplot2 and therefore I
> >> >>>>> am looking for a ggplot2 solution.
> >> >>>>>
> >> >>>>> Thanks in advance!
> >> >>>>>
> >> >>>>> BFD
> >> >>>>>
> >> >>>>>      [[alternative HTML version deleted]]
> >> >>>>>
> >> >>>>> ______________________________________________
> >> >>>>> 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]]
> >> >>>
> >> >>> ______________________________________________
> >> >>> 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]]
> >> >
> >> > ______________________________________________
> >> > 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.
> >> 
> >> David Winsemius
> >> Alameda, CA, USA
> >> 
> >> 'Any technology distinguishable from magic is insufficiently 
> >> advanced.'
> >>  -Gehm's Corollary to Clarke's Third Law
> >> 
> >> 
> >> 
> >> 
> >> 
> >> 
> > 
> > 	[[alternative HTML version deleted]]
> > 
> > ______________________________________________
> > 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.
> 
> ______________________________________________
> 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.

-- 
Important Notice: This mailbox is ignored: e-mails are set to be deleted on receipt. Please respond to the mailing list if appropriate. For those needing to send personal or professional e-mail, please use appropriate addresses.



More information about the R-help mailing list