Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So, I don't know much about WHAT CODE defines or consumes hjust/vjust, but using TextMate's "Find in project" (in the ggplot2/R/ directory) for hjust, I don't see any lines that look like they are the definition of or the implementation of hjust... just places where it's listed as a valid aes and where it gets passed along.</p> <p>That makes me want to go read grid...</p> <p><a href="http://stat.ethz.ch/R-manual/R-patched/library/grid/html/grid.text.html" rel="nofollow">http://stat.ethz.ch/R-manual/R-patched/library/grid/html/grid.text.html</a></p> <p>which leads me to want to know more about how grid.text is defined</p> <pre><code>R&gt; grid.text function (label, x = unit(0.5, "npc"), y = unit(0.5, "npc"), just = "centre", hjust = NULL, vjust = NULL, rot = 0, check.overlap = FALSE, default.units = "npc", name = NULL, gp = gpar(), draw = TRUE, vp = NULL) { tg &lt;- textGrob(label = label, x = x, y = y, just = just, hjust = hjust, vjust = vjust, rot = rot, check.overlap = check.overlap, default.units = default.units, name = name, gp = gp, vp = vp) if (draw) grid.draw(tg) invisible(tg) } &lt;environment: namespace:grid&gt; </code></pre> <p>so, it's a textGrob, and just, hjust, and vjust are simply being passed into it... off to textGrob</p> <pre><code>R&gt; textGrob function (label, x = unit(0.5, "npc"), y = unit(0.5, "npc"), just = "centre", hjust = NULL, vjust = NULL, rot = 0, check.overlap = FALSE, default.units = "npc", name = NULL, gp = gpar(), vp = NULL) { if (!is.unit(x)) x &lt;- unit(x, default.units) if (!is.unit(y)) y &lt;- unit(y, default.units) grob(label = label, x = x, y = y, just = just, hjust = hjust, vjust = vjust, rot = rot, check.overlap = check.overlap, name = name, gp = gp, vp = vp, cl = "text") } &lt;environment: namespace:grid&gt; </code></pre> <p>so, it's a grob........... off to grob......</p> <pre><code>R&gt; grob function (..., name = NULL, gp = NULL, vp = NULL, cl = NULL) { g &lt;- list(..., name = name, gp = gp, vp = vp) if (!is.null(cl) &amp;&amp; !is.character(cl)) stop("Invalid 'grob' class") class(g) &lt;- c(cl, "grob", "gDesc") validGrob(g) } &lt;environment: namespace:grid&gt; </code></pre> <p>Nothing too helpful there, so I Google</p> <p>R grid hjust vjust</p> <p>and after overriding Google's autocorrect of my search, I find</p> <p><a href="http://rwiki.sciviews.org/doku.php?id=tips:graphics-grid:hvjust" rel="nofollow">http://rwiki.sciviews.org/doku.php?id=tips:graphics-grid:hvjust</a></p> <p>Looking back at Hadley's book, I notice that the p.196 reference doesn't actually MENTION hjust or vjust... simply justification.</p> <p>Reading the documentation for</p> <pre><code>R&gt; ?textGrob </code></pre> <p>I see that</p> <pre><code>just The justification of the text relative to its (x, y) location. If there are two values, the first value specifies horizontal justification and the second value specifies vertical justification. Possible string values are: "left", "right", "centre", "center", "bottom", and "top". For numeric values, 0 means left alignment and 1 means right alignment. hjust A numeric vector specifying horizontal justification. If specified, overrides the just setting. vjust A numeric vector specifying vertical justification. If specified, overrides the just setting. </code></pre> <p>So, here's my thinking.</p> <ul> <li>the just parameter can be string or numeric</li> <li>hjust and vjust are numeric only and can override just</li> <li>if you try to use strings for them, it may "work", but throw warnings</li> </ul> <p>So, lets look at the grid.text demo code and in particular the draw.text function where they use just and seem to do so successfully with string values:</p> <pre><code>grid.newpage() x &lt;- stats::runif(20) y &lt;- stats::runif(20) rot &lt;- stats::runif(20, 0, 360) grid.text("SOMETHING NICE AND BIG", x=x, y=y, rot=rot, gp=gpar(fontsize=20, col="grey")) grid.text("SOMETHING NICE AND BIG", x=x, y=y, rot=rot, gp=gpar(fontsize=20), check=TRUE) grid.newpage() draw.text &lt;- function(just, i, j) { grid.text("ABCD", x=x[j], y=y[i], just=just) grid.text(deparse(substitute(just)), x=x[j], y=y[i] + unit(2, "lines"), gp=gpar(col="grey", fontsize=8)) } x &lt;- unit(1:4/5, "npc") y &lt;- unit(1:4/5, "npc") grid.grill(h=y, v=x, gp=gpar(col="grey")) draw.text(c("bottom"), 1, 1) draw.text(c("left", "bottom"), 2, 1) draw.text(c("right", "bottom"), 3, 1) draw.text(c("centre", "bottom"), 4, 1) draw.text(c("centre"), 1, 2) draw.text(c("left", "centre"), 2, 2) draw.text(c("right", "centre"), 3, 2) draw.text(c("centre", "centre"), 4, 2) draw.text(c("top"), 1, 3) draw.text(c("left", "top"), 2, 3) draw.text(c("right", "top"), 3, 3) draw.text(c("centre", "top"), 4, 3) draw.text(c(), 1, 4) draw.text(c("left"), 2, 4) draw.text(c("right"), 3, 4) draw.text(c("centre"), 4, 4) </code></pre> <p>Now notice the difference if I change draw.text to use hjust and vjust <strong>AS STRINGS</strong></p> <pre><code>grid.newpage() x &lt;- stats::runif(20) y &lt;- stats::runif(20) rot &lt;- stats::runif(20, 0, 360) grid.text("SOMETHING NICE AND BIG", x=x, y=y, rot=rot, gp=gpar(fontsize=20, col="grey")) grid.text("SOMETHING NICE AND BIG", x=x, y=y, rot=rot, gp=gpar(fontsize=20), check=TRUE) grid.newpage() draw.text &lt;- function(just, i, j) { grid.text("ABCD", x=x[j], y=y[i], hjust=just[1], vjust=just[2]) grid.text(deparse(substitute(just)), x=x[j], y=y[i] + unit(2, "lines"), gp=gpar(col="grey", fontsize=8)) } x &lt;- unit(1:4/5, "npc") y &lt;- unit(1:4/5, "npc") grid.grill(h=y, v=x, gp=gpar(col="grey")) draw.text(c("bottom"), 1, 1) draw.text(c("left", "bottom"), 2, 1) draw.text(c("right", "bottom"), 3, 1) draw.text(c("centre", "bottom"), 4, 1) draw.text(c("centre"), 1, 2) draw.text(c("left", "centre"), 2, 2) draw.text(c("right", "centre"), 3, 2) draw.text(c("centre", "centre"), 4, 2) draw.text(c("top"), 1, 3) draw.text(c("left", "top"), 2, 3) draw.text(c("right", "top"), 3, 3) draw.text(c("centre", "top"), 4, 3) draw.text(c(), 1, 4) draw.text(c("left"), 2, 4) draw.text(c("right"), 3, 4) draw.text(c("centre"), 4, 4) </code></pre> <p>Long story-short: I think when you use hjust or vjust as a string, you're violating the documentation (it's value should be numeric 0 &lt;= x &lt;= 1), and that if you want to use strings, you have to use the just parameter....</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload