Is there any "add" parameter (like in graphics::curve) for ggplot2? - r

i got some trouble with the ggplot. I drew some functions with ggplot and want to have them on the same graph.
With the curve function for instance i used to set the parameter add = T so the next plot will be put on the same graph.
curve(f.probit(x),add=T,col=col,lwd=lwd,lty=lty)
Now, i'm using the ggplot2 library... and want to do the same like with curve. Is there a parameter in ggplot like add, which allows to plot 2 curves on the graph?
qplot(my.data, aes(x, y, group = model, color = model)) +
geom_point() +
#geom_line() +
stat_function(fun = f.probit(x), 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)
Hier is my f.probit function:
beta1 <- -4.827511
beta2 <- -0.8401166
minv <- 0.05
maxv <- 1
f.probit <- function(x,beta1,beta2,minv,maxv){
return(pnorm(beta1+beta2*x)*(maxv-minv)+minv)
}
And i want to plot it on the graph with stat_function... But it doesn't work. I'm expecting some around this Graph:
these are the Data:
> 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

You can layer as many plots as you want to:
dat1 <- data.frame(x=c(0, 10))
dat2 <- data.frame(x=c(4, 14))
my_tan <- function(x) { tan(x) }
ggplot() +
stat_function(data=dat1, aes(x), fun=sin, colour="red") +
stat_function(data=dat2, aes(x), fun=cos, colour="blue") +
stat_function(data=dat1, aes(x), fun=my_tan, colour="black") +
stat_function(data=dat2, aes(x), fun=my_tan, colour="white", linetype="dashed")

Edit: Response to OP's comment, and the addition of data in the question.
I see now what your problem is: if your function has arguments in addition to x, you have to pass these in stat_function(...) using the args=list(...) argument, as follows:
library(ggplot2)
ggplot(my.data, aes(x=x, y=y)) +
geom_point() +
stat_function(fun=f.probit, args=list(beta1, beta2, minv, maxv))
Your question illustrates a common misconception about ggplot. The graphics functions in base R plot the graph (or render the image). The ggplot functions create a ggplot object, but do not plot it. You have to use print(...) or plot(...) on the ggplot object to render the image.
Consider the following:
library(ggplot2)
ggp <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
stat_function(fun=function(x) 60/x)
Notice that there is no image. You have to use print(ggp) or plot(ggp) to do that.
print(ggp)
Of course, typing the variable name at the command line will invoke the print method for that object, so
ggp
does the same thing. And of course, just typing an expression at the command line will evaluate the expression and print the result, which is why you get the image if you leave out the assigment:
# evaluate the expression to create ggplot object and then print it automatically.
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
stat_function(fun=function(x) 60/x)
Once you create a ggplot object (ggp in this example) you can add layers using +, so for instance
ggp + stat_function(fun=function(x) 30/x, color="red")
Finally, one of the great things about ggplot is that adding layers modifies the object (including the x- and y-limits). So for instance
ggp + stat_function(fun=function(x) 100/x, color="red")
Notice how the y-axis has been expanded to account for the new curve. Base R graphics do not work that way:
plot(mpg~wt, mtcars)
curve(100/x, add=TRUE)
Notice how the x- and y-limits have been defined in the initial call to plot(...).

Related

Optimum cut-off values

