[R] plot function creating bars instead of lines

Jim Lemon jim at bitwrit.com.au
Tue Apr 24 13:13:42 CEST 2012


On 04/24/2012 01:25 AM, la mer wrote:
> Hello,
>
> I am having a problem where code that plots lines using a different data
> frame plots bars with the current data frame (I am intended to plot lines).
> The code specifies lines (see below), so I can't figure out why the results
> are bars. I suspect that it may have something to do with the fact that in
> the data frame where the code worked as intended, the both variables
> specifying different lines were numeric, whereas in the current data frame
> one of those variables (challenge) is a factor with 2 levels. Any
> suggestions for getting this to plot as intended would be much appreciated.
>
> Thank you!
>
> ************ This is meant to plot a separate line for each subject for each
> challenge*************
> for (subj in unique(lab.samples$subid)) {
> 	#par(new=T)
> 	plot.new()
> 	par(mfrow=c(2,1))
> 	par(mfg=c(1,1))
> 	plot(data=lab.samples, subset=(subid==subj), cortisol ~ Sample, type='n',
> 		main=paste('Cortisol and Amylase for subject ', as.character(subj)))
>
> 	for ( t in unique(subset(lab.samples,subid==subj)$challenge) ) {
> 		par(mfg=c(1,1))
> 		lines(data=lab.samples, subset=(subid==subj&  challenge==t),
> 			cortisol ~ Sample, type='b', pch=as.character(t), col=rainbow(2)[t])
> 	}
> 	par(mfg=c(2,1))
> 	plot(data=lab.samples, subset=(subid==subj), amylase ~ Sample, type='n')
> 	for ( t in unique(subset(lab.samples,subid==subj)$challenge) ) {
> 		par(mfg=c(2,1))
> 		lines(data=lab.samples, subset=(subid==subj&  challenge==t),
> 			amylase ~ Sample, type='b', pch=as.character(t), col=heat.colors(2)[t])
> 	}
> }	
>
Hi la mer,
Without any data it is a bit difficult, but I would first try using 
as.numeric(challenge). I tried faking some data:

lab.samples<-data.frame(subid=rep(1:10,each=4),
  amylase=runif(40),cortisol=runif(40),
  Sample=rep(1:4,10),challenge=factor(rep(c("t","f"),20)))

but got nothing on the plots.

Jim



More information about the R-help mailing list