首页 文章

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

提问于
浏览
3

我注意到 ggplot2geom_text() geom中出现了意想不到的行为 . 如果属性 hjustvjust 被指定为字符串,则R返回强制错误,尽管图表似乎正常 . 问题出现在ggplot2-based package我创建的精简示例仍然产生错误 .

首先,我用 qplot() 试了一下

##qplot version
library(ggplot2)
p <- qplot(cty, hwy, 
           label = drv, 
           hjust = "right", 
           geom  = "text", 
           data  = mpg
)

print(p)

我收到了这个错误:

Warning message:
In validDetails.text(x) : NAs introduced by coercion

然后我用 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)

并获得了相同的情节,并且出现了相同的错误:

Warning message:
In validDetails.text(x) : NAs introduced by coercion

然后我尝试设置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

但是,当参数是数字时,R不会返回强制错误:

## 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)

我不太清楚为什么会这样,或者它是否重要,但我没想到 .

ggplot2文件不同意

Hadley's book(p.196)说 hjustvjust 可以接受字符串参数:

字符串(或图例)的对齐定义字符串中放置在给定位置的位置 . 水平和垂直对齐有两个值 . 值可以是:字符串:“left”,“right”,“center”,“center”,“bottom”和“top” . 一个介于0和1之间的数字,给出字符串中的位置(从左下角开始) .

但是版本0.8.9中 geom_text() 的man文件说hjust和vjust是数字的,虽然它没有说它们只能是数字:

美学以下美学可与geom_text一起使用 . 使用aes函数将美学映射到数据中的变量:geom_text(aes(x = var))x:x位置(必需)y:y位置(必需)标签:文本标签(必需)颜色:边框颜色大小:大小angle:angle hjust:水平对齐,介于0和1之间vjust:垂直对齐,介于0和1 alpha之间:透明度

2 回答

  • 4

    所以,我不太了解WHAT CODE定义或消耗hjust / vjust,但是使用TextMate的“在项目中查找”(在ggplot2 / R /目录中)为hjust,我没有看到任何看起来像它们的行hjust的定义或实现......只是它被列为有效aes的地方以及传递到的地方 .

    这让我想去看看网格......

    http://stat.ethz.ch/R-manual/R-patched/library/grid/html/grid.text.html

    这让我想知道更多关于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>
    

    所以,它是一个textGrob,而且,只是,hjust,和vjust只是被传递到它...关闭到textGrob

    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>
    

    所以,这是一个grob ........... off to grob ......

    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网格hjust vjust

    我发现,在重写谷歌的搜索自动更正后

    http://rwiki.sciviews.org/doku.php?id=tips:graphics-grid:hvjust

    回想一下Hadley的书,我注意到p.196参考文献实际上并没有提出正确的意见或仅仅是正当理由 .

    阅读文档

    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.
    

    所以,这是我的想法 .

    • just参数可以是字符串或数字

    • hjust和vjust只是数字,只能覆盖

    • 如果您尝试为它们使用字符串,它可能会"work",但会发出警告

    那么,让我们看看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)
    

    现在注意区别,如果我改变draw.text使用hjust和vjust AS STRINGS

    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)
    

    长话短说:我认为当你使用hjust或vjust作为字符串时,你违反了文档(它的值应该是数字0 <= x <= 1),如果你想使用字符串,你必须使用just参数....

  • 1

    hjustvjust 应该是数字,请查看手册( ?geom_text ):

    • hjust':水平对齐,介于0和1之间

    • 'vjust':垂直对齐,介于0和1之间

相关问题