【问题标题】:ggplot2 annotate layer position in Rggplot2在R中注释图层位置
【发布时间】:2016-04-08 19:20:18
【问题描述】:

在我的情节中,我有图例和文本注释。对于图例,我可以指定

legend.justification=c(1,0), legend.position=c(1,0)

定位相对于绘图区域的位置(例如右上角,左下角)。但是,当我放一个注释层(http://docs.ggplot2.org/0.9.3.1/annotate.html)时,似乎只能指定文本的坐标

annotate("text", x = 8e-7, y = 1e-5, label=data.note, size = 5)

而不是绘图区域的位置(我想将文本放在左下角)。文本的长度 (label) 可能因绘图而异。有没有办法做到这一点?谢谢!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以使用-InfInf 将映射到位置刻度的极端这一事实,而无需扩展它们以将其放置在左下角。需要hjustvjust 才能使参考点位于文本的左下角。 [使用 jlhoward 的模拟数据。]

    set.seed(1)
    df <- data.frame(x=rnorm(100),y=rnorm(100))
    
    ggplot(df, aes(x,y)) +geom_point()+
      annotate("text",x=-Inf,y=-Inf,hjust=0,vjust=0,label="Text annotation")
    

    【讨论】:

      【解决方案2】:

      这就是你要找的吗??

      set.seed(1)
      df <- data.frame(x=rnorm(100),y=rnorm(100))
      ggplot(df, aes(x,y)) +geom_point()+
        annotate("text",x=min(df$x),y=min(df$y),hjust=.2,label="Text annotation")
      

      可能需要对hjust=... 进行一些实验才能将其准确地显示在左下角。

      【讨论】:

        【解决方案3】:

        当您需要多行文本时,“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)
        

        【讨论】:

          猜你喜欢
          • 2016-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-16
          • 2016-01-31
          • 2021-08-03
          • 2016-04-11
          相关资源
          最近更新 更多