如何更改geom_text()在绘图区域外的字体大小?

时间:2016-06-17 21:29:32

标签: r plot ggplot2 geom-text

我想对一个情节进行注释,我希望这个音符在情节区域之外。我找到了this solution,它可以在绘图区域外添加注释,但我无法弄清楚如何更改标签的外观(最重要的是,为了我的目的,字体大小)。

以下是上述解决方案的最小示例:

library (ggplot2)
library(grid)

df=data.frame(y=c("dog1","dog2","dog3"),x=c(12,10,14),n=c(5,15,20))
p <- ggplot(df, aes(x,y)) + geom_point()

# Add the annotation
p <- p + geom_text(aes(label = "Hello World!", x = 0, y = 0), vjust = 2, hjust = 1)

# Code to override clipping
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

理想情况下,音符会位于左下角。

1 个答案:

答案 0 :(得分:1)

library (ggplot2)
library(grid)

df=data.frame(y=c("dog1","dog2","dog3"),x=c(12,10,14),n=c(5,15,20))
p <- ggplot(df, aes(x,y)) + geom_point()

# Add the annotation
p <- p + geom_text(size=8, colour="red", aes(label = "Hello World!", x = 0, y = 0), vjust = 2.5, hjust = 1)

# Code to override clipping
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)