Can';t调用函数中的last_plot和grid.text

Can';t调用函数中的last_plot和grid.text,r,ggplot2,R,Ggplot2,我使用ggplot2为我的公司开发了一个特定的主题,我需要根据grid.text中的字体大小调整打印边距 我的想法是创建一个函数,在这里我调用last\u plot(),调整plot.margin,然后添加一些grid.text()函数。但是,不知何故,我无法使这个最小的代码示例正常运行: library(ggplot2) library(grid) ggplot(diamonds, aes(x = cut, fill = cut)) + geom_bar() addText <

我使用ggplot2为我的公司开发了一个特定的主题,我需要根据grid.text中的字体大小调整打印边距

我的想法是创建一个函数,在这里我调用
last\u plot()
,调整plot.margin,然后添加一些
grid.text()
函数。但是,不知何故,我无法使这个最小的代码示例正常运行:

library(ggplot2)
library(grid)

ggplot(diamonds, aes(x = cut, fill = cut)) + 
  geom_bar()

addText <- function(){
  last_plot() %+% theme(plot.margin = margin(0.30, 0, 0, 0, "npc"))
  grid.text("Something", y = unit(0.90, "npc"))
}

addText()

需要打印()。这就是原因。它起作用了!需要打印()。这就是原因。它起作用了!
library(ggplot2)
library(grid)

ggplot(diamonds, aes(x = cut, fill = cut)) + 
  geom_bar()

addText <- function(){
  print(last_plot() + theme(plot.margin = margin(0.30, 0, 0, 0, "npc")))
  grid.text("Something", y = unit(0.90, "npc"))
}

addText()