s-news
[Top] [All Lists]

[S] Answer to my question 'add text to the plot'

To: s-news@wubios.wustl.edu
Subject: [S] Answer to my question 'add text to the plot'
From: Jeff Wang NONLILLY <WANG_JEFF_NONLILLY@lilly.com>
Date: Fri, 17 Dec 1999 16:16:43 -0500
Cc: Jeff Wang NONLILLY <WANG_JEFF_NONLILLY@lilly.com>
Sender: owner-s-news@wubios.wustl.edu


Dear S-Plus users,

Happy holidays!

Attached is a summary of answers to my original question and
thanks for those who replied my question (I list their names below).

Jeff Wang


ORIGINAL QUESTION:
----------------------------------------------------------------------------
>Dear S-Plus users,
>
>I have a subset data in the following:
>
>event  freq  monyr
>FEVER     3 1997.08
>FEVER    18 1997.17
>FEVER     9 1997.25
>FEVER     8 1997.33
>FEVER     7 1997.42
>FEVER     2 1997.50
>RASH     10 1997.08
>RASH     25 1997.17
>RASH     13 1997.25
>RASH     10 1997.33
>RASH     22 1997.42
>RASH      8 1997.50
>
>I want to plot frequency of event versus time by each event.
>My question is that how can put EVENT label on each plot, such as FEVER or
>RASH?
>So I can identify each plot by event name.
>
>Thanks,
>
>Jeff Wang

INDIVIDUALS WHO REPLIED (Thanks a lot, I solved the problem!):
----------------------------------------------------------------------------
Bill Dunlap
Nick Ellis
Andrzej Galecki
Renaud Lancelot
Melissa Matzke
Andrew Sinclair
Anne E. York


FOLLOWING ARE ANSWERS PROVIDED BY ABOVE-MENTIONED INDIVIDUALS:
----------------------------------------------------------------------------
>From Andrzej Galecki:
----------------------------------------------------------------------------
> help(plot)

