ggplot2: draw curve with ggplot2 - r

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.

Related

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)

I want to simulate moving average process of order one MA(1) with varying sample size n, varying SD values and varying theta values

I want to simulate some time series data with mean = 0 but varying:
Mathematically, moving average process of order one, MA(1) is presented as
$$x_t=\mu+\varepsilon_{t}+\theta_{1}\varepsilon_{t-1}$$
$x_t$ is the MA(1) process
$\mu$ is the mean which can be zero in my case (just like intercept in regression equation)
$\varepsilon_{t}$ is the error term
$\theta_{1}$ is a constant which need be specified (in my case, a varying number in between +-1). Example: in simple regression equation of $x=a+b*x_{i}$, $theta$ is like the $b$
Number N = 15, 20, 30, 50, 100, 200.
Standard Deviation SD=1, 4, 9, 16, 25.
and theta value \theta = +-0.2, +-0.4, +-0.6, +-0.8, +-0.9, +-0.95, +-0.99
set.seed(123)
# here I am only using first sample size 15
n <- 15
# white noise:
wnsd1<-ts(rnorm(n, mean=0, sd=1^2))
wnsd4<-ts(rnorm(n, mean=0, sd=2^2))
wnsd9<-ts(rnorm(n, mean=0, sd=3^2))
wnsd16<-ts(rnorm(n, mean=0, sd=4^2))
wnsd25<-ts(rnorm(n, mean=0, sd=5^2))
# initialise the first two values:
ma1 <- wnsd1[1:2]
# loop through and create the 3:15th values:
for(i in 3:n){
# here I only use when SD=1
ma1[i] <- wnsd1[i - 1] * 0.2 + wnsd1[i]
}
#turn them into time series, and for the last two, "integrate" them via cumulative sum
ma1 <- ts(ma1)
I want a mature way of varying the sample size N, the standard deviation SD and the estimate of MA(1) \theta
Here's an OK way. Note, I do not know how phi is used as it wasn't explicitly in the code. If you modify your code, I would try to address it.
N <- c(15L, 20L)
SD = c(1, 2)^2
phi = c(0.2, 0.4)
set.seed(123)
res <- lapply(N,
function(n)
lapply(SD,
function(s.d.) {
wn <- ts(rnorm(n, 0, s.d.))
ar1 <- ma1 <- arma11 <- arma22 <- vector('numeric', n)
ar1 <- ma1 <- arma11 <- arma22 <- wn[1:2]
for (i in 3:n) {
ar1[i] <- ar1[i - 1] * 0.2 + wn[i]
ma1[i] <- wn[i - 1] * 2.8000 + wn[i]
arma11[i] <- arma11[i - 1] * 0.2 + wn[i - 1] * 2.80003769654 + wn[i]
arma22[i] <- arma22[i - 1] * 0.862537 + arma22[i - 2] * (-0.3) + 0.2 * wn[i - 1] - 0.3 * wn[i -
2] + wn[i]
}
#turn them into time series, and for the last two, "integrate" them via cumulative sum
return(data.frame(ar1 = ts(ar1),
ma1 = ts(ma1),
arma11 = ts(arma11),
arima111 = ts(cumsum(arma11)),
arima222 = ts(cumsum(cumsum(arma22)))
))
}))
res <- setNames(lapply(res, setNames, paste('SD', SD, sep = '_')), paste('n', N, sep = '_'))
res
Result - truncated to only one combination:
$n_15
$n_15$SD_1
ar1 ma1 arma11 arima111 arima222
1 -0.5604756 -0.56047565 -0.56047565 -0.56047565 -0.5604756
2 -0.2301775 -0.23017749 -0.23017749 -0.79065314 -1.3511288
3 1.5126728 0.91421134 0.86816717 0.07751403 -0.4913603
4 0.3730430 4.43489167 4.60858386 4.68609790 2.3123144
5 0.2038963 0.32671123 1.24843066 5.93452856 5.9733306
6 1.7558443 2.07707065 2.32676165 8.26129021 11.5104337
7 0.8120851 5.26309817 5.72851515 13.98980536 19.1736717
8 -1.1026442 0.02550414 1.17122455 15.16102991 26.4205560
9 -0.9073817 -4.22902431 -3.99482709 11.16620282 31.5923395
10 -0.6271383 -2.36884996 -3.16784126 7.99836155 34.8956636
11 1.0986541 -0.02377172 -0.65735677 7.34100478 38.5509080
12 0.5795447 3.78724286 3.65581765 10.99682243 43.8085632
13 0.5166804 1.40825017 2.13942726 13.13624969 50.4482906
14 0.2140188 1.23284278 1.66074334 14.79699303 57.8822760
15 -0.5130374 -0.24592953 0.08622331 14.88321634 64.9327807
Edit: This approach is similar but uses explicit for loops instead of lapply and only returns the ma variable:
N <- c(15L, 20L)
SD = c(1, 2) ^ 2
phi = c(0.2, 0.4)
res <- vector('list', length(N))
names(res) <- paste('N', N, sep = '_')
set.seed(123L)
for (i in seq_along(N)){
res[[i]] <- vector('list', length(SD))
names(res[[i]]) <- paste('SD', SD, sep = '_')
ma <- matrix(NA_real_, nrow = N[i], ncol = length(phi))
for (j in seq_along(SD)){
wn <- rnorm(N[i], mean = 0, sd = SD[j])
ma[1:2, ] <- wn[1:2]
for (k in 3:N[i]){
ma[k, ] <- wn[k - 1L] * phi + wn[k]
}
colnames(ma) <- paste('ma_theta', phi, sep = '_')
res[[i]][[j]] <- ma
}
}
res
$N_15
$N_15$SD_1
ma_theta_0.2 ma_theta_0.4
[1,] 0.68374552 0.68374552
[2,] -0.06082195 -0.06082195
[3,] 0.62079632 0.60863193
[4,] 1.46210976 1.58870190
[5,] 0.27439361 0.54149714
[6,] 1.01901666 1.02047467
[7,] -0.98492231 -0.78141058
[8,] -0.95929125 -1.19697805
[9,] 1.37489682 1.23057594
[10,] 0.68123152 0.98507506
[11,] -1.97674523 -1.90126763
[12,] -1.77448202 -2.18492658
[13,] -0.47358851 -0.74639600
[14,] 0.82562320 0.78546700
[15,] 0.07127263 0.24442851
$N_15$SD_4
ma_theta_0.2 ma_theta_0.4
[1,] 2.4967499 2.4967499
[2,] 3.8360215 3.8360215
[3,] 7.4514236 8.2186279
[4,] 1.5609108 2.8977547
[5,] -0.1631142 -0.1183009
[6,] -7.0545350 -7.0961205
[7,] -1.0052795 -2.4078694
[8,] -2.2079382 -2.1284761
[9,] -4.3535184 -4.8109984
[10,] -1.4988326 -2.2780403
[11,] 3.9158477 3.7719227
[12,] -7.1590394 -6.3470849
[13,] -3.3033159 -4.8975147
[14,] 0.1247257 -0.2170977
[15,] -3.4795205 -3.3862106
$N_20
$N_20$SD_1
ma_theta_0.2 ma_theta_0.4
[1,] 0.33390294 0.3339029
[2,] 0.41142992 0.4114299
[3,] 0.04924982 0.1315358
[4,] -2.47250543 -2.4791127
[5,] 2.07827851 1.5850989
[6,] 0.30899237 0.8232840
[7,] 0.61013343 0.5690736
[8,] 0.40400515 0.5342438
[9,] 1.07942653 1.1341798
[10,] 1.02259409 1.2275287
[11,] -0.04626128 0.1172706
[12,] 0.33620914 0.2942505
[13,] -0.86977528 -0.7941417
[14,] 0.66784124 0.4787595
[15,] -0.28965374 -0.1182691
[16,] 2.32456569 2.2323580
[17,] -1.16769422 -0.6843396
[18,] -0.79419702 -1.1244068
[19,] 0.73258241 0.6397850
[20,] 0.67520852 0.8402845
$N_20$SD_4
ma_theta_0.2 ma_theta_0.4
[1,] -2.35792415 -2.35792415
[2,] -3.98712297 -3.98712297
[3,] -0.21952177 -1.01694637
[4,] 0.05835091 0.17393147
[5,] -7.17257088 -7.18401681
[6,] -1.29402072 -2.72624571
[7,] 0.78856212 0.81620297
[8,] 0.85108984 1.00327409
[9,] -4.08028705 -3.94050594
[10,] 1.06051948 0.21650585
[11,] 5.89518717 6.27609379
[12,] 2.92780172 4.03065783
[13,] -4.17736476 -3.81237564
[14,] -2.65105266 -3.55952343
[15,] 1.03589810 0.68738173
[16,] -2.31129963 -2.03441673
[17,] -9.14822185 -9.66585835
[18,] 1.81088621 0.08476914
[19,] -2.61050979 -1.90310913
[20,] -2.95782317 -3.62140526

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

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(...).

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