Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does geom_text() throw coercion errors when hjust and vjust are strings?
    text
    copied!<p>I've noticed an unexpected behavior in <code>ggplot2</code>'s <code>geom_text()</code> geom. If the attributes <code>hjust</code> and <code>vjust</code> are specified as strings, R returns coercion errors, though the plots seem to come out OK. The problem came up in a <a href="https://github.com/briandk/granova/tree/dev" rel="nofollow">ggplot2-based package</a> I'm developing. For simplicity, I've created stripped-down examples that still produce the error.</p> <p>First, I tried it with <code>qplot()</code></p> <pre><code>##qplot version library(ggplot2) p &lt;- qplot(cty, hwy, label = drv, hjust = "right", geom = "text", data = mpg ) print(p) </code></pre> <p>And I got this error:</p> <pre><code>Warning message: In validDetails.text(x) : NAs introduced by coercion </code></pre> <p>Then I tried it with <code>ggplot()</code>: </p> <pre><code>##ggplot version library(ggplot2) p &lt;- ggplot( aes(x = cty, y = hwy ), data = mpg ) p &lt;- p + geom_text( aes(label = drv), hjust = "right" ) print(p) </code></pre> <p>and got an identical plot, and an identical error:</p> <pre><code>Warning message: In validDetails.text(x) : NAs introduced by coercion </code></pre> <p>I then tried setting both hjust and vjust:</p> <pre><code>library(ggplot2) p &lt;- ggplot( aes(x = cty, y = hwy ), data = mpg ) p &lt;- p + geom_text( aes(label = drv), hjust = "right", vjust = "top" ) print(p) </code></pre> <p>With both parameters set using strings, R returns two coercion errors:</p> <pre><code>Warning messages: 1: In validDetails.text(x) : NAs introduced by coercion 2: In validDetails.text(x) : NAs introduced by coercion </code></pre> <p>But, when the parameters are numbers, R returns no coercion errors:</p> <pre><code>## Using numbers instead of strings library(ggplot2) p &lt;- ggplot( aes(x = cty, y = hwy ), data = mpg ) p &lt;- p + geom_text( aes(label = drv), hjust = 0, vjust = 0, data = mpg ) print(p) </code></pre> <p>I'm not quite sure why this happens, or whether it's significant, but I didn't expect it.</p> <h3>ggplot2 documentations don't agree</h3> <p><a href="http://books.google.com/books?id=F_hwtlzPXBcC&amp;printsec=frontcover&amp;dq=ggplot2&amp;hl=en&amp;src=bmrr&amp;ei=DgJjTd3EBMGclgfs6ezhCw&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=1&amp;ved=0CCcQ6AEwAA" rel="nofollow">Hadley's book</a>(p. 196) says <code>hjust</code> and <code>vjust</code> can accept string arguments:</p> <blockquote> <p>Justification of a string (or legend) defines the location within the string that is placed at the given position. There are two values for horizontal and vertical justification. The values can be:</p> <ul> <li>A string: "left", "right", "centre", "center", "bottom", and "top". </li> <li>A number between 0 and 1, giving the position within the string (from bottom-left corner).</li> </ul> </blockquote> <p>But the man file for <code>geom_text()</code> in version 0.8.9 says hjust and vjust are numeric, though it <em>doesn't</em> say they can <em>only</em> be numeric:</p> <blockquote> <p>Aesthetics</p> <p>The following aesthetics can be used with geom_text. Aesthetics are mapped to variables in the data with the aes function: geom_text(aes(x = var))</p> <ul> <li>x: x position (required)</li> <li>y: y position (required)</li> <li>label: text label (required)</li> <li>colour: border colour</li> <li>size: size</li> <li>angle: angle</li> <li>hjust: horizontal justification, between 0 and 1</li> <li>vjust: vertical justification, between 0 and 1</li> <li>alpha: transparency</li> </ul> </blockquote>
 

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