Skip to content
Prev 389300 / 398506 Next

Trying to Learn Details of Grid Graphics, Help Page Errors

Hi Paul,

Thanks very much for the pointer!

With that and a bit more investigation, I was able to make the following
functions that seem to work in initial testing.

Thanks,

Bill

library(grid)

# The no_clipping function indicates if a shape is clipped in any dimension
# relative to the entire device.  It should work for any rectangular shaped
# grob.
no_clipping <- function(x) {
  # Check the cardinal directions and corners to allow for any rotation
  angles <- seq(0, 3.5, by=0.5)*90
  if ("rot" %in% names(x)) {
    # At least text grobs have a "rot" attribute
    angles <- angles + (x$rot*pi/180)
  }
  edges_raw <-
    grid::deviceLoc(
      x=grid::grobX(x, theta=angles),
      y=grid::grobY(x, theta=angles)
    )
  edges <-
    list(
      x=grid::convertX(edges_raw$x, unitTo="cm", valueOnly=TRUE),
      y=grid::convertY(edges_raw$y, unitTo="cm", valueOnly=TRUE)
    )
  d_size <- dev.size(units="cm")
  c(
    bottom=0 <= min(edges$y),
    top=max(edges$y) <= d_size[2],
    left=0 <= min(edges$x),
    right=max(edges$x) <= d_size[1]
  )
}

makeContent.text <- function(x) {
  if (!all(no_clipping(x))) {
    warning(
      "Graphics text is outside of the device: ",
      paste0("'", x$label, "'", collapse=", ")
    )
  }
  x
}

Code I used for testing:

graphics.off()
foo <- grid.text(label="foo bar", x=0.95, y=0.9)
no_clipping(foo)
graphics.off()
foo <- grid.text(label="foo bar", x=0.9, y=0.9)
no_clipping(foo)
graphics.off()
foo <- grid.text(label="foo bar", x=0.95, y=0.9, rot=45)
no_clipping(foo)
foo <- grid.text(label="foo bar", x=0.96, y=0.9, rot=45)
no_clipping(foo)
graphics.off()
foo <- grid.text(label="foo bar", x=0.96, y=0.9, rot=80)
no_clipping(foo)
graphics.off()
foo <- grid.rect(x=0.8, y=0.8, width=0.15, height=0.15, default.units="npc",
just=c(0, 0))
no_clipping(foo)
graphics.off()
vp <- viewport(angle=45)
foo <- grid.rect(x=0.8, y=0.8, width=0.15, height=0.15, default.units="npc",
just=c(0, 0), vp=vp)
no_clipping(foo)

# It also works with ggplot2
library(ggplot2)
data_with_long_names <-
  data.frame(
    A=c(paste0(rep(LETTERS, 3), collapse=""), "ab", paste0(rep(letters, 3),
collapse="")),
    B=1
  )

# Feature request: This gives a warning
ggplot(data_with_long_names, aes(x=A, y=B)) +
  geom_point()

ggplot(data_with_long_names, aes(x=A, y=B)) +
  geom_point() +
  theme(
    axis.text.x=element_text(angle=45, hjust=1, vjust=1, size=rel(0.5))
  )

-----Original Message-----
From: Paul Murrell <paul at stat.auckland.ac.nz> 
Sent: Wednesday, September 22, 2021 4:50 PM
To: bill at denney.ws; r-help at r-project.org
Subject: Re: [R] Trying to Learn Details of Grid Graphics, Help Page Errors

Hi

The first place you should probably start (given where you are right
now) is this R Journal article ...

https://journal.r-project.org/archive/2013/RJ-2013-035/RJ-2013-035.pdf

In brief, the drawDetails() function has been (almost entirely) superceded
by the makeContent() function.

The best overall reference is probably the "R Graphics" book (3rd edition,
chapts 6, 7, & 8).  Unfortunately, because the first edition came out in
2005, that is an Olde Worlde pay-for-a-print-version book (and probably will
be until something stupid like 50 years after I have gone).  Or maybe you
are lucky and work for a first-world university that has purchased access to
an electronic version.

Thanks for pointing out the problems with the drawDetails() help page; I
will need to fix that.

Paul
On 9/23/2021 2:21 AM, bill at denney.ws wrote:
"text" class.
and it doesn't exist.
--
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/