[R] Graphics function question

Sarah Goslee sarah.goslee at gmail.com
Tue Jul 29 19:15:43 CEST 2008


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



More information about the R-help mailing list