R 当hjust和vjust是字符串时,为什么geom_text()抛出强制错误?

R 当hjust和vjust是字符串时,为什么geom_text()抛出强制错误?,r,ggplot2,R,Ggplot2,我注意到ggplot2的geom_text()geom中有一个意外行为。如果属性hjust和vjust被指定为字符串,则R返回强制错误,尽管打印结果似乎正常。这个问题出现在我正在开发的一个项目中。为了简单起见,我创建了仍然会产生错误的精简示例 首先,我用qplot() 然后我用ggplot()进行了尝试: 然后我尝试设置hjust和vjust: library(ggplot2) p <- ggplot( aes(x = cty, y

我注意到
ggplot2
geom_text()
geom中有一个意外行为。如果属性
hjust
vjust
被指定为字符串,则R返回强制错误,尽管打印结果似乎正常。这个问题出现在我正在开发的一个项目中。为了简单起见,我创建了仍然会产生错误的精简示例

首先,我用
qplot()

然后我用
ggplot()
进行了尝试:

然后我尝试设置hjust和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)
但是,当参数为数字时,R不返回强制错误:

Warning messages:
1: In validDetails.text(x) : NAs introduced by coercion
2: In validDetails.text(x) : NAs introduced by coercion
## 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)
##使用数字代替字符串
图书馆(GG2)

p
hjust
vjust
应该是数字,检查手册(
?geom_text
):

  • hjust':水平对齐,介于0和1之间
  • “vjust”:垂直对齐,介于0和1之间

因此,我不太了解hjust/vjust的代码定义或使用了什么,但使用TextMate的“在项目中查找”(在ggplot2/R/目录中)来查找hjust,我看不到任何看起来像是hjust定义或实现的行。。。只是它被列为有效aes的地方,以及它被传递的地方

这让我想去读网格

这让我想知道更多关于grid.text是如何定义的

R> 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 <- 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)
}
<environment: namespace:grid>
我明白了

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.
这是我的想法

  • just参数可以是字符串或数字
  • hjust和vjust仅为数字,可以覆盖
  • 如果您尝试为它们使用字符串,它可能会“起作用”,但会抛出警告
因此,让我们看一下grid.text演示代码,特别是draw.text函数,在该函数中,他们仅使用了字符串值,并且似乎成功地使用了字符串值:

grid.newpage()
x <- stats::runif(20)
y <- stats::runif(20)
rot <- 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 <- 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 <- unit(1:4/5, "npc")
y <- 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)
grid.newpage()

我同意手册上说的hjust和vjust应该是数字。我对这个问题进行了编辑,以反映这样一个事实:哈德利的书似乎非常清楚地指出,它们也可以是字符串。我不清楚哪个文档取代了哪个。代码取代了所有文档;文件编制(续):非常特别的是,196页的书中并没有说hust和vjust可以有这些值。。。它只是(粗略地)说“理由”可以是。。。查看下面我的完整“答案”。在浏览
textGrob
的文档时,我想我真正的问题是:为什么ggplot2的
theme_text()
不能接受并传递
just
参数,这样用户就可以指定对齐向量,比如
just=c(“底部”,“中心”)
?它可以。。。事实并非如此。也许这意味着对ggplot2的请求?
## 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)
R> 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 <- 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)
}
<environment: namespace:grid>
R> 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 <- unit(x, default.units)
    if (!is.unit(y)) 
        y <- 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")
}
<environment: namespace:grid>
R> grob
function (..., name = NULL, gp = NULL, vp = NULL, cl = NULL) 
{
    g <- list(..., name = name, gp = gp, vp = vp)
    if (!is.null(cl) && !is.character(cl)) 
        stop("Invalid 'grob' class")
    class(g) <- c(cl, "grob", "gDesc")
    validGrob(g)
}
<environment: namespace:grid>
R> ?textGrob
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.
grid.newpage()
x <- stats::runif(20)
y <- stats::runif(20)
rot <- 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 <- 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 <- unit(1:4/5, "npc")
y <- 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)
grid.newpage()
x <- stats::runif(20)
y <- stats::runif(20)
rot <- 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 <- 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 <- unit(1:4/5, "npc")
y <- 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)