Christoph Lehmann
2004-Sep-06 08:52 UTC
[R] locator() in a multi-figure setting using mfrow()
Hi
based on some code from Thomas Petzoldt (see below), I have a question,
about how to use locator() in a mfrow() multi-figure setting. I am sure
this should be a kind of problem, which other people face too?
we have 8 matrices each 10x10 fields, plotted as mfrow = c(2,4).
how can I get, using locator(), the correct index of a field of one of
the 8 matrices? means, I should get 3 values: the matrix I am in (a
value between 1..8), and the corresponding x and y coordinates (each in
1..10)
many, thanks for your kind help.
---
opar <- par(mfrow = c(2,4))
slices <- 8
m <- matrix(runif(100),10,10)
my.list <- list()
for (slice in 1:slices) {
my.list[[slice]] <- m
}
for (slice in 1:slices) {
x <- 1*(1:25)
y <- 1*(1:25)
z <- my.list[[slice]]
image(list(x = 0:9, y = 0:9, z = z))
}
par(opar) #restore device parameters
p <- locator(1)
c(round(p$x), round(p$y))
---
how can I get the "correct" location in the sense of a
3d info: (a) which slice (p$slice) (b) p$x (c) p$y
so that it could be used in the sense of:
my.list[[p$slice]][round(p$x), round(p$y)]
christoph
Christoph Lehmann
2004-Sep-06 09:40 UTC
[R] locator() in a multi-figure setting using mfrow()
I know, that I can use par(mfg = c(i,u)) to get the correct x,y coordinates of one of the 8 matrices/subimages, but how can I get the i and the j, means how can I know in which of the 8 images I am clicking in? thanks Christoph Christoph Lehmann wrote:> Hi > > based on some code from Thomas Petzoldt (see below), I have a question, > about how to use locator() in a mfrow() multi-figure setting. I am sure > this should be a kind of problem, which other people face too? > > we have 8 matrices each 10x10 fields, plotted as mfrow = c(2,4). > > how can I get, using locator(), the correct index of a field of one of > the 8 matrices? means, I should get 3 values: the matrix I am in (a > value between 1..8), and the corresponding x and y coordinates (each in > 1..10) > > many, thanks for your kind help. > > --- > opar <- par(mfrow = c(2,4)) > slices <- 8 > m <- matrix(runif(100),10,10) > my.list <- list() > for (slice in 1:slices) { > my.list[[slice]] <- m > } > > for (slice in 1:slices) { > x <- 1*(1:25) > y <- 1*(1:25) > z <- my.list[[slice]] > image(list(x = 0:9, y = 0:9, z = z)) > } > par(opar) #restore device parameters > > > p <- locator(1) > c(round(p$x), round(p$y)) > --- > > how can I get the "correct" location in the sense of a > 3d info: (a) which slice (p$slice) (b) p$x (c) p$y > > so that it could be used in the sense of: > > my.list[[p$slice]][round(p$x), round(p$y)] > > > christoph > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Christoph Lehmann
2004-Sep-09 13:07 UTC
[R] locator() in a multi-figure setting using mfrow(): SOLVED
thanks to some great hints by Paul Murrel I could solve it: here we are
with one solution (code needs to be cleaned and simplified, but maybe
one can understand it)
#######
## create a multifigure setting
nr <- 4
nc <- 2
opar <- par(mfrow = c(nr, nc))
slices <- 8
m <- matrix(runif(100),10,10)
my.list <- list()
for (slice in 1:slices) {
my.list[[slice]] <- m
}
for (slice in 1:slices) {
x <- 1*(1:25)
y <- 1*(1:25)
z <- my.list[[slice]]
image(list(x = 0:9, y = 0:9, z = z))
}
########
my.get.coord <- function() {
par(mfg = c(1,1)) #locator() shall be relative to the first plot out
# of the eight plots totally
my.loc <-locator(1) #location, not in inches
my.plot.region <- par("usr") #extremes of plotting region
#(in plot units, not inches)
my.plot.region.x <- my.plot.region[2] - my.plot.region[1]
my.plot.region.y <- my.plot.region[4] - my.plot.region[3]
my.loc.inch.x <- (my.loc$x + 0.5)/my.plot.region.x *
(par("pin")[1])
#par("pin") #current plot dimension in inches
#relative to the plotting-region bottom left corner, not the axis c(0,0)
point
my.loc.inch.y <- (my.loc$y + 0.5)/my.plot.region.y *
(par("pin")[2])
## search the plot we are in with locator(1)
my.plot.inch.x <- par("pin")[1] + par("mai")[2] +
par("mai")[4] #plot.x
+ left & right margin
par("fin")[1]
my.plot.inch.y <- par("pin")[2] + par("mai")[1] +
par("mai")[3] #plot.y
+ bottom & top margin
par("fin")[2]
pos.rel.x <- (my.loc.inch.x / par("fin")[1] - floor(my.loc.inch.x /
par("fin")[1])) *
par("fin")[1] / par("pin")[1] *
(par("usr")[2] - par("usr")[1]) - 0.5
#inches from left bottom corner in target plot region (c(0,0)
# is plot-region bottom-left corner, not the axis c(0,0) point
pos.rel.y <- (my.loc.inch.y / par("fin")[2] - floor(my.loc.inch.y /
par("fin")[2])) *
par("fin")[2] / par("pin")[2] *
(par("usr")[4] - par("usr")[3]) - 0.5
#inches from left bottom corner in target plot
fig.coord.x <- ceiling(my.loc.inch.x / par("fin")[1])
fig.coord.y <- 1 +(-1) *ceiling(my.loc.inch.y / par("fin")[2])
# cat("figure-coord x: ", fig.coord.x,"\n")
# cat("figure-coord y: ", fig.coord.y,"\n")
cat("we are in figure: ", fig.coord.y * nc + fig.coord.x,
"\n")
cat("coordinates of the identified point x: ",
round(pos.rel.x),"\n")
cat("coordinates of the identified point y: ",
round(pos.rel.y),"\n")
}
########
my.get.coord()
Christoph