[R] xtable without a loop alongside a ggplot

Justin Haynes jtor14 at gmail.com
Wed May 4 17:57:53 CEST 2011


I would like to create a table of my points and identify which
'quadrant' of a plot they are in with the 'origin' at the means.  the
kicker is i would like to display it right next to or below a ggplot
of the data.  Maybe xtable isnt the right thing to use, but its the
only thing i can think of.  Any help is appreciated!

set.seed(144)
x=rnorm(100,mean=5,sd=1)
test<-data.frame(x=x,y=x^2)
test$right<-sapply(test$x,function(x) {mean.x<-mean(test$x);any(x>mean.x)})
test$up<-sapply(test$y,function(y) {mean.y<-mean(test$y);any(y>mean.y)})

for(i in 1:length(test$x)){
  if(test$right[i]==TRUE & test$up[i]==TRUE)
print(paste(rownames(test[i,]),'is in the upper right quadrant'))
  if(test$right[i]==FALSE & test$up[i]==TRUE)
print(paste(rownames(test[i,]),'is in the upper left quadrant'))
  if(test$right[i]==TRUE & test$up[i]==FALSE)
print(paste(rownames(test[i,]),'is in the lower right quadrant'))
  if(test$right[i]==FALSE & test$up[i]==FALSE)
print(paste(rownames(test[i,]),'is in the lower left quadrant'))
}

I know theres a better way then using a for loop!  and I haven't the
foggiest how to use xtable.  as i said, the ultimate goal is to create
a plot with a table along side it showing outliers and where they
appear using the inout function from the splancs package and a
confidence ellipse from the ellipse package.

Thank you for your help as usual!

Justin



More information about the R-help mailing list