使用函数" stat_function"

时间:2015-08-25 13:29:57

标签: r ggplot2

我又遇到了麻烦。我尝试使用ggplot2库绘制一些曲线。我已经找到了stat_function函数来完成它。现在我在我的draw.data中调用一个函数,我收到一个错误:

  

do.call出错(fun,c(list(xseq),args))

这是我的代码:

1

#f.probit:
f.probit <- function(x,beta1,beta2,minv,maxv){

  return(pnorm(beta1+beta2*x)*(maxv-minv)+minv)
}

2

draw.probit <- function(xy, beta1,beta2,minv,maxv, 
                        mod,lwd,lty, add,from,to){
  # Aufruf der Funktion f.probit zur Verbesserung der y-Werte
  #f <- f.probit(xy[,1],beta1=beta1,beta2=beta2,minv=minv,maxv=maxv)

  # Ersetzen der Y-Werte mit der Verbesserung
  #xy[,2] = f

  if (add){ # TODO: Falls add = TRUE, dann die Kurve im geöffneten Fenster hinzufügen
    draw.data(xy, add, mod, f.probit(xy[,1],beta1=beta1,beta2=beta2,minv=minv,maxv=maxv))
    #curve(f.probit(x,beta1=beta1,beta2=beta2,minv=minv,maxv=maxv),add=T,mod=model,lwd=lwd,lty=lty)
  }else{ # TODO: Falls add = FALSE, dann die Kurve in ein neues Fenster hinzufügen
    draw.data(xy, add, mod, f.probit(xy[,1],beta1=beta1,beta2=beta2,minv=minv,maxv=maxv))
    #curve(f.probit(x,beta1=beta1,beta2=beta2,minv=minv,maxv=maxv),from=from,to=to,mod=model,lwd=lwd,lty=lty)
  }
}

3

draw.data <- function(xy, add = FALSE, mod, FUN){
  # Bibliothek für ggplot-Funktion
  # Dependencies: > library("ggplot2") must be imported!

  x.lab <- "concentration [M]"
  y.lab <- "normalised luminescence [%]"

  my_labels <- parse(text = paste("1E", seq(-10, -4, 1), sep = ""))

  # Find max, min and difference
  # y.max <- max(my.data$y)
  # y.min <- min(my.data$y)

  y.max <- 1
  y.min <- 0

  diff <- y.max - y.min

  # Find percentage and apply to new column 
  #my.data$y <- apply(my.data, 1, function(z) ((z["y"] - y.min)/diff)*100)

  data <- data.frame(xy)
  my.data <- data.frame(x=data$x,y=apply(data, 1, function(z) ((z['y'] - y.min)/diff)*100),model = mod)

  if(!add){
    quartz() # windows() unter MS Windows
    ggplot(my.data, aes(x, y, group = model, color = model)) +
      #geom_point(aes(x = x, y = y, color = as.factor(x))) +
      #geom_point(aes(x = x, y = y)) +
      #geom_line(aes(x = x, y = y)) +
      #geom_line(aes(x = x, y = y, color = as.factor(x))) +
      geom_line() +
      stat_function(fun = FUN, geom = "line", aes(group = model, colour = model)) +
      # Draw 2 lines at 50% and 90% through the y-axis
      geom_hline(yintercept = c(50, 90), linetype = "dotted") + # draw dotted horizontal lines at 50 and 90
      scale_x_continuous(x.lab, breaks = seq(-10, -4, 1), labels = my_labels) + 
      labs(title = "Graph", x = x.lab, y = y.lab)
  } else{
    geom_line(aes(x, y, group = model, color = model), data = my.data)
  }
}

您可以使用以下参数进行测试:

> add
[1] FALSE
> beta1
[1] -4.666667
> beta2
[1] -0.6666667
> minv
[1] 0.04061895
> maxv
[1] 2.132124
> lwd
[1] 2
> lty
[1] 1

和数据:

> xy
        x          y
 [1,] -10 1.14259527
 [2,]  -9 1.15024188
 [3,]  -8 1.10517450
 [4,]  -7 1.00961311
 [5,]  -6 0.71238360
 [6,]  -5 0.20355333
 [7,]  -4 0.04061895
 [8,] -10 1.11022461
 [9,]  -9 1.11083317
[10,]  -8 1.07867942
[11,]  -7 0.98422000
[12,]  -6 0.73539660
[13,]  -5 0.36134577
[14,]  -4 0.18124645
[15,] -10 2.13212408
[16,]  -9 1.14529425
[17,]  -8 1.25102307
[18,]  -7 1.16045169
[19,]  -6 0.50321380
[20,]  -5 0.15422609
[21,]  -4 0.10198811
[22,] -10 1.16539392
[23,]  -9 1.15855333
[24,]  -8 1.11766975
[25,]  -7 0.97204379
[26,]  -6 0.53504417
[27,]  -5 0.17431435
[28,]  -4 0.29470416
[29,] -10 1.03683145
[30,]  -9 1.07524250
[31,]  -8 1.07761291
[32,]  -7 0.96401682
[33,]  -6 0.78346457
[34,]  -5 0.32783725
[35,]  -4 0.08103084
[36,] -10 0.81372339
[37,]  -9 0.85402909
[38,]  -8 0.86584396
[39,]  -7 0.80705470
[40,]  -6 0.53086151
[41,]  -5 0.15711034
[42,]  -4 0.11496499

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您传递给FUN的{​​{1}}应该只使用stat_identity个参数。试试这个,

x