ggplot2:用ggplot2绘制曲线

时间:2015-09-12 12:06:37

标签: r ggplot2 curve

  你好,我在绘制数据方面遇到了一些麻烦。我有两种方法,两者都给我不同的结果。我不明白。

在上一篇文章中,有人告诉我ggplot2中的函数“stat_function”,它就像“曲线”函数,但我没有得到相同的结果。

第一。 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)时,我分别得到这些曲线:

enter image description here

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

第一。 Methode(正如预期的那样):

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

enter image description here

第二。 Methode(错误)

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条相同的曲线。方法....请有人帮忙吗?我厌倦了尝试一切。

谢谢你们!

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找的答案可能会在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有助于处理此问题。