ggplot2:用ggplot2绘制曲线

ggplot2: draw curve with ggplot2

Hallo guys, i'm having some trouble to plot data. i got 2 methods and both give me different results. I don't get it.

在之前的 post 中,有人告诉我 ggplot2 中的函数 "stat_function" 就像 "curve" 函数,但我没有得到相同的结果。

第一。 Methode(带曲线):

# draw.data:
draw.data <- function(xy,xlab="log10",ylab="",pch=16,col=1){
    plot(xy,xlab=xlab,ylab=ylab,pch=pch,col=col)
}

# f.probit
f.probit <- function(x,beta1=0,beta2=1,minv=0,maxv=1){
return(pnorm(beta1+beta2*x)*(maxv-minv)+minv)
}

# draw.probit
draw.probit <-function(beta1=0,beta2=1,minv=0,maxv=1,col=1,
lwd=2,lty=1,add=T,from=0,to=1){
  if (add){
    curve(f.probit(x,beta1=beta1,beta2=beta2,minv=minv,maxv=maxv),add=T,col=col,lwd=lwd,lty=lty)
  }else{
    curve(f.probit(x,beta1=beta1,beta2=beta2,minv=minv,maxv=maxv),from=from,to=to,col=col,lwd=lwd,lty=lty)
  }
}

第二。 Methode(使用 ggplot)

# draw.data:
draw.data <- function(xy, add = F, mod = "Data", FUN = NULL){
  # 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 
  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() +
    #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) +
    stat_function(fun = FUN, geom = "line", aes(x, y, group = model, colour = model))
  }
}

# f.probit remains the same!

# draw.probit
draw.probit <- function(xy, beta1 = 0, beta2 = 1,minv = 0, maxv = 1, 
                        mod = "Probit", add = T){
  # Aufruf der Funktion f.probit zur Verbesserung der y-Werte
  #f <- f.probit(xy[,1],beta1=beta1,beta2=beta2,minv=minv,maxv=maxv)

  selected_FUN <- function(x){
      f.probit(x,beta1=beta1,beta2=beta2,minv=minv,maxv=maxv)
  }

  draw.data(xy, add, mod, selected_FUN)
}

数据如下:

> 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

现在当我开始 draw.data(xy) 时,在这两种情况下我分别得到这些曲线:

这正是我所期望的。但是当我开始 'draw.probit' 我得到:

第一。方法(如预期):

> draw.probit(beta1 -4.827511, beta2 = -0.8401166, minv = 0.05, maxv = 1, add = T)

第二。方法(错误)

mapping: x = x, y = y, group = model, colour = model 
geom_line:  
stat_function: fun = function (x) 
{
    f.probit(x, beta1 = beta1, beta2 = beta2, minv = minv, maxv = maxv)
}, n = 101, args = list() 
position_identity: (width = NULL, height = NULL)
> 

现在问题:-)

我该怎么做才能获得与 1 相同的曲线。方法....请有人帮忙吗?我厌倦了尝试一切。

谢谢大家!

我认为您正在寻找的答案可能在某个地方找到 here。这是一两年前的一个问题,展示了如何将 logit 和 probit 模型拟合到 ggplot2 曲线的非常好的示例。 我相信您正在寻找的是

stat_smooth(method="glm",family="binomial",link="probit")

但您可能需要稍微尝试一下才能使其正常工作。当我尝试使用您的数据集的子集时,出现错误

Error in eval(expr, envir, enclos) : y values must be 0 <= y <= 1

这与回归模型的建立方式有关。您可能会发现一些 these links 有助于解决这个问题。