【发布时间】:2015-07-30 11:31:22
【问题描述】:
我想在每个面板的底部画一个框,每个面板可能有不同的 y 范围。该框应始终位于 x 轴上的 3 到 7 之间,并且从面板底部的边框到大约 y 轴高度的 5%。
所以我想指定例如的坐标annotation_custom 或 geom_rect 单位如下:
xmin=unit(3, "native"), xmax=unit(7,"native"), ymin=unit(0,"npc"), ymax=unit(0.05,"npc")
我可以做到,但单位似乎被忽略了,它总是原生的。
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") )
【问题讨论】: