은 "Inf를"해결책은 문제가 당신이 여러 줄의 텍스트를 할 때. 또한 텍스트와 패널 가장자리 사이에는 여백이 없으므로 추한 것입니다. 다른 솔루션은 좋지 않은 데이터에 대해 명시 적으로 언급해야합니다.
원하는 효과는 annotation_custom (또는 내 예에서는 proto Geom에서 직접 생성)을 통해 멋지게 얻을 수 있습니다. 구성 가능한 여백, 텍스트 및 상자 양쪽 맞춤이 있습니다. 다음 코드에서 추가 된 보너스는 어떤 패싯에 facets=data.frame(cat1='blue', cat2='tall')
같은 주석을 달 것인지 지정할 수 있다는 것입니다.
library("ggplot2")
annotate_textp <- function(label, x, y, facets=NULL, hjust=0, vjust=0, color='black', alpha=NA,
family=thm$text$family, size=thm$text$size, fontface=1, lineheight=1.0,
box_just=ifelse(c(x,y)<0.5,0,1), margin=unit(size/2, 'pt'), thm=theme_get()) {
x <- scales::squish_infinite(x)
y <- scales::squish_infinite(y)
data <- if (is.null(facets)) data.frame(x=NA) else data.frame(x=NA, facets)
tg <- grid::textGrob(
label, x=0, y=0, hjust=hjust, vjust=vjust,
gp=grid::gpar(col=alpha(color, alpha), fontsize=size, fontfamily=family, fontface=fontface, lineheight=lineheight)
)
ts <- grid::unit.c(grid::grobWidth(tg), grid::grobHeight(tg))
vp <- grid::viewport(x=x, y=y, width=ts[1], height=ts[2], just=box_just)
tg <- grid::editGrob(tg, x=ts[1]*hjust, y=ts[2]*vjust, vp=vp)
inner <- grid::grobTree(tg, vp=grid::viewport(width=unit(1, 'npc')-margin*2, height=unit(1, 'npc')-margin*2))
layer(
data = NULL,
stat = StatIdentity,
position = PositionIdentity,
geom = GeomCustomAnn,
inherit.aes = TRUE,
params = list(
grob=grid::grobTree(inner),
xmin=-Inf,
xmax=Inf,
ymin=-Inf,
ymax=Inf
)
)
}
qplot(1:10,1:10) + annotate_text2('some long text\nx = 1', x=0.5, y=0.5, hjust=1)