[R] help with making a function of scatter plot with multiple variables

jim holtman jholtman at gmail.com
Thu Sep 20 22:31:45 CEST 2007


The simple way is to enclose it in a 'function' and pass parameters.
Assuming that you have the same number of parameters, then the
following will do:

my.func <- function(x,y,d1,v1,s1,t1,s2,t2,s3,t3,s4,t4,s5,t5)
{
    op <- par(bg = "grey97")
    par(mfrow=c(1,2))
    plot(d1,v1, pch="v", col="orange",cex=0.6, lwd=2,
       xlab="day", ylab="resp",cex.main =1,font.main= 1,main=" Surv
data",ylim=y,xlim=x,
       col.main="navyblue",col.lab="navyblue",cex.lab=0.7)

   points(s1,t1, pch="A", col="green4", cex=1)
   points(s2,t2, pch="B",col="navyblue", cex=1)
   points(s3,t3, pch="C",col="red", cex=1)
   points(s4,t4, pch="D",col="darkviolet", cex=1)
   points(s5,t5, pch="E",col="blue", cex=1)
     legend("topright",lbels,col=c("orange","green4","navyblue","red","darkviolet","blue"),
    text.col=c("orange","green4","navyblue","red","darkviolet","steelblue"),
    pch=c("v","A","B","C","D","E"),bg='gray100',cex=0.7,box.lty=1,box.lwd=1)
   abline(h = -1:9, v = 0:8, col = "lightgray", lty=3)
    par(op)
}

# call it with

my.func(x,y,d1,v1,s1,t1,s2,t2,s3,t3,s4,t4,s5,t5)

You might also include the data in a list to make it easier



On 9/20/07, Tom Cohen <tom.cohen78 at yahoo.se> wrote:
> Dear list,
>
>  I have done a scatter plot of  multiple variables in the same graph, with different col and pch. I managed to do it with the following code but not know how to make a function of these so that next time if I want to do similar graph but with new variables, I dont have to copy the code and then change the old variables with the new ones but just call a function with the new variables. I dont have any experience in making a function and would be very grateful if you can help me. A function will shorten my prog dramatically, since I repeat tthis type of graph alots in my analysis.
>
>  Thanks in advance,
>  Tom
>
>  op <- par(bg = "grey97")
> par(mfrow=c(1,2))
> plot(d1,v1, pch="v", col="orange",cex=0.6, lwd=2,
>    xlab="day", ylab="resp",cex.main =1,font.main= 1,main=" Surv data",ylim=y,xlim=x,
>    col.main="navyblue",col.lab="navyblue",cex.lab=0.7)
>
>    points(s1,t1, pch="A", col="green4", cex=1)
>    points(s2,t2, pch="B",col="navyblue", cex=1)
>    points(s3,t3, pch="C",col="red", cex=1)
>    points(s4,t4, pch="D",col="darkviolet", cex=1)
>    points(s5,t5, pch="E",col="blue", cex=1)
>      legend("topright",lbels,col=c("orange","green4","navyblue","red","darkviolet","blue"),
>     text.col=c("orange","green4","navyblue","red","darkviolet","steelblue"),
>     pch=c("v","A","B","C","D","E"),bg='gray100',cex=0.7,box.lty=1,box.lwd=1)
>    abline(h = -1:9, v = 0:8, col = "lightgray", lty=3)
> par(op)
>
>
>
>
> ---------------------------------
>
> Jämför pris på flygbiljetter och hotellrum: http://shopping.yahoo.se/c-169901-resor-biljetter.html
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list