简体   繁体   中英

Plot multiple similar equations in r

there I am new on R. I want to plot a graph like this. 在此处输入图片说明

The curves are created by these equations :

(log(0.4)-(0.37273*log(x)-1.79389))/0.17941 
(log(0.5)-(0.37273*log(x)-1.79389))/0.17941
(log(0.6)-(0.37273*log(x)-1.79389))/0.17941 

etc. The equations are similar, the only difference is the first log(XXX). I already manually draw the graph by repeating plot() for each equation. But I think there must be a way to just assign a simple variable like

x<-c(0.4,0.5,0.6,0.7)

and then plot all the curves automatically. I tried to use data frame to make a set of equations, but failed.

You can create a function-generating function and then loop over values of interest. For example

# takes a value, returns a function
logfn <- function(b) {
    function(x) (log(b)-(0.37273*log(x)-1.79389))/0.17941
 }

x <- c(0.4,0.5,0.6,0.7)
# empty plot
plot(0,0,type="n", ylim=c(-5,5), xlim=c(1,8), xlab="Lenght", ylab="Z-score")
# add plots for questions with `curve()`
for(v in x) {
    curve(logfn(v)(x),add=T)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM