如何在同一图表上绘制多个ggplots曲线

时间:2015-09-16 11:10:39

标签: r plot ggplot2

我试图用ggplot2在同一个图上绘制多条曲线....实际上我正在使用一个函数" draw.data"有一些参数,所以我可以有多个图。但我得到了一个问题,通过第一次调用,我有一个情节,一旦我第二次调用该方法,我有一个错误。但我希望曲线绘制在同一个Plot上。

Hier是我的代码:

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

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

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

  return((1/(1+exp(-(beta1+beta2*x))))*(maxv-minv)+minv)
}

# draw.data
draw.data <- function(xy, mod, add = F, 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 
  my.data <- data.frame(xy, model = mod)

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

我对函数的调用&#34; draw.data&#34;参数&quot; add = F&#39;第二次给出我的错误:

mapping: group = model, colour = model 
geom_line:  
stat_function: fun = function (x, beta1, beta2, minv, maxv) 
{
    return((1/(1 + exp(-(beta1 + beta2 * x)))) * (maxv - minv) + minv)
}, n = 101, args = list(-4.827511, -0.8401166, minv = 0, maxv = 1) 
position_identity: (width = NULL, height = NULL)

这次电话有什么问题?我应该如何实现draw.data,所以我可以进入else部分并调用stat_function在同一个图形上绘制第二条曲线?

更新

Hier结果是:

enter image description here

> 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

0 个答案:

没有答案