How to put a text label on a plot located independ

2019-09-07 17:46发布

问题:

I am trying to put a text label on a ggplot, but I do not want to give the coordinates of the label in the data coordinates, but use some kind of coordinates that are not related to the data. I know the transformation trick:

xrng <- range(x)
yrng <- range(y)
plot <- plot + annotate("text",x = xrng[2], y = yrng[2], label="bla") # plot label on top right

But this does not work well for plots with a logarithmic scale (and you have to do the transformation, which may not always be as easy as in the example). Any ideas?

回答1:

ggplot2 plots are made with grid graphics so you can use grid commands to annotate the plot.

library('ggplot2')
library('grid')
qplot(rnorm(10))
grid.text('my label')

?grid.text gives several ways of specifying the coordinates. Maybe one of them will make sense in your case.



标签: r ggplot2