-1

I read a case with the same title in stackoverflow, but it did not solve my questions. I have a data frame like this:

ID T1 T2 T3 T4....T10
1  40 50 50 70....40
2  30 60 29 50....NA
3  59 80 NA NA....NA
4  80 90 90 NA....NA

I want to have just one graph putting T1, T2, T3...T10 on the x-axis, and the scores in T1, T2...T10 on the y-axis. Each id forms a single line with the changing of the scores. I used add=TRUE for each one, but it is still cumbersome because I have more than 300 IDs. How can I do it quickly in plot or even ggplot?

Thanks!

William Liu
  • 339
  • 1
  • 2
  • 9

1 Answers1

1
d <- data.frame(id=1:5,t1=1:5,t2=2:6,t3=3:7)
d[3,4]=NA
d
plot(1:(length(d)-1),ylim=range(d,na.rm=TRUE),type="n",xaxt="n",xlab="",ylab="")
for(i in 1:nrow(d)) points(unlist(d[i,])[-1],col=i,type="o")
axis(1,at=1:(length(d)-1),labels=names(d)[-1])
DaveTurek
  • 1,297
  • 7
  • 8
  • Ah, forgot about `matplot(d[,-1])`. So many useful things built into R (if maybe not so useful in this case as @Roland points out). – DaveTurek Mar 23 '15 at 16:06
  • Thanks, DaveTurek. But there are NAs at different locations for different IDs. What I am sure is that once there is an NA in one row, the rest columns in that row will be all NAs. – William Liu Mar 23 '15 at 16:15
  • @William Liu it looks to me like both my code and `matplot` should handle those cases. (Though it should be `matplot(t(d[,-1]),...)`.) Am I missing something? – DaveTurek Mar 23 '15 at 16:27