[R] how to get a line plot before/after treatment

Marc Schwartz MSchwartz at medanalytics.com
Wed May 28 20:29:42 CEST 2003


On Wed, 2003-05-28 at 11:48, Frank Mattes wrote:
> Dear R help-list reader,
> 
> I would like to generate a plot which compares to states in a patient 
> treatment, before and after. for this reason I have generated a vector
> before<-c(1,30,23,40)
> and
> after<-c(20,10,20,60)
> the first element in "before" corresponds to the first element in "after".
> I would like and generate a dotplot with
> before and after as x-scale, the elements of "before" and "after" on 
> the y-scale and the corresponding elements connected with a line.
> 
> However, so far I couldn't figure out how to do this in R. If anyone 
> as a suggestion, please let me know
> 
> Many thanks
> 
> Frank


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




More information about the R-help mailing list