In a simple ggplot2 plot there is a textbox inserted with the function geom_textbox()
and my aim is to insert two more geom_textbox()
, one on the left side and one on the right side.
Adding the left textbox is easy - but for the right textbox I need the coordinates of the textbox border. And depending of what I will add to my code, there is a "new" coordinate system...
My code:
plot = ggplot() +
geom_textbox(aes(x=5,y=5,
label = "Test",
hjust = 0,
vjust = 0.5,
colour = "black", fill = "lightgray", orientation = "upright"),
width = unit(6, "cm"),
height = unit(2, "cm"),
box.r = unit(5.5, "pt")) +
# left textbox
#geom_textbox(aes(x=5,y=5,
# label = "",
# hjust = 1,
# vjust = 0.5,
# colour = "black", fill = "lightgray", orientation = "upright"),
# width = unit(0.5, "cm"),
# height = unit(0.5, "cm"),
# box.r = unit(0, "pt")) +
# right textbox
#geom_textbox(aes(x=5.???,y=5,
# label = "",
# hjust = 0,
# vjust = 0.5,
# colour = "black", fill = "lightgray", orientation = "upright"),
# width = unit(0.5, "cm"),
# height = unit(0.5, "cm"),
# box.r = unit(0, "pt")) +
geom_point(aes(x=c(5),y=c(5)), color = "black", size = 2) +
# the following three geom_point() are in the screenshot and I'm missing the correct x or y value
#geom_point(aes(x=5.???,y=5), color = "red", size = 3) + # x value of the right border of the textbox
#geom_point(aes(x=5,y=5.???), color = "red", size = 3) + # y value of the upper border of the textbox
#geom_point(aes(x=5,y=4.???), color = "red", size = 3) + # y value of the lower border of the textbox
scale_discrete_identity(aesthetics = c("color", "fill", "orientation")) #+
#coord_fixed(ratio=1)+
#xlim(4.5,5.5) + ylim(4.5, 5.5)
The screenshot with the 3 red points, I'm interested in the coordinates.
While my research I also crossed ggplot_build(plot)$data[[1]]
but I was not able to use it correctly?!
So may thanks in advance for helping me to find a way to calculate the three red points.
It seems like we have to think differently of the geom_textboxes(). They do not have so much of real bounding boxes as more of a fixed box which always stays the same size independent of the axis scaling.
We can still achieve what you want using hjust
. About Hjust
library(ggplot2)
#install.packages("ggtext")
library(ggtext)
middle_width <- 6
side_width <- 0.5
ggplot() +
geom_textbox(aes(x=5,y=5,
label = "Middle",
hjust = 0,
vjust = 0.5,
colour = "black", fill = "lightgray", orientation = "upright"),
width = unit(middle_width, "cm"),
height = unit(2, "cm"),
box.r = unit(5.5, "pt")) +
# Left textbox
geom_textbox(aes(x=5,y=5,
label = "",
hjust = 1,
vjust = 0.5,
colour = "black", fill = "lightgray", orientation = "upright"),
width = unit(side_width, "cm"),
height = unit(0.5, "cm"),
box.r = unit(0, "pt")) +
# Right textbox
geom_textbox(aes(x=5,y=5,
label = "",
hjust = -1*middle_width/side_width,
vjust = 0.5,
colour = "black", fill = "lightgray", orientation = "upright"),
width = unit(side_width, "cm"),
height = unit(0.5, "cm"),
box.r = unit(0, "pt")) +
scale_discrete_identity(aesthetics = c("color", "fill", "orientation"))