annotation_custom 与 ggplot2 中的 npc 坐标

Ido*_*mir 4 r ggplot2

我想在每个面板的底部画一个框,每个面板可能有 不同的y 范围。该框应始终位于 x 轴上的 3 到 7 之间,并且从面板底部的边框到 y 轴高度的 5% 左右。所以我想指定 egannotation_customgeom_rect单位的坐标,例如:

xmin=unit(3, "native"), xmax=unit(7,"native"), ymin=unit(0,"npc"), ymax=unit(0.05,"npc")
Run Code Online (Sandbox Code Playgroud)

我可以做到,但这些单位似乎被忽略了,它总是原生的。

library("grid")
library("ggplot2")

df <- data.frame(x=c(1:10,1:10),y=c(1:10,1001:1010),condition=rep(c("A","B"),each=10))
g <- rectGrob(gp=gpar(fill="black"))
p <- ggplot(df, aes(x=x,y=y)) + geom_line() + facet_wrap(~ condition, scales="free_y") 
p + geom_rect(xmin=3,xmax=7,ymin=0.56,ymax=1,colour="black", fill="black")

g <- rectGrob(gp=gpar(fill="black"))
p + annotation_custom(g, xmin=3, xmax=7, ymin=0, ymax=1 )
p + annotation_custom(g, xmin=unit(3, "native"), xmax=unit(7,"native"),ymin=unit(0,"npc"),ymax=unit(1,"npc") )
Run Code Online (Sandbox Code Playgroud)

bap*_*ste 7

你可以这样做,

g <- rectGrob(y=0,height = unit(0.05, "npc"), vjust=0, 
              gp=gpar(fill="black"))
p + annotation_custom(g, xmin=3, xmax=7, ymin=-Inf, ymax=Inf)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明