0

Im developing a function that uses ggplot2 to create multiple graphs that compares two set of points representing shapes.

ref = matrix(c(1,3,1,3,2,2,4,4),nrow=4, ncol=2) 
ref<-data.frame(x=ref[,1], y=ref[,2])

shapes<-list()
shapes[[1]]<-matrix(c(1.5,2.9,1.4,3.1,2.2,2.3,4.5,3.5),nrow=4, ncol=2) 
shapes[[2]]<-matrix(c(0.5,3.9,1.1,3.1,1.8,2,4.5,3.5),nrow=4, ncol=2) 
shapes[[3]]<-matrix(c(1.8,3.2,1,3.5,2.2,2.3,4.5,3.5),nrow=4, ncol=2) 

newplots<-list()
for(i in 1:length(shapes)){
  target<-shapes[[i]]
  vari<-data.frame(x=target[,1], y=target[,2])
  newplots[[i]]<-ggplot(ref,aes(x = x,y = y)) + geom_point(size=2,color="red")+coord_fixed()+
    geom_point(data=vari,aes(x=x,y=y),color="black",size=3)
}

newplots[[1]]
newplots[[2]]
newplots[[3]]
newplots[[4]]

The problem is that the reference points seem to "move" from plot to plot when they suppose to stay in the same place.

  • 1
    Please make your problem [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). – Jaap Feb 18 '16 at 16:34

1 Answers1

0

When you say 'move' are you referring to the graph scaling changing? I ran the code and that's the only change I noticed. If so- you can simply add something like: + xlim(1, 4) + ylim(2,5) in the the loop after the ggplot.

cgage1
  • 579
  • 5
  • 15