text/plain colorspaceplotter.txt — 3.3 KB

File contents

### Functions for plotting pixel color data to identify threshold values for image analysis
### These worked for me, but your mileage may vary. Use with caution. Eric Nord. ericnord@psu.edu
### type notes() for reminder of how they work
### getdata() assumes that your data is in a tab delimited text file called "color.txt", with 6 columns for:
	### class, x and y coordinates, and red, green, and blue intensities
	### note - the ImageJ macro "color saver" produces the last 5 columns, the first column can be added in a spreadsheet, or in R
### superplotter() produces a 3 panel plot with total brightness vs red:green, total vs  green:blue, and total vs. red.
### This was useful for me in my image analysis problem - you may need different plots - modify as needed
### top(x,y,z) (and bottom()) return the upper (lower) z values of variable y in class x

### Load data and calculate ratios
getdata=function(){
color<<-read.table("color.txt",header=T, sep="\t")
names(color)<<-c("class","x","y","r","g","b")
color[,7]<<-color$r/color$g
color[,8]<<-color$r/color$b
color[,9]<<-color$g/color$b
color[,10]<<-color$r+color$g+color$b
names(color)[7:10]<<-c("r.g","r.b","g.b","tot")
msg<-cat(nrow(color),"observation read from color.txt in", "\n",getwd(),"\n")
# return(msg)
}
notes=function(){
cat("getdata() reads data from a color.txt file","\n","top/bottom(grp,var,lim) give the upper/lower lim values of var in class grp.","\n","Note that grp and var are specified as names, eg in quotes.","\n", "plotter(xx,yy,grpcol,omit,Dta) plots the color space data in the xx and yy space for all","\n"," groups defined by the column number in grpcol (default is 1), omiting levels specified by omit.","\n","xx and yy specified with quotes.","\n")}

top=function(grp,var,lim){
sort(subset(color, color$class == grp, select=var)[,1],decreasing=T)[1:lim]
}

bottom=function(grp,var,lim){
sort(subset(color, color$class == grp, select=var)[,1],decreasing=F)[1:lim]
}

### Plot 2 variables against each other with groups
	# user supplies:
	# Dta   - a data.frame
	# xx,yy    - variables to plot (specified with quotes)
	# groups - grouping factor
	# omit   - indices of groups to omit
	# use levels(grouping factor) to set omit
	# to do 1)add a x and y scaling fn
plotter=function(xx,yy,grpcol=1,omit=0,Dta=color){
toplot<-levels(Dta[,grpcol])
grps<-Dta[,grpcol]
maxx<-0;maxy<-0;minx<-200;miny<-200
if(0 %in% omit){toplot<-toplot; ci<-0}else{toplot<-toplot[-omit]; ci<-length(omit)}
for (i in 1:length(toplot)){
	xs<-subset(Dta,grps==toplot[i])
	maxx<-max(max(with(xs,get(xx))),maxx)
	maxy<-max(max(with(xs,get(yy))),maxy)
	minx<-min(min(with(xs,get(xx))),minx)
	miny<-min(min(with(xs,get(yy))),miny)}
with(Dta,plot(get(xx),get(yy),type="n",xlab=xx,ylab=yy,xlim=c(minx,maxx),ylim=c(miny,maxy)))
sym<-19; symc<-c("green","plum","yellow","brown")
for (i in 1:length(toplot)){
	ci<-ci+1
 	with(subset(Dta,grps==toplot[i]),points(get(xx),get(yy),pch=sym,col=symc[ci]))
	} # end plotting
} # end fn

### Automatically produces the three plots most useful to me using the plotter funtion.

superplotter=function(){
cat(levels(color$class),"\n")
	x11(width = 18,height = 6, pointsize = 15)
	layout(matrix(c(1,2,3),1,3),c(1,1,1), 1,respect = TRUE)
plotter("r.g","tot",omit=0)
plotter("g.b","tot",omit=1)
plotter("r","tot",omit=c(1,2))
}