Skip to content

A better way to do this

5 messages · Lao Meng, 1Rnwb, Dennis Murphy

#
Hello gurus,

I have a dataframe containing two groups viz., 'control' and 'case', each of
these groups contains longitudinal data for 100 subjects. I have to plot all
these subjects on a single chart and then put a regression line for each of
the group for all the subjects. I have written a function to do the chart
grpcharts<-function (dat, group,group2,molecule,cutoff){
dat2<-dat[grep(group,dat$Group),]
ylim=log2(c(min(dat2[,molecule],na.rm=T)+4,max(dat2[,molecule],na.rm=T)+1))
all.sub.id<-dat2$Subject.ID
 if (group=='control'){
	 col=c('blue')
}else{col=c('red')}

if(group2=='case'){
	col2=c('red')
}else{ col2=c('blue')}
uniq.sub.id<-unique(all.sub.id)
		 errcol<-c()
			for (i in 1: length(uniq.sub.id)) 
			{
		 	 sub<-dat2[grep(uniq.sub.id[i],dat2$Subject.ID),]
		 	 sub<- sub[order(sub$Age.at.Sample.Collection),]
			 sub<-sub[sub[,molecule]>cutoff,]
			 sub.id<-uniq.sub.id[i]
		 	 if (dim(sub)[1]<=1) errcol<-c(errcol, sub.id)
		 	 if (dim(sub)[1]>1) 
				{
				 par(new=TRUE)
				 plot(log2(sub[,molecule])~sub$Age.at.Sample.Collection,
ylab=paste('Log2_',molecule,sep=''),xlab="Age at Sample",pch=1, ylim=ylim,
xlim=c(0,25),main=paste(group,'-',molecule))
			 	 mod<-loess(log2(sub[,molecule])~Age.at.Sample.Collection,
na.action=na.exclude, data=sub)
			 	 pred<-predict(mod)
			 	 lines(pred~Age.at.Sample.Collection, data=sub,lwd=1, lty=1)
				 }
			}
		   dat2<-dat2[order(dat2$Age.at.Sample.Collection),]
		   mod<-loess(log2(dat2[,molecule])~Age.at.Sample.Collection,
na.action=na.exclude, data=dat2)
		   pred<-predict(mod)
		   lines(pred~Age.at.Sample.Collection, data=dat2,lwd=2, lty=1,col=col)
		   dat2<-dat[grep(group2,dat$Group),]
		   dat2<-dat2[order(dat2$Age.at.Sample.Collection),]
		   mod<-loess(log2(dat2[,molecule])~Age.at.Sample.Collection,
na.action=na.exclude, data=dat2)
		   pred<-predict(mod)
		   lines(pred~Age.at.Sample.Collection, data=dat2,lwd=2, lty=1,col=col2)
		   legend(c(20,20),c(ylim),c(group,group2), lty=1,lwd=2,col=c(col,col2),
bty='n')
		  print('done')	
	}
    
the function subsets the data based on the 'group' and plots the datapoints
currently I am using a loop to assign the colors under two conditions. I
need some pointers to assign the colors to the regression lines for the two
groups without using a loop.
thanks
sharad


--
View this message in context: http://r.789695.n4.nabble.com/A-better-way-to-do-this-tp3536576p3536576.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi:

Could you post a small, reproducible data set that illustrates what
you want to do? It sounds like you're creating 'spaghetti plots',
which can be done with a minimal amount of pain in ggplot2.

Dennis
On Thu, May 19, 2011 at 11:29 AM, 1Rnwb <sbpurohit at gmail.com> wrote:
2 days later
#
here is the data set, yes i am doing spagghetti plots for each Subject for
MCP1 with respect to the Age.at.Sample.Collection, with a final of all the
controls and all the cases

 Subject.ID sample   Group Age.at.Sample.Collection        MCP1
