Please help me find a universal way to place text at a constant distance from the right side of the plot, as shown below. Since the plot area is already on the right, unfortunately, the current location calculation positions the text too far to the left. Notice that the text is right justified.
My last thought: if we knew the distance in centimeters between the words x = 1 and x = 2, we could easily calculate the position. Unfortunately, this is apparently not so easy to get this distance.
FYI: I do not want to name the lines.
Full size image
library(ggplot2) library(reshape) library(gridExtra) df = data.frame(x =(1:3),One=c(12, 8, 13),Two=c(13, 7, 11),Three=c(11, 9, 11)) df.melt = melt(df, id.vars="x") xmax = max(df.melt$x); xmin = min(df.melt$x) ymax = max(df.melt$value); ymin = min(df.melt$value) dfa = data.frame(x=(xmax-xmin)*1.15+xmin, y=c(11,12,13.5), ann=c("10.1|","1.1|","Texttexttext|")) dfa.melt = melt(dfa, id.vars=c("x","ann")) p = ggplot() + geom_line(data=df.melt, aes(x=x, y=value, color=variable), show_guide=F) + geom_text(data=dfa.melt, aes(x=x, y=value, hjust = 1, label=ann), size=3) + coord_cartesian(xlim=c(xmin,xmax), ylim=c(ymin,ymax)) p1 = p + theme(plot.margin=unit(c(1,3,0,0),"cm"), axis.text.y=element_text(size=10)) p2 = p + theme(plot.margin=unit(c(1,3,0,3),"cm"), axis.text.y=element_text(size=35)) p1c <- ggplot_gtable(ggplot_build(p1)) p1c$layout$clip[p1c$layout$name=="panel"] <- "off" p2c <- ggplot_gtable(ggplot_build(p2)) p2c$layout$clip[p2c$layout$name=="panel"] <- "off" grid.arrange(p1c, p2c, ncol=2)