is relevant to your problem:

       population <- state.x77[,"Population"]
       area <- state.x77[,"Area"]
       plot(area, population, log="xy", xlab="Area in square miles",
             ylab="Population in thousands")
       states.lab <- c("Alaska", "California", "Florida", "Hawaii",
      "New Jersey",
             "New York", "Rhode Island", "Texas", "Wyoming")
       text(area[states.lab], population[states.lab],
             paste(" ", states.lab, sep=""), adj=0

----------------------------------------------------------------------------
>From Anne E. York:
----------------------------------------------------------------------------
Do you want to make separate plots for each level of event? OR do  you want
one plot with levels of event specified in a legend?

For the former if DF is the data frame
foo <- unique(levels(event))
foo.n <- length(foo)
for (i in 1:n){
plot(DF$monyr[event==foo[i],DF$freq[event==foo[i],main=as.character(foo[i],...)

}

For the later, here is a function that I have changed over the years:
"Cplot" <-
function(x, y, z, leg = T, low = F, lsl = F, line = F, chars = c(16, 1, 17,
2, 18, 5, 3, 4, 6, 0), ltypes = c(1:10), col = rep(1, 10), f = 2/3,
cex.leg = 1, max.levels.to.plot = 10, which.part = 1, ...)
{
#Written by AE York Sept 1994; last revision April 1999
#
#
#use: plots y versus x with different symbols
#     corresponding to the values of z
#     optionally use different plotting characters
# If the number of levels of z is > max.levels.to.plot,
# only first max.levels.to.plot levels are plotted (which.part = 1)
# (if which.part = 2, levels max.levels.to.plot +1 to 2*max.levels to plot
# are plotted, etc)
# if leg =T, a legend is made at locator(1) (ie where you click your mouse)
# if low = T, a lowess fit (f = f) is shown for each level of z
# if lsl = T, a least squares line if shown for each level of z
# if line = T, successive points are joined with a line
        newz <- factor(as.character(z))        #gets rid of missing factors
        levels.of.z <- levels(newz)
        t.lev <- length(levels(newz))
        to.plot <- (max.levels.to.plot * which.part - (max.levels.to.plot -
1)
                ):(min(max.levels.to.plot * which.part, t.lev))
        n.lev <- length(to.plot)
        #        out <- list(levels.of.z, t.lev, to.plot, n.lev)
#        return(out)
#        exit()
        plot(x, y, pch = " ", ...)
        for(i in to.plot) {
                xx <- x[newz == levels.of.z[i] & !is.na(x) & !is.na(y)]
                yy <- y[newz == levels.of.z[i] & !is.na(x) & !is.na(y)]
                what.char <- round(i %% (max.levels.to.plot + 0.1))
                points(xx, yy, pch = chars[what.char], col=col[what.char])
                if(low == T) {
                        lines(lowmiss(xx, yy, f = f), lty =
                         ltypes[what.char])
                }
                if(lsl == T) {
                        mod.temp <- glm(y ~ x, subset = newz ==
levels.of.z[[i
                                ]], na.action = na.omit)
                        abline(coef(mod.temp), lty = ltypes[what.char], col
=
                                col[what.char])
                }
                if(line == T) {
                        ord <- order(xx)
                        lines(xx[ord], yy[ord], lty = ltypes[what.char],
col
                                 = col[what.char])
                }
        }
        if(leg == T & low == F & lsl == F & line == F) {
                legend(locator(1), levels.of.z[to.plot], marks =
chars[1:n.lev
                        ], col = col[1:n.lev], background = -999999, cex =
                        cex.leg)
        }
        if(leg == T & (low == T | lsl == T | line == T)) {
                legend(locator(1), levels.of.z[to.plot], marks =
chars[1:n.lev
                        ], lty = ltypes[1:n.lev], col = col[1:n.lev],
                        background = -999999, cex = cex.leg)
        }
}

----------------------------------------------------------------------------
>From Bill Dunlap:
----------------------------------------------------------------------------
Is
     xyplot(freq~monyr|event, data=data)
satisfactory?

If you want to do it by hand then
     par(mfrow=c(1,2)) # to get 2 plots on page
     for(event in unique(as.character(data$event)))
          plot(data$monyr[data$event==event],
               data$freq[data$event==event], main=event,
               xlab="Year", ylab="Frequency")
should do it.


----------------------------------------------------------------------------
>From Nick Ellis:
----------------------------------------------------------------------------
plot(x,y,type='n')
text(x,y,string)
If you want the string rotated or justified, look up srt and adj in the help
pages for text() or par().

----------------------------------------------------------------------------
>From Renaud Lancelot:
----------------------------------------------------------------------------
Hi Jeff,

test <- read.table(file="clipboard")
> test
   event freq  mon.yr
 1 FEVER    3 1997.08
 2 FEVER   18 1997.17
 3 FEVER    9 1997.25
 4 FEVER    8 1997.33
 5 FEVER    7 1997.42
 6 FEVER    2 1997.50
 7  RASH   10 1997.08
 8  RASH   25 1997.17
 9  RASH   13 1997.25
10  RASH   10 1997.33
11  RASH   22 1997.42
12  RASH    8 1997.50

names(test) <- c("event", "freq", "mon.yr")

### Basic
xyplot(freq ~ mon.yr | event, data=test)

### Enhanced
xyplot(freq ~ mon.yr | event,
     data = test,
     par.strip.text = list(cex = 1),
     xlab=list("Time", cex=5/4), ylab=list("Frequency", cex=5/4),
     scales = list(cex = 1),
     panel = function(x, y){
          panel.xyplot(x, y, col=1, cex=3/2, type="b")})


Hope this helps,

----------------------------------------------------------------------------
>From Andrew Sinclair:
----------------------------------------------------------------------------
If you like trellis graphics you can do

     xyplot(freq ~ monyr | event, data = your.data.frame.name)

This will label the subplots automatically.


Otherwise try :

     par(mfrow=c(2,1)) ### assuming you want a 2 x 1 layout
     attach(your.data.frame.name)
     ### apply a function to each event type
     sapply(unique(event), function(ievent){
                    ## X-Y plot of data for ith event
                    plot(monyr[event==ievent],
freq[event==ievent])
                    ## add title
                    title(ievent) })


Trust this is of some help,


----------------------------------------------------------------------------
>From Melissa Matzka
----------------------------------------------------------------------------
This code should work,

>plot(monyr,freq,type='n')
>text(monyr,frew,label=as.character(event))

Best of luck,

Melissa


-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>
  • [S] Answer to my question 'add text to the plot', Jeff Wang NONLILLY <=