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 Peter L. Flom, PhD Brainscope, Inc. 212 263 7863 (MTW) 212 845 4485 (Th) 917 488 7176 (F) [[alternative HTML version deleted]]
on 07/29/2008 11:38 AM Peter Flom 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. > > PeterHey Peter, LTNS! Job change it looks like from the e-mail address? Here is one approach. It is not clear from the above, where 'startage', 'endage' and 'decline' come from, so I am passing them as arguments: In your example above, the function call would be: MyPlot(diffs$BReP3O1T, diffs$diff_BReP3O1T, startage, endage, decline) MyPlot <- function(x, y, startage, endage, decline) { start <- x - y end <- x s1 <- seq(47, 89, length = 10) # Don't need to use min/max on each vector separately ymin <- min(start, end, na.rm = TRUE) ymax <- max(start, end, na.rm = TRUE) s2 <- seq(ymin, ymax, length = 10) # Turn 'x' into a label, stripping anything before "$" if present label <- gsub("^.+\\$", "", deparse(substitute(x))) plot(s1, s2, type = "n", xlab = "Age", ylab = label, main = paste("Age, decline and", label) segments(startage, endage, start, end, col = decline) legend("topleft", legend = c("Stable", "Decline"), lty = 1, col = c(1, 2)) } HTH, Marc Schwartz
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
One clarification: I did the dirty and completely unprofessional thing, and assumed that this function would only be run in the workspace with all your data. I use functions like this frequently for something I need to do multiple times for a particular project, but will never do anywhere else, but it isn't proper programming, and will fail if it can't find diffs and the other items referenced. If you need this to be portable, you will need to pass them all as arguments to the function. Sarah -- Sarah Goslee http://www.functionaldiversity.org
Just a quick follow up to my own post here. In offline exchanges, Peter noted a typo in the call to plot(), where I was missing the final ")". Also, I had the order of the unnamed arguments to segments() off. Here is the corrected code, with some additional teaks: plotdiffs <- function(x, y, startage, endage, decline) { startvar <- x - y endvar <- x s1 <- seq(47, 89, length = 10) ymin <- min(startvar, endvar, na.rm = TRUE) ymax <- max(startvar, endvar, na.rm = TRUE) s2 <- seq(ymin, ymax, length = 10) label <- gsub("^.+\\$", "", deparse(substitute(x))) plot(s1, s2, type = "n", xlab = "Age", ylab = label, main = paste("Age, decline and", label)) segments(startage, startvar, endage, endvar, col = decline) legend("topleft", legend = c("Stable", "Decline"), lty = 1, col = c(1, 2)) } Marc