Specify position of geom_text by keywords like “top”, “bottom”, “left”, “right”, “center”

后端 未结 5 1384
名媛妹妹
名媛妹妹 2020-12-31 23:37

I wish to position text in a ggplot without specifying x and y positions, but instead using keywords, like e.g. in graphics::leg

5条回答
  •  無奈伤痛
    2021-01-01 00:06

    In ggpmisc::geom_text_npc the x and y positions are given in npc units (0-1). However, the positions can also be specified as "words":

    d = data.frame(x = rep(c("left", "center", "right"), each = 3),
                   y = rep(c("bottom", "middle", "top"), 3))
    d$lab = with(d, paste0(x, "-", y))
    d
    #        x      y           lab
    # 1   left bottom   left-bottom
    # 2   left middle   left-middle
    # 3   left    top      left-top
    # 4 center bottom center-bottom
    # 5 center middle center-middle
    # 6 center    top    center-top
    # 7  right bottom  right-bottom
    # 8  right middle  right-middle
    # 9  right    top     right-top
    
    ggplot(d) +
      geom_text_npc(aes(npcx = x.chr, npcy = y.chr, label = lab))
    

提交回复
热议问题