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

Ole*_*Ole 5 plot r ggplot2 geom-text

我想对绘图进行注释,并且希望注释位于绘图区域之外。我找到了这个解决方案,它适用于在绘图区域之外添加注释,但我无法弄清楚如何更改标签的外观(最重要的是,就我的目的而言,字体大小)。

这是上述解决方案的一个最小示例:

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)
Run Code Online (Sandbox Code Playgroud)

理想情况下,注释应位于左下角。

Kur*_*ert 3

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)
Run Code Online (Sandbox Code Playgroud)

  • 如果您不打算使用“annotation_custom”,那么您应该为“geom_text”创建一个新的数据框。否则,“你好世界!” 将被重叠绘制 3 次,父数据框的每行一次。这就是注释文本看起来呈锯齿状的原因。如果您在调用 `geom_text` 中执行 `x=c(1,3,5)` 来展开“Hello World!”的三个副本,则可以表明发生了过度绘制。 (2认同)