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

后端 未结 5 1390
名媛妹妹
名媛妹妹 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条回答
  •  爱一瞬间的悲伤
    2020-12-31 23:56

    geom_text wants to plot labels based on your data set. It sounds like you're looking to add a single piece of text to your plot, in which case, annotate is the better option. To force the label to appear in the same position regardless of the units in the plot, you can take advantage of Inf values:

    sp <- ggplot(mpg, aes(hwy, cty, label = "sometext"))+
      geom_point() +
      annotate(geom = 'text', label = 'sometext', x = -Inf, y = Inf, hjust = 0, vjust = 1)
    print(sp)
    

提交回复
热议问题