Skip to content
Prev 43464 / 398506 Next

Trouble plotting with factor

On Fri, 2004-01-30 at 15:04, Paul Johnson wrote:
Paul,

I did not see any responses come through yet on this, so I don't know if
you got anything offlist.

I do not know how your data is structured, but one approach, if your
data is in a matrix, with the numbers being the columns and the
agegroups being the rownames, is the following:

# Create the agegroups
agegroups <- c("15-19", "20-24", "25-29","30-34", "35-39",
               "40-44","45-49","50-54","55-59","60-64",
               "65-69", "70-74", "75-79", "80-84", "OVER")

# Create the first column
fracld <- c(0.4914204, 0.9752746, 1.0864465, 1.0555984, 1.0631969,
            1.0738725, 1.0971969, 1.0657212, 1.0217373, 0.9761226,
            0.9043233, 0.9045744, 0.8573243, 0.7889182, 0.5217992)

# Now create two additional columns for the example
# 'fracld2' won't make sense "real world" since many vals
# will be > 1.0
fracld1 <- fracld - 0.25
fracld2 <- fracld + 0.25

# Create the matrix
df <- cbind(fracld, fracld1, fracld2)

# Set the rownames
rownames(df) <- agegroups
fracld   fracld1   fracld2
15-19 0.4914204 0.2414204 0.7414204
20-24 0.9752746 0.7252746 1.2252746
25-29 1.0864465 0.8364465 1.3364465
30-34 1.0555984 0.8055984 1.3055984
35-39 1.0631969 0.8131969 1.3131969
40-44 1.0738725 0.8238725 1.3238725
45-49 1.0971969 0.8471969 1.3471969
50-54 1.0657212 0.8157212 1.3157212
55-59 1.0217373 0.7717373 1.2717373
60-64 0.9761226 0.7261226 1.2261226
65-69 0.9043233 0.6543233 1.1543233
70-74 0.9045744 0.6545744 1.1545744
75-79 0.8573243 0.6073243 1.1073243
80-84 0.7889182 0.5389182 1.0389182
OVER  0.5217992 0.2717992 0.7717992


# Now use matplot() to plot each column
# Do not plot the axes
matplot(df, type = "l", axes = FALSE,
        xlab = "Age Group", ylab = "Proportion DL Ownership")

# Create the X axis, specifying 15 tick marks 
# and using rownames(df) as the labels
axis(1, at = 1:nrow(df), labels = rownames(df))

# Now draw the Y axis with defaults
axis(2)

# Put a box around the whole thing
box()


BTW, this is also using FC1 and R 1.8.1 Patched.

HTH,

Marc Schwartz