【问题标题】:annotation_custom with npc coordinates in ggplot2annotation_custom 与 ggplot2 中的 npc 坐标
【发布时间】:2015-07-30 11:31:22
【问题描述】:

我想在每个面板的底部画一个框,每个面板可能有不同的 y 范围。该框应始终位于 x 轴上的 3 到 7 之间,并且从面板底部的边框到大约 y 轴高度的 5%。 所以我想指定例如的坐标annotation_customgeom_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") )

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    你可以的,

    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)
    

    【讨论】:

    • 不知何故,这个问题让我想知道如何定义 complex 单位,1 npc + 2i cm 其中真实(imag)部分将代表相对(resp. absolute)单位。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多