I am using geom_text
to annotate plots in gglot2
and I want use relative positioning rather than absolute. That is, I want a position of (0.5, 0.5)
to be dead center regardless of the x and y axis limits. Is that possible?
Alternatively I could of course transform a relative position to an absolute one if I had the x and y limits. Is it possible to extract those from a plot?
Yes, it is possible to extract the x and y limits from a ggplot2-plot. This function returns the x and y coordinate of the center of a ggplot2 plot object:
If your x-Data is in POSIXct-format, you still have to transform it:
If you know the range of the data in your plot, you can calculate the "true" x and y limits using the fact that
ggplot
using an additive expansion factor of 0.05 by default, so that the extents of the graph extend just slightly beyond the actual data values.You can specify and multiplicative and additive expansion factor when specifying scales using
expand = c(mult, add)
wheremult
is the multiplicative factor and so on. So the default setting isexpand = c(0,0.05)
.