Re: [R] Graphics function question

From: Sarah Goslee <sarah.goslee_at_gmail.com>
Date: Tue, 29 Jul 2008 13:15:43 -0400

You can do it very easily using subsetting and a bit of paste().

I assumed you didn't need startdata and enddata after the plot has been made, but if you do you can change the last line of the function to return them.

Sarah

myfunction <- function(dataname)
{
# where dataname is a string, eg myfunction("BReP3O1T")

startdata <- diffs[, dataname] - diffs[, paste("diff", dataname, sep="_")] enddata <- diffs[, dataname[

x <- seq(47,89, length = 10)
ymin <- min(min(startdata), min(enddata))
> ymax <- max(max(startdata), max(enddata))
y <- seq(ymin, ymax, length = 10)
plot(x,y, type = 'n', xlab = 'Age', ylab = dataname, main = paste("Age, decline and", dataname)
segments(x0 = startage, x1 = endage, y0 = startdata, y1 = enddata, col = decline)
legend('topleft', legend = c('Stable', 'Decline'), lty = 1, col = c(1,2)) invisible()

}

On Tue, Jul 29, 2008 at 12:38 PM, Peter Flom <peterf_at_brainscope.com> wrote:
> Hello
>
> I have created a graph using the following commands:
>
> <<<
> startBReP3O1T <- diffs$BReP3O1T - diffs$diff_BReP3O1T
> endBReP3O1T <- diffs$BReP3O1T
>
> x <- seq(47,89, length = 10)
> ymin <- min(min(startBReP3O1T), min(endBReP3O1T))
> ymax <- max(max(startBReP3O1T), max(endBReP3O1T))
> y <- seq(ymin, ymax, length = 10)
> plot(x,y, type = 'n', xlab = 'Age', ylab = 'BReP3O1T', main = 'Age, decline and BReP3O1T')
> segments(x0 = startage, x1 = endage, y0 = startBReP3O1T, y1 = endBReP3O1T, col = decline)
> legend('topleft', legend = c('Stable', 'Decline'), lty = 1, col = c(1,2))
>>>>>>
>
> I would like to make this into a function. The only thing that changes is BReP3O1T.
> The problem is that sometimes this occurs as text (e.g. in ylab and main) sometimes after a $ (e.g. in the
> first two assigment statements), and sometimes it refers to the values (e.g. in ymin and ymax)
>
> Any help appreciated.
>
> Peter
>
>
>

-- 
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
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.
Received on Tue 29 Jul 2008 - 17:19:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Jul 2008 - 18:32:51 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive