简体   繁体   English

ggplot2绘图函数,带有多个参数

[英]ggplot2 plot function with several arguments

I got this function. 我有这个功能。

change <- function(score, d, k, p) {k*(score - 1/(1+k^(d/p)))}

I would like to plot, in one single plot, all results of this function for a range of arguments d and p. 我想在一个单独的图中绘制该函数针对所有参数d和p的所有结果。 In base r it would be this. 在基数r中就是这样。

parameters <- c(100:400)
colorshelf <-rainbow(length(parameters)) #red is low
for(i in seq_along(parameters)) {
    print(i)
    curve(change(score=1, d=x, k=100, p=parameters[i]), from=0, to=500, add=T, col=colorshelf[i])
}

But I thought this must be possible in ggplot2, but can not wrap my head around this. 但是我认为这在ggplot2中一定是可能的,但是我无法解决这个问题。 I am currently stuck with this. 我目前对此感到困惑。 Any help is appreciated. 任何帮助表示赞赏。

ggp <- ggplot(data.frame(Ds=c(0:1000), Ps=c(0:1000)), aes(x=Ds, col=Ps)) + 
    stat_function(fun=change, args=list(score=1, d=Ds, k=100, p=Ps))
ggp

I would do this outside of ggplot2 . 我会在ggplot2之外执行此ggplot2 I think it might be too much to expect ggplot to vectorise over two different parameters ... 我认为可能期望ggplot在两个不同的参数上进行向量化...

This is with tidyverse, but could easily be done with apply as well. 这与tidyverse一起使用,但也可以很容易地通过apply来完成。

 library(dplyr)
 change <- function(score, d, k, p) {k*(score - 1/(1+k^(d/p)))}
 dd <- expand.grid(d=0:1000,p=0:100)
 dd %>% rowwise %>%
     mutate(c=change(score=1,d=d,k=100,p=p)) ->
  dd2

 library(ggplot2)
 ggp <- ggplot(dd2,aes(d,c,col=p,group=p))+
            geom_path()

I only did p from 0 to 100 (rather than 0 to 1000) because 1 million points is a fairly large data set for ggplot. 我只将p从0设置为100(而不是0到1000),因为100万个点对于ggplot来说是一个相当大的数据集。 (Do you really need to see 1000 separate values? Maybe seq(0,1000,length=100) ? (您是否真的需要查看1000个单独的值?也许是seq(0,1000,length=100)

just now, i had a very similar problem; 刚才,我有一个非常相似的问题; i wanted to plot a function in ggplot with multiple parameters stored rowwise in a dataframe. 我想在ggplot中绘制一个函数,其中多个参数按行存储在数据框中。 As i did not need the dataframe with the function evaluated for each data point in the remainder of my code, i let ggplot do the dirty work for me. 由于我不需要在代码的其余部分中为每个数据点评估函数的数据框,因此我让ggplot为我完成了肮脏的工作。 However, my approach comes at the minor disadvantage of positional knowlege on the dataframes ordering in apply (which should not be your problem). 但是,我的方法的缺点是在应用中对数据帧进行排序时位置知识的次要缺点(这不应该是您的问题)。 Also i need a color column (here: 'group'), which was in my case actually desirable for grouping. 我还需要一个颜色列(在这里:“组”),在我的情况下,这实际上是分组所需要的。 And ever since your parameters are unbalnced, you will have to expand.grid the parameters 并且由于您的参数是非平衡的,因此您将必须扩展.grid参数

The skeleton of the solution i came up with is basically the following: 我想出的解决方案的基本原理如下:

p = ggplot(data.frame(x = c(-5, 10)), aes(x)) 
# this format specifies start & end of the x-axis for which the function is evaluated. 
d = data.frame(mu = 1:4, sigma= c(1,1,2,2), group = c(1,1,2,2))
# each row is a parameter set.
p = p + apply(
  d,
  MARGIN = 1,
  FUN = function(z)
    stat_function(
      fun = dnorm, # you can specify your own function which lives in .GlobalEnvir
      geom = "line",
      args = list(mean = z[1] , sd = z[2]),
      color = z[3]
    )
)
print(p)

But if you would have a non-standardized set of parameters (eg sometimes you dont have values for mean or sd and would want to omit them entirely in the passing of args) a more flexible approach with lapply might be an alternative. 但是,如果您有一组非标准化的参数(例如,有时您没有均值或sd的值,并且希望在传递args时完全忽略它们),则可以使用lapply更为灵活的方法。 Note, that if you would like to use a default value of the function eg dnorm, you do not need to specify the particular parameter. 注意,如果您想使用函数的默认值(例如dnorm),则无需指定特定参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM