I've noticed an unexpected behavior in ggplot2
's geom_text()
geom. If the attributes hjust
and vjust
are specified as strings, R returns coercion errors, though the plots seem to come out OK. The problem came up in a ggplot2-based package I'm developing. For simplicity, I've created stripped-down examples that still produce the error.
First, I tried it with qplot()
##qplot version
library(ggplot2)
p <- qplot(cty, hwy,
label = drv,
hjust = "right",
geom = "text",
data = mpg
)
print(p)
And I got this error:
Warning message:
In validDetails.text(x) : NAs introduced by coercion
Then I tried it with ggplot()
:
##ggplot version
library(ggplot2)
p <- ggplot(
aes(x = cty,
y = hwy
), data = mpg
)
p <- p + geom_text(
aes(label = drv),
hjust = "right"
)
print(p)
and got an identical plot, and an identical error:
Warning message:
In validDetails.text(x) : NAs introduced by coercion
I then tried setting both hjust and vjust:
library(ggplot2)
p <- ggplot(
aes(x = cty,
y = hwy
), data = mpg
)
p <- p + geom_text(
aes(label = drv),
hjust = "right",
vjust = "top"
)
print(p)
With both parameters set using strings, R returns two coercion errors:
Warning messages:
1: In validDetails.text(x) : NAs introduced by coercion
2: In validDetails.text(x) : NAs introduced by coercion
But, when the parameters are numbers, R returns no coercion errors:
## Using numbers instead of strings
library(ggplot2)
p <- ggplot(
aes(x = cty,
y = hwy
), data = mpg
)
p <- p + geom_text(
aes(label = drv),
hjust = 0,
vjust = 0,
data = mpg
)
print(p)
I'm not quite sure why this happens, or whether it's significant, but I didn't expect it.
ggplot2 documentations don't agree
Hadley's book(p. 196) says hjust
and vjust
can accept string arguments:
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:
- A string: "left", "right", "centre", "center", "bottom", and "top".
- A number between 0 and 1, giving the position within the string (from bottom-left corner).
But the man file for geom_text()
in version 0.8.9 says hjust and vjust are numeric, though it doesn't say they can
only be numeric:
Aesthetics
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))
- x: x position (required)
- y: y position (required)
- label: text label (required)
- colour: border colour
- size: size
- angle: angle
- hjust: horizontal justification, between 0 and 1
- vjust: vertical justification, between 0 and 1
- alpha: transparency