在ggplot2中具有npc坐标的annotation_custom [英] annotation_custom with npc coordinates in ggplot2

查看:163
本文介绍了在ggplot2中具有npc坐标的annotation_custom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每个面板的底部绘制一个框,其中每个面板的y范围可能不同.该框应始终在x轴上从3到7,并且从面板底部的边界到y轴高度的大约5%. 所以我想指定例如annotation_customgeom_rect的单位类似于:

I would like to draw a box at the bottom of each panel, where each panel may have different y-ranges. The box should always be from 3 to 7 on the x-axis and from the border at the bottom of the panel to about 5% of y-axis high. So I would like to specify the coordinates of e.g. annotation_custom or geom_rect in units something like:

xmin=unit(3, "native"), xmax=unit(7,"native"), ymin=unit(0,"npc"), ymax=unit(0.05,"npc")

我可以做到,但是单位似乎总是被忽略了,它总是本机的.

I can do it, but the units seem to be ignored, its always native.

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") )

推荐答案

您可以做到,

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)

这篇关于在ggplot2中具有npc坐标的annotation_custom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