Oops! I sent before attaching the code.
On 03/25/2010 06:03 AM, kathy_BJ wrote:
Jim, I really appreciate your hlep. You almost solve my problem, I had tried
both of your suggestions, but both of them only solve part of problem, and
how can I combine their functions? your second sample can't give the axis
(like 0-20, 20-50 etc), btw, we don't have "plotrix" function on our
machine.
That's okay, I have rewritten the x axis tick labelling so that you
don't have to use staxlab. That's why you didn't get the axis labels.
What I want looks like the second plot from
http://www.statmethods.net/graphs/boxplot.html,
Great! We finally have an example of what you want.
I want to compare the MB between the two dataset, so the first/third/fifth
box should from file1(MB) with same color (black), second/fourth/sixth box
from file2(MB) with same color(red). Of course, the first (black) box and
second(red) box share the same data range (eg 0-20), and so on for other
group of box.
The attached code does this for the values in the made-up data that have
col5 equal to RED (in your original post you indicated that you wanted
to separate these colors). You will probably want to change this
somewhat, but it is what I understand by the example and your
explanation above.
Jim
file1<-data.frame(
col1=sample(c(50:52,220:229),1000,TRUE)*1000000,
col2=sample(0:270,1000,TRUE),
col3=runif(1000,50,80),col4=runif(1000,50,80),
col5=sample(c("RED","GREEN","BLUE"),1000,TRUE))
file2<-data.frame(
col1=sample(c(50:52,220:229),1000,TRUE)*1000000,
col2=sample(0:270,1000,TRUE),
col3=runif(1000,50,80),col4=runif(1000,50,80),
col5=sample(c("RED","GREEN","BLUE"),1000,TRUE))
file1$col_group<-cut(file1$col2,breaks=c(0,20,50,70,271),
right=FALSE)
file2$col_group<-cut(file2$col2,breaks=c(0,20,50,70,271),
right=FALSE)
# get logical vectors that will pick out the different colors
reds1<-file1$col5=="RED"
reds2<-file2$col5=="RED"
greens1<-file1$col5=="GREEN"
greens2<-file2$col5=="GREEN"
blues1<-file1$col5=="BLUE"
blues2<-file2$col5=="BLUE"
# calculate the differences between col4 and col3
file1$col43<-file1$col4-file1$col3
file2$col43<-file2$col4-file2$col3
col_group11<-as.numeric(file1$col_group)==1
col_group12<-as.numeric(file1$col_group)==2
col_group13<-as.numeric(file1$col_group)==3
col_group14<-as.numeric(file1$col_group)==4
col_group21<-as.numeric(file2$col_group)==1
col_group22<-as.numeric(file2$col_group)==2
col_group23<-as.numeric(file2$col_group)==3
col_group24<-as.numeric(file2$col_group)==4
# need a wide display for this
x11(width=9)
# display the boxplots without
boxplot(file1$col43[reds1&col_group11],
file2$col43[reds2&col_group21],
file1$col43[reds1&col_group12],
file2$col43[reds2&col_group22],
file1$col43[reds1&col_group13],
file2$col43[reds2&col_group23],
file1$col43[reds1&col_group14],
file2$col43[reds2&col_group24],
main="Comparison of RED col4 and col3 differences across col2 groups",
ylab="MB",names=rep("",8),notch=TRUE,col=c("black","red"))
# add the x axis labels, staggered to avoid overlap
mtext(text=paste(c("file1","file2"),rep(levels(file1$col_group),each=2)),
side=1,line=c(0.5,1.5),at=1:8)
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.