Manipulating subsections of an array
I am using R to plot trying to conditionally change parts of an array based on the columns of the array.
I have worked out the following steps:
x<-array(1,dim=c(4,4,3))
r<-x[,,1]
g<-x[,,2]
b<-x[,,3]
r1<-apply(r[,2:3],1:2,function(f){return(0)})
g1<-apply(g[,2:3],1:2,function(f){return(0)})
b1<-apply(b[,2:3],1:2,function(f){return(0)})
r3<-cbind(r[,1],r1,r[,4])
g3<-cbind(g[,1],g1,g[,4])
b3<-cbind(b[,1],b1,b[,4])
# Pass to pixmapRGB
This works, but as I am new to R, I was wondering if there was a more efficient way to manipulate parts of an array.
For example, does apply know which element it is working on?
The bigger picture is that I want to graph a time-series scatter plot over many pages.
I would like to have a thumbnail in the corner of the page that is a graph of the whole series. I would like to color a portion of that thumbnail a different color to indicate what range the current page is examining.
There is alot of data, so it is not feasible to redraw a new plot for the thumbnail on every page.
What I have done 开发者_如何学Cis to first write the thumbnail plot out to a tiff file. Then I read the tiff file back in, used getChannels from pixmap to break the picture into arrays, and used the above code to change some of the pixels based on column. Finally I then print the image to a viewport using pixmapRGB/pixmapGrob/grid.draw
It seems like alot of steps. I would be grateful for any pointers that would help me make this more efficient.
Maybe I don't understand your question, but if what you're trying to do is just "change some pixels based on column," why don't you just use the basic array indexing to do that?
This will do the same thing you have posted:
x<-array(1,dim=c(4,4,3))
r<-x[,,1]
g<-x[,,2]
b<-x[,,3]
r[,2:3]=0
g[,2:3]=0
b[,2:3]=0
Is that helpful?
Perhaps more of a comment than an answer, but when I try to plot over a number of pages I usually go left to right, breaking up the plots into quantiles and setting appropriate xlim
(or ylim
)
x <- rnorm(100000)
y <- rnorm(100000)
df <- data.frame(x,y)
seq1 <- quantile(df$x, probs = seq(0,1,0.1))
seq2 <- quantile(df$x, probs = seq(0,1,0.1))
for(x in 1:(length(seq1)-1)) {
plot(df, xlim=c(seq1[x],seq1[x+1]))
}
No idea how to overlay a thumbnail onto the graphs although I think you could do this with one of the rimage
functions if you saved the thumbnail.
You could avoid having to read and paste a tiff thumbnail by actually replotting the whole chart at reduced scale. check out par(fig)
, and then do something like
Rgames: plot(1:2,1:2)
Rgames: par(mar=c(.1,6,.1,.1),new=T,fig=c(0,.25,.5,.75))
Rgames: plot(1:2,1:2)
Rgames: polygon(c(1,2,2,1),c(1,1,2,2),col='red')
("Rgames:" is my prompt) You'll have to play a bit with the margin values, but this will get your "mini-graph" set up.
精彩评论