So the setup is this:
dat <- data.frame(a = c("longnameonthelefthandside"), b = c(sample(10)))
p <- ggplot(dat, aes(b,a)) + geom_point() + xlab("label")
I can adjust the x axis label by passing a unit
object to hjust
:
p + opts(axis.title.x=theme_text(size=12,hjust=unit(0.3,"npc")))
But there's something about adding unit objects (possible, according to ?unit) that I'm not grasping:
u1 <- unit(0.5,"npc")
u2 <- unit(0.25,"npc")
p + opts(axis.title.x=theme_text(size=12,hjust=u1+u2))
yields the following error:
Error in grid.C开发者_开发问答all("L_textBounds", as.graphicsAnnot(x$label), x$x, x$y, :
Polygon edge not found
In addition: Warning message:
In validDetails.text(x) : NAs introduced by coercion
As further context, I'm trying to piece together a somewhat cryptic workaround via Baptiste here
Probably unit for hjust is implicitly converted to numeric on the way to drawing. So, try this:
grid.lines(c(0.5, 0.5))
grid.text("orzorzorz", y=0.4, hjust=unit(0.25, "npc"))
grid.text("orzorzorz", y=0.5, hjust=unit(0.25, "mm"))
grid.text("orzorzorz", y=0.6, hjust=unit(0.25, "cm"))
grid.text("orzorzorz", y=0.7, hjust=0.25)
and why the u1+u2
induces the error is that:
> c(u1+u2)
$fname
[1] "+"
$arg1
[1] 0.5npc
$arg2
[1] 0.25npc
definitely this cannot be converted into numeric.
so, convertUnit is a workaround, but simply, hjust = c(u1)+c(u2)
is sufficient.
Baptiste's code includes a call to convertUnit
. Once you put this back into your code, it produces a plot:
p + opts(
axis.title.x=theme_text(size=12,hjust=convertUnit(u1+u2, "npc", value=TRUE))
)
精彩评论