19      00173-0   3455 control                11.767282 212.4438625
104     00173-0   4992 control                 4.087611 222.6706897
148     00173-0   5046 control                 5.103353 257.5997389
192     00173-0   5205 control                 7.249828 191.8050229
223     00173-0   4991 control                 6.061601 171.2995728
235     00173-0   5295 control                 7.843942 240.2288223
275     00173-0   5366 control                 9.817932  64.9237708
319     00173-0   5473 control                10.704996 199.4368169
2102    00173-0   3140 control                15.260780 366.1121484
3174    00173-0   4950 control                14.012320 253.4390051
3883    00173-0   4103 control                12.974674 260.3490578
23      01039-0 901388 control                 3.058179 273.6916770
491     01039-0 901068 control                 1.284052 255.5981436
735     01039-0 900985 control                 1.968514 450.2616943
1199    01039-0 900733 control                 5.086926 161.5011670
3948    01039-0 901422 control                 4.041067 365.8560986
24      21038-0 901594 control                 8.418891 303.9160091
128     21038-0 900089 control                12.440793 250.4846256
381     21038-0 901157 control                 6.477754  35.3079462
486     21038-0 901739 control                 6.669404  77.1044173
582     21038-0 900313 control                10.464065 233.8540385
972     21038-0 900527 control                11.438740 194.8173368
1256    21038-0 900262 control                14.472279 175.7577419
1565    21038-0 901475 control                 2.015058 246.2465683
1641    21038-0 901216 control                 4.416153  99.7496760
1957    21038-0 901618 control                 6.669404 141.4459310
2291    21038-0 900563 control                 3.025325 278.6264989
3131    21038-0 900363 control                 5.470225 103.3293132
3704    21038-0 900872 control                 9.434633 153.7875157
3984    21038-0 900766 control                13.437371 269.4589484
2       00041-0   3810    case                17.368925 203.1979716
310     00041-0   4923    case                17.648186 168.8229783
444     00041-0   3842    case                16.375085 102.6136126
1082    00041-0   4078    case                16.813141  89.7548225
1459    00041-0   5005    case                14.817248 131.0459201
1464    00041-0   4816    case                16.082135 131.1120569
1470    00041-0   3495    case                15.323750 118.7034216
1677    00041-0   3991    case                 9.782340 169.1765831
1845    00041-0   4824    case                 8.520191 122.3348897
1928    00041-0   4599    case                16.651608   2.3051960
2142    00041-0   3146    case                15.164955 191.6823727
2184    00041-0   3220    case                15.526351 188.6667153
2283    00041-0   5239    case                18.269678 174.9620952
2542    00041-0   3536    case                10.910335 301.6238182
2883    00041-0   4787    case                13.527720 159.0956166
2921    00041-0   4058    case                17.845311 137.7700442
2932    00041-0   5015    case                 7.520876 101.6216590
3641    00041-0   4266    case                 5.497604 125.6831543
3683    00041-0   4360    case                 4.421629 187.2961108
3725    00041-0   4428    case                13.451060 157.4203222
3767    00041-0   4506    case                 6.507871 141.1115853
3889    00041-0   4455    case                12.147843 213.7489685
3       00709-0   5023    case                 2.004106 412.4962003
398     00709-0   3180    case                 5.234770   9.6108457
639     00709-0   3205    case                 4.974674 262.5951990
1058    00709-0   3788    case                 4.577686 281.6220685
1700    00709-0   3450    case                 4.156057 131.7354794
2050    00709-0   3183    case                 5.878165   0.3707844
2483    00709-0   4434    case                 2.162902 339.0410965
2789    00709-0   3849    case                 4.405201 405.9685980
3281    00709-0   3619    case                 5.541409 255.1460421
3842    00709-0   3943    case                 3.515400 624.2636701
4011    00709-0   5304    case                 3.312799   2.9206063
4       00174-0   4142    case                16.060232 240.4858235
13      00174-0   4244    case                 5.615331          NA
22      00174-0   4446    case                15.523613 223.3748681
57      00174-0   4292    case                 6.209445 168.6679707
100     00174-0   4314    case                 6.666666 147.6358300
183     00174-0   3689    case                16.224503   0.5689179
270     00174-0   4208    case                 3.468856 286.9223725
313     00174-0   4223    case                 4.427104 178.0005964
471     00174-0   3188    case                11.633127 150.2065531
554     00174-0   3212    case                12.377823 217.9799114
1304    00174-0   5193    case                14.694045 150.0169015
1463    00174-0   4443    case                13.626283 155.9339661
1589    00174-0   5294    case                13.990417 167.2262451
1884    00174-0   4349    case                15.271731 146.8486102
1975    00174-0   3857    case                10.611909  82.6978002
2084    00174-0   3174    case                10.924024 217.8106656
2196    00174-0   5126    case                16.892539 272.8141490
2748    00174-0   4607    case                16.481861 156.5503087
2963    00174-0   4861    case                11.967145 294.4989050
3294    00174-0   4859    case                12.783025 205.6801413
3566    00174-0   4380    case                 9.070499  84.6743348
3608    00174-0   4405    case                10.132785  92.1683089
3844    00174-0   4252    case                10.324435 305.3678248
3923    00174-0   4193    case                11.340177 336.7327758
--
View this message in context: http://r.789695.n4.nabble.com/A-better-way-to-do-this-tp3536576p3542376.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi:

Here are a few options for making this type of plot:

# ggplot2

library(ggplot2)
ggplot(dd, aes(x = Age.at.Sample.Collection, y = MCP1, group =
Subject.ID, colour = Subject.ID,
               shape = Group)) +
      geom_point(size = 2.5) + geom_line(size = 0.6) + labs(x = 'Age')

ggplot(dd, aes(x = Age.at.Sample.Collection, y = MCP1, group =
Subject.ID, colour = Subject.ID)) +
      geom_point(size = 2.5) + geom_line() + labs(x = 'Age') +
facet_wrap( ~ Group, ncol = 1)

ggplot(dd, aes(x = Age.at.Sample.Collection, y = MCP1, group =
Subject.ID, colour = Subject.ID)) +
      theme_bw() + geom_point(size = 2.5) + geom_line() +
      labs(x = 'Age') + facet_wrap( ~ Group, nrow = 1)

# lattice
subjectKey <- list(space = 'right',
                   title = 'Subject ID',
                   cex.title = 1.2,
                   text = list(levels(dd$Subject.ID)),
                   points = list(pch = rep(c(16, 17), each = 3), cex =
1, col = 1:6))

xyplot(MCP1 ~ Age.at.Sample.Collection | Group, data = dd, group = Subject.ID,
       type = c('p', 'a', 'g'), cex = 1, pch = rep(c(16, 17), each = 3),
       col = 1:6, xlab = 'Age',
       key = subjectKey)

xyplot(MCP1 ~ Age.at.Sample.Collection, data = dd, group = Subject.ID,
       cex = 1, pch = rep(c(16, 17), each = 3), col = 1:6, type =
c('p', 'a', 'g'), xlab = 'Age',
       key = subjectKey)

HTH,
Dennis
On Sun, May 22, 2011 at 9:46 AM, 1Rnwb <sbpurohit at gmail.com> wrote: