NLM09 - 2 months ago 12

R Question

I have 300 variables (columns) taken at 10 timepoints (rows), for each variable at any given timepoint I have temperature values A and F.

Attached is a sample of the dataframe

`structure(list(Timepoint = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L,`

5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 13L, 13L, 25L, 25L),

Temperature = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,

1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("A",

"F"), class = "factor"), Concentration.of.chylomicrons = c(1.29e-11,

1.25e-11, 1.02e-11, 1.1e-11, 1.08e-11, 1.3e-11, 1.28e-11,

1.26e-11, 1.06e-11, 1.32e-11, 8.85e-12, 1.21e-11, 8.83e-12,

1.08e-11, 1.35e-11, 1.12e-11, 8.99e-12, 1.08e-11, 9.55e-12,

1.04e-11, 0, 1.01e-11), Total.lipids = c(0.00268, 0.0026,

0.00208, 0.00225, 0.00222, 0.0027, 0.00268, 0.0026, 0.00219,

0.00273, 0.0018, 0.00247, 0.00179, 0.00221, 0.00276, 0.00229,

0.00182, 0.00222, 0.00195, 0.00212, 0, 0.00204), Phospholipids = c(0.000224,

0.000223, 0.000145, 0.00016, 0.000157, 0.000211, 0.00023,

0.000211, 0.000165, 0.000224, 0.000109, 0.00018, 0.000113,

0.000163, 0.000175, 0.000177, 0.000122, 0.000173, 0.000127,

0.000156, 0, 0.000138)), .Names = c("Timepoint", "Temperature",

"Concentration.of.chylomicrons", "Total.lipids", "Phospholipids"

), class = "data.frame", row.names = c(NA, -22L))

I would like to draw a line graph to show how each variable varies with time. On this line graph I would like the A and F lines to be drawn.I have successfully managed to write the loop code for this.

`# subset based on temperatures A and F`

a_df <- subset(df, Temperature == "A")

f_df <- subset(df, Temperature == "F")

# loop from columns 3:x

for (i in 3:x) {

plot(a_df[, 1],

a_df[, i],

type = "l",

ylab = colnames(a_df[i]),

xlab = "Timepoint",

lwd = 2,

col = "blue")

lines(f_df[, 1],

f_df[, i],

type = "l",

lwd = 2,

col = "red")

legend("bottomleft",

col = c("blue", "red"),

legend = c("Temperature A", "Temperature F"),

lwd = 2,

y.intersp = 0.5,

bty = "n")

}

However for certain variables, certain points are outside the plot area, image attached below

Please click on this link for image

How can I make sure that in this loop command I can have graghs with all points visible. Im sure there is a quick way to fix this, can anyone help?

I have tried the following line, kindly suggested

ylim = c(min(f_df[,-1] ,max(f_df[,-1]),

I get the following error message

for (i in 3:229) {

+ plot(a_df[, 1],

+ a_df[, i],

+ type = "b",

+ ylim = c(min(f_df[,-1] ,max(f_df[,-1]),

+ ylab = colnames(f_df[i]),

+ main = colnames(f_df[i]),

+ xlab = "Timepoint",

+ lwd = 2,

+ col = "red")

+ lines(f_df[, 1],

Error: unexpected symbol in:

" col = "red")

lines"

f_df[, i],

Error: unexpected ',' in " f_df[, i],"

type = "b",

Error: unexpected ',' in " type = "b","

lwd = 2,

Error: unexpected ',' in " lwd = 2,"

col = "blue")

Error: unexpected ')' in " col = "blue")"

legend("bottomleft",

+ col = c("red", "blue"),

+ legend = c("Ambient", "Fridge"),

+ lwd = 2,

+ y.intersp = 0.5,

+ bty = "n")

Error in strwidth(legend, units = "user", cex = cex, font = text.font) :

plot.new has not been called yet

}

Error: unexpected '}' in "}"

Lakmal

Answer

To recap in an answer. Setting `ylim`

solves the issue

```
# loop from columns 3:x
for (i in 3:x) {
plot(a_df[, 1],
a_df[, i],
type = "l",
ylab = colnames(a_df[i]),
xlab = "Timepoint",
ylim = c(min(df[,-1]) ,max(df[,-1])),
lwd = 2,
col = "blue")
...
```

sets the plot boundaries as equal for each plot which is better if you want to compare the plots but has the downside that the plot area might be considerably larger than your data.

```
# loop from columns 3:x
for (i in 3:x) {
plot(a_df[, 1],
a_df[, i],
type = "l",
ylab = colnames(a_df[i]),
xlab = "Timepoint",
ylim = c(min(df[,i]) ,max(df[,i])),
lwd = 2,
col = "blue")
...
```

sets new boundaries for each plot, which is worse for comparison but reduces unnecessary empty plot space. I've replaced `min(a_df[, i],f_df[, i])`

with `min(df[,i])`

since they should be identical.