How can I get the x, y coordinates of a geom_point in a ggplot, where the reference frame is the entire plotted image?
I can create a ggplot with some geom_points using:
library(ggplot2)
my.plot <- ggplot(data.frame(x = c(0, 0.456, 1), y = c(0, 0.123, 1))) +
geom_point(aes(x, y), color = "red")
This gives:
By converting this into a grob, I can extract some additional information about this ggplot, like the coordinates with respect to the plot panel, marked by the purple arrow. However, this ignores the space taken up by the axes.
my.grob <- ggplotGrob(my.plot)
my.grob$grobs[[6]]$children[[3]]$x
# [1] 0.0454545454545455native 0.46native 0.954545454545454native
my.grob$grobs[[6]]$children[[3]]$y
# [1] 0.0454545454545455native 0.157272727272727native 0.954545454545454native
How can I get the values of the x, y coordinates when I start measuring from the bottom-left corner of the entire image, marked by the green arrow?
If it's possible, I would like the solution to take into account the theme of the ggplot. Adding a theme like + theme_void()
affects the axes and also shifts the location of the points with respect to the entire plotted image.
Update: I realised that the font size of the axes changes depending on the width and height of the plot, affecting the relative size of the plot panel. So it won't be trivial to provide the location in npc units without defining the plot width and plot height. If possible, give the location of the geom_points as a function of the plot width and plot height.
npc
units (so the frame where bottom left is0-0
and top right is1-1
). – Liris