Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting positions (x and y coordinates) of a drawn grid grob #18

Closed
Anirban166 opened this issue Aug 8, 2021 · 4 comments
Closed

Comments

@Anirban166
Copy link

Hi @pmur002
I was wondering whether we could acquire the (x, y) positions of a grid grob after it has been drawn (i.e. the x and y coordinates in pairs resembling the points which altogether construct that particular grob) - if that's possible, what would be the approach?

What I'm trying to achieve is to obtain

  • the y-position/coordinate of the topmost part of a drawn polygon grob (any one point along the topmost edge would suffice, since while the x-coordinate will vary, the y-coordinate will be constant, being the maximum value for the points on the top side of the polygon - I would like this ymax value)
  • and the topmost line of the plot panel (creating the top-level boundary for the plot).

Link for reference: tdhock/directlabels#41 (comment)

@pmur002
Copy link
Owner

pmur002 commented Aug 8, 2021

Are you looking for grid::grobX() and grid::grobY() ?

@Anirban166
Copy link
Author

I tried using them but they aren't returning anything useful: (just returns the name of 'rotationgrobaxis', where rotation is the angle equivalent of the direction-based theta value provided, and axis is either x or y depending upon use of grobX or grobY)

> df <- data.frame(i=1:10, label="more
+ than
+ one
+ line
+ really
+ a
+ lot")
> library(ggplot2)
> gg <- ggplot(df, aes(i, i, color=label)) + geom_line()
> library(directlabels)
> direct.label(gg, "right.polygons")
> library(grid)
> grid.force()
> grid.ls(fullNames = TRUE)
forcedgrob[layout]
  forcedgrob[background.1-9-12-1]
  forcedgrob[panel.7-5-7-5]
    gTree[grill.gTree.14]
      rect[panel.background..rect.5]
      polyline[panel.grid.minor.y..polyline.7]
      polyline[panel.grid.minor.x..polyline.9]
      polyline[panel.grid.major.y..polyline.11]
      polyline[panel.grid.major.x..polyline.13]
    zeroGrob[NULL]
    polyline[GRID.polyline.1]
    dlgrob[GRID.dlgrob.1.right.polygons]
    zeroGrob[NULL]
    zeroGrob[panel.border..zeroGrob.2]
  forcedgrob[spacer.8-6-8-6]
  forcedgrob[spacer.8-4-8-4]
  forcedgrob[spacer.6-6-6-6]
  forcedgrob[spacer.6-4-6-4]
  forcedgrob[axis-t.6-5-6-5]
  forcedgrob[axis-l.7-4-7-4]
    zeroGrob[NULL]
    forcedgrob[axis]
      forcedgrob[axis.1-1-1-1]
        text[GRID.text.22]
      forcedgrob[axis.1-2-1-2]
  forcedgrob[axis-r.7-6-7-6]
  forcedgrob[axis-b.8-5-8-5]
    zeroGrob[NULL]
    forcedgrob[axis]
      forcedgrob[axis.1-1-1-1]
      forcedgrob[axis.2-1-2-1]
        text[GRID.text.17]
  forcedgrob[xlab-t.5-5-5-5]
  forcedgrob[xlab-b.9-5-9-5]
    text[GRID.text.26]
  forcedgrob[ylab-l.7-3-7-3]
    text[GRID.text.30]
  forcedgrob[ylab-r.7-7-7-7]
  forcedgrob[subtitle.4-5-4-5]
  forcedgrob[title.3-5-3-5]
  forcedgrob[caption.10-5-10-5]
  forcedgrob[tag.2-2-2-2]
> y1 <- grid::grobY("GRID.dlgrob.1.right.polygons","north")
> y1
[1] 90groby 
> class(y1)
[1] "unit"    "unit_v2"
> y2 <- grid::grobY("panel.grid.major.x..polyline.13","north")
> y2
[1] 90groby 

image

Also, as you can see from the above they're returning the exact same thing for two different grobs. Am I doing something wrong?

@pmur002
Copy link
Owner

pmur002 commented Aug 9, 2021

It depends on your definition of "useful" :) y1 and y2 are "unit" objects, so they can be used as locations or dimensions for other 'grid' drawing, e.g., ...

grid.points(unit(.5, "npc"), y1)

... or they can be converted to other coordinate systems, e.g., ...

convertY(y1, "in")

... with the warning that the value of the y1 unit will make most sense in the context of the grob it is referring to, i.e., "GRID.dlgrob.1.right.polygons" (and is likely to be completely nonsensical in other contexts).

@Anirban166
Copy link
Author

Ah gotcha. I was too quick to assume that they were not useful, but it seems like I can make some use out of them now :)
will update you when I do - might be a bit late though, sorry! (closing this for now)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants