【问题标题】:r plot text adding variable for text to be boldedr plot text 为要加粗的文本添加变量
【发布时间】:2022-12-14 08:27:07
【问题描述】:

在下面的示例中,变量包含字母 a。但是 legend in legend 只是返回 temp 而不是加粗的 a。如何使这项工作像它“应该”的那样?

a <- 2
b <- 5
temp <- letters[1]
temp
plot(a,b)

legend("topleft", legend = c(as.expression(bquote(bold(get(temp)))),
                             as.expression(bquote(temp))))

【问题讨论】:

标签: r plot


【解决方案1】:

从这里建立答案:using bold in mtext on string coming from vector element

为了告诉bquote评估变量,而不是简单地将其打印为文本,您需要将术语包装在.()中,这会导致它在指定的环境中评估它(由where参数给出) ).

temp <- 'a'
e <- new.env()
assign('temp','b',envir = e)
plot(2,5)
legend("topleft",
       legend = c(as.expression(
           bquote(bold(temp))),                 # No .(), `temp` is text
           bquote(bold(.(temp))),               # eval `temp` in parent.env
           bquote(bold(.(temp)), where = e)))   # eval `temp` in envir "e"

?bquote 的帮助中提到了这一点:

bquote 引用它的参数,除了 .() 中包含的术语在指定的 where 环境中评估

【讨论】:

    【解决方案2】:

    这两个回答帮助我找到了答案。我问的是传说中的问题。我正在使用 terra 包中的绘图函数,但答案在 terra::plot 的图例选项中不起作用。但是,如果您将 x 和 y 替换为绘图中的相关坐标,则此代码会起作用。

    text(x, y, cex = 1.5, (bquote(paste((bold(.(a)))))))
    

    看起来非常笨拙,但是为了清楚起见,尝试在“a”之后的那串括号中添加一个空格似乎不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-07
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多