[R] par("uin") ? / brace function
Michael Friendly
friendly at yorku.ca
Tue Jul 20 19:17:51 CEST 2010
Thanks, Peter
This more or less corresponds to the solution I came to, modulo xyinch()
and atan2(x,y) = atan(x/y).
It is mostly intended as a replacement for arrows() in statistical
diagrams/plots where you want to
show a given range in a plot precisely without additional boundary lines.
It works reasonably well for horizontal and vertical braces, but in
other cases depends on the
aspect ratio of the plot. The internal calculation of scale is a kludge
to try to adjust for that.
-Michael
Peter Ehlers wrote:
> The following works for me:
>
> brace <- function (x1 = 0, y1 = 0, x2 = 0, y2 = 1, right = TRUE, rad =
> 0.2)
> {
> uin<-xyinch() # thanks to Paul's comment; I had forgotten xinch()
> ux <- uin[1]
> uy <- uin[2]
> dx <- x2 - x1
> dy <- y2 - y1
> alpha <- atan2(ux * dx, uy * dy) # in R, we need atan2()
> scale <- sqrt((ux * dx)^2 + (uy * dy)^2)
> if (scale > 5 * rad)
> rad <- rad/scale
> qcirc <- cbind(cos((0:10) * pi/20), sin((0:10) * pi/20))
> qcircr <- cbind(cos((10:0) * pi/20), sin((10:0) * pi/20))
> rot <- function(theta) t(cbind(c(cos(theta), sin(theta)),
> c(-sin(theta), cos(theta))))
> seg1 <- t(t(rad * qcirc %*% rot(-pi/2)) + c(0, rad))
> seg4 <- t(t(rad * qcirc) + c(0, 1 - rad))
> seg3 <- t(t((rad * qcircr) %*% rot(pi)) + c(2 * rad, 0.5 +
> rad))
> seg2 <- t(t((rad * qcircr) %*% rot(pi/2)) + c(2 * rad, 0.5 -
> rad))
> bra <- rbind(seg1, seg2, seg3, seg4)
> if (!right)
> bra <- bra %*% diag(c(-1, 1))
> bra <- scale * bra %*% rot(-alpha)
> bra <- bra %*% diag(c(1/ux, 1/uy))
> bra <- t(t(bra) + c(x1, y1))
> bra
> }
>
> plot(...)
> lines(brace(...))
>
> For non-vertical/horizontal braces, the arcs at the ends
> don't seem quite right to me. Also, it would seem useful
> to be able to just define the cusp and the width of a
> brace. I might have a look at that over the weekend.
>
> -Peter Ehlers
>
>
> On 2010-07-19 9:20, Michael Friendly wrote:
>> I inherited a function written either for an older version of R or SPlus
>> to draw a brace, "{", in a graph. It uses par("uin") to determine the
>> scaling of the
>> quarter circles that make up segments of the brace, but that setting
>> doesn't
>> exist in current R.
>> I'm guessing that, in the function below, ux, uy can be defined from
>> par("usr") and par("pin"), but maybe someone remembers what par("uin")
>> was supposed to refer to.
>>
>> brace <- function (x1 = 0, y1 = 0, x2 = 0, y2 = 1, right = TRUE, rad
>> = 0.2)
>> {
>>
> [....]
--
Michael Friendly Email: friendly AT yorku DOT ca
Professor, Psychology Dept.
York University Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street Web: http://www.datavis.ca
Toronto, ONT M3J 1P3 CANADA
More information about the R-help
mailing list