I have both univariate and multivariate logistic regression models and I want to find cut-off values with their respective sensitivity and specificity. I want to chose the best cut-off values for both my univariate and multivariate models.
I tried the following code for the univariate models but I am getting the sensitivity and specificity values in decimals. Is there any other way I can get the cut-off values as whole numbers rather than rounding up to the nearest integer? I am also not sure how to use the same code to get the cut-off values of the multivariate model.
Thank you for any help in advance!!
###Cut off values of Var1
library(pROC)
ok <- multiclass.roc(DATA$Outcome, DATA$Var1)
class_1 <- ok$rocs[[1]]
wants <- cbind(sensitivity = class_1$sensitivities, specificity = class_1$specificities, cutt_off = class_1$thresholds)
wants
I am getting the values:
sensitivity specificity cutt_off
[1,] 1.00000 0.000000 Inf
[2,] 1.00000 0.012346 73.500
[3,] 1.00000 0.024691 72.500
[4,] 1.00000 0.049383 71.500
[5,] 1.00000 0.061728 70.500
[6,] 1.00000 0.135802 69.500
[7,] 1.00000 0.172840 68.500
[8,] 0.94118 0.222222 67.500
[9,] 0.88235 0.283951 66.500
[10,] 0.88235 0.320988 65.750
[11,] 0.88235 0.333333 65.250
[12,] 0.88235 0.432099 64.500
[13,] 0.88235 0.506173 63.500
[14,] 0.82353 0.617284 62.500
[15,] 0.82353 0.629630 61.750
[16,] 0.76471 0.629630 61.250
[17,] 0.76471 0.691358 60.500
[18,] 0.70588 0.753086 59.750
[19,] 0.70588 0.777778 59.250
[20,] 0.70588 0.814815 58.500
[21,] 0.64706 0.827160 57.500
[22,] 0.64706 0.876543 56.500
[23,] 0.64706 0.901235 55.250
[24,] 0.58824 0.913580 54.250
[25,] 0.58824 0.938272 53.900
[26,] 0.52941 0.938272 53.400
[27,] 0.41176 0.938272 52.500
[28,] 0.35294 0.950617 51.835
[29,] 0.29412 0.950617 50.835
[30,] 0.29412 0.962963 49.000
[31,] 0.23529 0.975309 47.500
[32,] 0.17647 0.975309 46.000
[33,] 0.11765 0.987654 44.500
[34,] 0.00000 0.987654 42.500
[35,] 0.00000 1.000000 -Inf
To determine the cut-off values for the multivariate model, I tried the following code but I am getting errors. Also, my model comprises of both continuous and categorical values. Var1, Var2, Var3 are continuous variables and Var4 is categorical which was changed to 0, 1, & 2.
library(pROC)
ok <- multiclass.roc(DATA$Outcome, DATA$var1 + DATA$Var2 + DATA$Var3 + DATA$Var4)
class_1 <- ok$rocs[[1]]
wants <- cbind(sensitivity = class_1$sensitivities, specificity = class_1$specificities, cutt_off = class_1$thresholds)
wants

How does R calculate the PCA ellipses?

How does R know where to place the confidence ellipse for a PCA plot?
I have a minimal code using the iris dataset:
library(factoextra)
a<-data.matrix(iris[-5])
b<-prcomp(a, scale. = TRUE, center = TRUE)
fviz_pca_ind(b,
col.ind = iris$Species,
addEllipses = TRUE)
I know that I can find the plot coordinate with b$x. I also know that I can find the cluster centers with b$center. How do I re-derive the ellipses from the data?
If you are talking about the how, it eventually calls ggplot2::stat_ellipse.
If you want the coordinates, like with other ggplot objects, you can extract the data with ggplot_build
library(factoextra)
a<-data.matrix(iris[-5])
b<-prcomp(a, scale. = TRUE, center = TRUE)
p <- fviz_pca_ind(b,
col.ind = iris$Species,
addEllipses = TRUE)
ell <- ggplot2::ggplot_build(p)$data[[2]]
head(ell)
# colour fill x y group PANEL size linetype alpha
# 1 #F8766D #F8766D -1.697756 -0.06395559 1 1 0.5 1 0.1
# 2 #F8766D #F8766D -1.701694 0.22197334 1 1 0.5 1 0.1
# 3 #F8766D #F8766D -1.713449 0.50017215 1 1 0.5 1 0.1
# 4 #F8766D #F8766D -1.732842 0.76642364 1 1 0.5 1 0.1
# 5 #F8766D #F8766D -1.759579 1.01669171 1 1 0.5 1 0.1
# 6 #F8766D #F8766D -1.793255 1.24718254 1 1 0.5 1 0.1
p + geom_point(aes(x, y, color = factor(group)), data = ell, size = 4)
If you trace the code all the way through, you find that the ellipses are simply geom_polygons created with stat = "ellipse", i.e. they are calculated by stat_ellipse in ggplot.
We can show this by recreating the plot using only base R and ggplot. The following is a fully reproducible example:
library(ggplot2)
b <- prcomp(iris[-5], scale. = TRUE, center = TRUE)
df <- as.data.frame(predict(b)[,1:2])
df$Species <- iris$Species
ggplot(df, aes(PC1, PC2, color = Species)) +
geom_point() +
theme_bw() +
geom_polygon(stat = "ellipse", aes(fill = Species), alpha = 0.3)
Ultimately, stat_ellipse gets its data from the same method as cars::dataEllipse, so if you want the raw co-ordinates of the ellipses, you can do:
e <- car::dataEllipse(df$PC1, df$PC2, df$Species)
and obtain the 95th centile normal data ellipse co-ordinates like this:
e$setosa$`0.95`
#> x y
#> [1,] -2.167825 2.06328716
#> [2,] -2.104642 2.04546589
#> [3,] -2.043166 1.99227221
#> [4,] -1.984331 1.90451250
#> [5,] -1.929028 1.78351710
#> [6,] -1.878095 1.63112017
#> [7,] -1.832305 1.44963190
#> [8,] -1.792351 1.24180347
#> [9,] -1.758839 1.01078534
#> [10,] -1.732278 0.76007952
#> [11,] -1.713069 0.49348644
#> [12,] -1.701504 0.21504739
#> [13,] -1.697759 -0.07101678
#> [14,] -1.701889 -0.36036963
#> [15,] -1.713833 -0.64862486
#> [16,] -1.733410 -0.93141283
#> [17,] -1.760322 -1.20444675
#> [18,] -1.794162 -1.46358770
#> [19,] -1.834417 -1.70490738
#> [20,] -1.880476 -1.92474763
#> [21,] -1.931641 -2.11977588
#> [22,] -1.987137 -2.28703571
#> [23,] -2.046123 -2.42399164
#> [24,] -2.107703 -2.52856754
#> [25,] -2.170946 -2.59917816
#> [26,] -2.234892 -2.63475311
#> [27,] -2.298571 -2.63475311
#> [28,] -2.361018 -2.59917816
#> [29,] -2.421288 -2.52856754
#> [30,] -2.478465 -2.42399164
#> [31,] -2.531684 -2.28703571
#> [32,] -2.580138 -2.11977588
#> [33,] -2.623091 -1.92474763
#> [34,] -2.659894 -1.70490738
#> [35,] -2.689988 -1.46358770
#> [36,] -2.712917 -1.20444675
#> [37,] -2.728333 -0.93141283
#> [38,] -2.736002 -0.64862486
#> [39,] -2.735809 -0.36036963
#> [40,] -2.727757 -0.07101678
#> [41,] -2.711966 0.21504739
#> [42,] -2.688678 0.49348644
#> [43,] -2.658244 0.76007952
#> [44,] -2.621126 1.01078534
#> [45,] -2.577888 1.24180347
#> [46,] -2.529183 1.44963190
#> [47,] -2.475751 1.63112017
#> [48,] -2.418401 1.78351710
#> [49,] -2.358004 1.90451250
#> [50,] -2.295473 1.99227221
#> [51,] -2.231758 2.04546589
#> [52,] -2.167825 2.06328716
Created on 2021-11-05 by the reprex package (v2.0.0)

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.
In a previous post someone told me the function "stat_function" in ggplot2 it's like the "curve"-function but i don't get the same result.
1st. Methode (with curve):
# 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)
}
}
2nd. Methode (with 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)
}
And hier are the data:
> 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
Now when i start draw.data(xy) in both cases i get respectively these curves:
Which is exactly, what i expected. But when i start 'draw.probit' i get:
1st. Methode (as expected):
> draw.probit(beta1 -4.827511, beta2 = -0.8401166, minv = 0.05, maxv = 1, add = T)
2nd. Methode (Error)
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)
>
Now the question :-)
What can i do to get the same curve like in the 1st. method.... Please can someone help? I'm getting tired trying everything.
Thanks guys!
I think the answer you're looking for might be found somewhere here. This is a question from a year or two ago and shows really nice examples of how to fit a logit and probit model to a ggplot2 curve.
I believe what you're looking for is something along the lines of
stat_smooth(method="glm",family="binomial",link="probit")
but you may have to play around with that a bit to get it to work. When I tried with a subset of your data set, I got an error
Error in eval(expr, envir, enclos) : y values must be 0 <= y <= 1
which has something to do with how the regression model is set up. You might find some of these links helpful for dealing with that.

