contour plot of a custom function in R
Asked Answered
H

2

10

I'm working with some custom functions and I need to draw contours for them based on multiple values for the parameters.

Here is an example function:

enter image description here

I need to draw such a contour plot:

enter image description here

Any idea?

Thanks.

Hanahanae answered 29/9, 2013 at 14:26 Comment(7)
Do you have your values for x-axis and y-axis or just use the values as shown in figure?Doolie
This does look like homework.Lenticular
Indeed. It's not clear what 0.1, 0.2, 0.3 stand for?Doolie
It doesn't matter what they stand for. The problem is well posed and I have a solution but I'm still hesitant to just post an answer that seems to be doing someone's HW.Lenticular
then this should be good candidate for closureDoolie
I left the section that I considered difficult unsolved. If this is not homework and solving that "example" function with R isn't really the task, then I think the answer is responsive. I don't think SO expects that all HW Q's should be closed, but rather there should be academic honesty and the student should show effort.Lenticular
No, Be sure! This is not a HW. I tried a lot of codes without success, and as I said, this is just an example of many custom function that I'm working on them.Hanahanae
L
17

First you construct a function, fourvar that takes those four parameters as arguments. In this case you could have done it with 3 variables one of which was lambda_2 over lambda_1. Alpha1 is fixed at 2 so alpha_1/alpha_2 will vary over 0-10.

fourvar <- function(a1,a2,l1,l2){ 
  a1* integrate( function(x) {(1-x)^(a1-1)*(1-x^(l2/l1) )^a2} , 0 , 1)$value }

The trick is to realize that the integrate function returns a list and you only want the 'value' part of that list so it can be Vectorize()-ed.

Second you construct a matrix using that function:

  mat <- outer( seq(.01, 10, length=100),  
                seq(.01, 10, length=100), 
                Vectorize( function(x,y) fourvar(a1=2, x/2, l1=2, l2=y/2) ) )

Then the task of creating the plot with labels in those positions can only be done easily with lattice::contourplot. After doing a reasonable amount of searching it does appear that the solution to geom_contour labeling is still a work in progress in ggplot2. The only labeling strategy I found is in an external package. However, the 'directlabels' package's function directlabel does not seem to have sufficient control to spread the labels out correctly in this case. In other examples that I have seen, it does spread the labels around the plot area. I suppose I could look at the code, but since it depends on the 'proto'-package, it will probably be weirdly encapsulated so I haven't looked.

require(reshape2)
mmat <- melt(mat)
str(mmat) # to see the names in the melted matrix
g <- ggplot(mmat, aes(x=Var1, y=Var2, z=value) )
g <- g+stat_contour(aes(col = ..level..), breaks=seq(.1, .9, .1) )
g <- g + scale_colour_continuous(low = "#000000", high = "#000000") # make black
install.packages("directlabels", repos="http://r-forge.r-project.org", type="source")
require(directlabels)
direct.label(g)

Note that these are the index positions from the matrix rather than the ratios of parameters, but that should be pretty easy to fix.

enter image description here

This, on the other hand, is how easilyy one can construct it in lattice (and I think it looks "cleaner":

  require(lattice)
  contourplot(mat, at=seq(.1,.9,.1))

enter image description here

Lenticular answered 29/9, 2013 at 17:50 Comment(0)
H
2

As I think the question is still relevant, there have been some developments in the contour plot labeling in the metR package. Adding to the previous example will give you nice contour labeling also with ggplot2

require(metR)
g + geom_text_contour(rotate = TRUE, nudge_x = 3, nudge_y = 5)

Labeled contour plot

Hanging answered 12/7, 2019 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.