Skip to content
Prev 32601 / 398530 Next

how to get a line plot before/after treatment

On Wed, 2003-05-28 at 11:48, Frank Mattes wrote:
Dr. Mattes,

How about this, using interaction.plot() in package nlme (I am presuming
that each pair of measures is an independent pairing). It takes a bit to
get the data in the proper structure, but the plotting is then easy:


before <- c(1,30,23,40)
after <- c(20,10,20,60)

# Create a new dataframe with columns:
# 'score', 'when' and 'unit'
# 'when' and 'unit' are set to factors

before.new <- data.frame(score = before, when = "Before", 
                         unit = factor(1:4))

after.new <- data.frame(score = after, when = "After", 
                        unit = factor(1:4))

df.new <- rbind(before.new, after.new)

#display df.new to see the structure
df.new

# load nlme
library(nlme)

#attach the dataframe to call variables directly
attach(df.new)

#create plot
interaction.plot(when, unit, score, ylab = "Score", xlab = "When", 
                 col = rainbow(4))

# detach df.new to clean up
detach(df.new)



See ?interaction.plot for more information. This is a simple example of
the function which can handle repeated measures (the default y axis is
the means of scores) and multiple x-axis factors (for example if you had
before, 1 wk after and 2 wks after).

HTH,

Marc Schwartz