Use the function "stat_function"

I'm having some trouble again. I try to use the ggplot2 library to draw some curve. I've already found out the function stat_function to do it. Now I call a function in my draw.data and I get an error:
Error in do.call(fun, c(list(xseq), args))
Here is my code:
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)
}
}
You can test with these parameters:
> 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
and data:
> 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
Can anyone help me out?
The FUN that you pass to stat_identity should take only an x argument. Try this,
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
selected_FUN <- function(x) {
f.probit(x, beta1=beta1,beta2=beta2,minv=minv,maxv=maxv)
}
draw.data(xy, add, mod, selected_FUN)
#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
selected_FUN <- function(x) {
f.probit(x,beta1=beta1,beta2=beta2,minv=minv,maxv=maxv)
}
draw.data(xy, add, mod, select_FUN)
#curve(f.probit(x,beta1=beta1,beta2=beta2,minv=minv,maxv=maxv),from=from,to=to,mod=model,lwd=lwd,lty=lty)
}
}

How plot multiple ggplots curves on the same Graph

i'm trying to plot multiple curves on the same Graph with ggplot2.... Actually i'm using a function "draw.data" with some parameters, so i can have multiple plots. But i got an issue, by the first call, i got a plot, once i call the method the 2nd time, i have an error. But i want the curves to be drew on the same Plot.
Hier are my codes:
# 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)
}
}
My call of the function "draw.data" with arguments 'add = F' the second time give my this error:
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)
What's wrong with this call? And how am i supposed to implement draw.data, so i can get into the else part and call stat_function to draw the 2nd curve on the same graph?
Update:
Hier ist the result:
> 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

Resources