R中自定义函数的等高线图 [英] contour plot of a custom function in R

查看:101
本文介绍了R中自定义函数的等高线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些自定义函数,我需要根据参数的多个值为其绘制轮廓。

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

这里是一个示例函数:

我需要绘制这样的轮廓图:

I need to draw such a contour plot:

有什么想法吗?

谢谢。

推荐答案

首先构造一个函数, fourvar ,将这四个参数用作参数。在这种情况下,您可以使用3个变量来完成此操作,其中一个变量是lambda_2而不是lambda_1。 Alpha1固定为2,因此alpha_1 / alpha_2的变化范围为0-10。

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 }

诀窍是要认识到集成函数返回一个列表,您只需要该列表的值部分,因此可以进行 Vectorize() -ed。

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) ) )

然后创建带有标签的绘图的任务只能在这些位置进行使用 lattice :: contourplot 可以轻松完成。经过合理数量的搜索后,看来ggplot2中的geom_contour标签解决方案仍在进行中。我发现的唯一标记策略是在外部包装中。但是,在这种情况下, directlabels软件包的功能 directlabel 似乎没有足够的控制权来正确地散布标签。在我看到的其他示例中,它确实将标签分布在绘图区域周围。我想我可以看一下代码,但是由于它取决于'proto'程序包,因此它可能会被奇怪地封装起来,所以我没有看过。

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.

另一方面,这就是将其构造成格子的难易程度(我认为它看起来更干净:

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))

这篇关于R中自定义函数的等高线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