3

Say I have a dataframe named as "data":

Sal T1  T2  T3  T4  T5  T6
29.0    4.00    NA  NA  NA  NA  NA
29.5    7.50    NA  NA  NA  NA  NA
30.0    10.40   1.50    NA  NA  NA  NA
30.5    12.50   6.00    NA  NA  NA  NA
31.0    14.50   9.00    NA  NA  NA  NA
31.5    16.25   11.50   4.00    NA  NA  NA
32.0    18.00   13.50   7.50    NA  NA  NA
32.5    19.50   15.25   10.00   1.50    NA  NA
33.0    20.90   17.00   12.20   5.55    NA  NA
33.5    22.40   18.50   14.10   8.50    NA  NA
34.0    23.60   20.00   16.05   11.00   4.0 NA
34.5    24.90   21.50   17.50   13.00   7.1 NA
35.0    NA  22.75   19.20   14.70   9.7 1.5
35.5    NA  23.90   20.50   16.50   11.9    5.5

I need to plot all columns with respect to column 1 in one graph. I can do it one by one by adding points and lines to the initial plot. For example:

mydata1 = na.omit(data[1:2])
plot(mydata1)
lines(mydata1,col=1)
mydata2 = na.omit(data.frame(c(data[1]),c(data[3])))
points(mydata2, col =2)
lines(mydata2,col=2)

But it is too tedious. I am thinking using for loop to plot all at once. But it seems only plotting the last one:

for (i in 2:ncol(data)){
  mydata = na.omit(data.frame(c(data[1]),c(data[i])))
  plot(mydata)
  lines(mydata)
}

How do I do this in R? Thanks for any help.

Here is the final solution:

plot(0,0,xlim=c(28,37),ylim=c(0,25),type="n",ylab = "T", xlab = "Sal")

for (i in 2:ncol(data)){
  mydata = na.omit(data.frame(c(data[1]),c(data[i])))
  lines(mydata, col = i)
  legendN = colnames(data)
  legendN = legendN[2:7]
  legend("topleft", legend = legendN, col= 2:7, pch=1) # optional legend
}
2
  • Maybe throw plot() outside the loop? Lines and points leave in the loop. Commented Mar 1, 2016 at 18:32
  • Won't working in that way. Commented Mar 1, 2016 at 18:38

2 Answers 2

4

You could also melt the data and use ggplot2

library(data.table)
library(ggplot2)
#data1 <- as in @John_West's post
dat <- data.table(data1)
melted <- melt(dat,id.vars="Sal",variable.factor=F)

> melted
     Sal variable value
 1: 29.0       T1  4.00
 2: 29.5       T1  7.50
 3: 30.0       T1 10.40
 4: 30.5       T1 12.50
 5: 31.0       T1 14.50
 6: 31.5       T1 16.25
 7: 32.0       T1 18.00
 8: 32.5       T1 19.50
 9: 33.0       T1 20.90
10: 33.5       T1 22.40
11: 34.0       T1 23.60
12: 34.5       T1 24.90
13: 35.0       T1    NA
14: 35.5       T1    NA
15: 29.0       T2    NA
16: 29.5       T2    NA
17: 30.0       T2  1.50
18: 30.5       T2  6.00
19: 31.0       T2  9.00
20: 31.5       T2 11.50
...

ggplot(melted,aes(x=Sal,y=value,group=variable)) + geom_line()

ggplot of values by variable on y vs original first column on x

Incidentally this strategy makes it easy to distinguish the data by the kind of observation as well:

ggplot(melted,aes(x=Sal,y=value,color=variable)) + geom_line()

Same with coloring of line segments by variable

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Both of your solutions are good, but I can choose one as the answer for this problem.
3

At first, I suggest you to take plot outside for loop, and leave lines in the loop.

At second, you did not scale the axes properly.

1.dat:

Sal T1 T2 T3 T4 T5 T6
29.0 4.00 NA NA NA NA NA
29.5 7.50 NA NA NA NA NA
30.0 10.40 1.50 NA NA NA NA
30.5 12.50 6.00 NA NA NA NA
31.0 14.50 9.00 NA NA NA NA
31.5 16.25 11.50 4.00 NA NA NA
32.0 18.00 13.50 7.50 NA NA NA
32.5 19.50 15.25 10.00 1.50 NA NA
33.0 20.90 17.00 12.20 5.55 NA NA
33.5 22.40 18.50 14.10 8.50 NA NA
34.0 23.60 20.00 16.05 11.00 4.0 NA
34.5 24.90 21.50 17.50 13.00 7.1 NA
35.0 NA 22.75 19.20 14.70 9.7 1.5
35.5 NA 23.90 20.50 16.50 11.9 5.5

Code:

x11()
data1 <- read.table("1.dat", header=TRUE, sep = " ", check.names=FALSE, as.is=TRUE)
plot(0,0,xlim=c(28,37),ylim=c(0,25),type="n",xlab="X",ylab="Y")

for (i in 2:ncol(data1)){
mydata = na.omit(data.frame(c(data1[1]),c(data1[i])))
lines(mydata)
}
Sys.sleep(10)

enter image description here

1 Comment

Thanks! Both of your solutions are good, but I can choose one as the answer for this problem. I pasted the final code based on ur suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.