I make plots with an annotation, currently defined by absolute values. This causes issues when the Y-axis scale changes: the annotation gets squished/expanded. I am making many of these plots within a for loop so manually adjusting the values isn't feasible.
Is there a way for me to make the size of the annotation constant, perhaps relative to the image output size? Alternatively, relative to y-axis range? If using the axis range, keep in mind that two datasets are plotted so finding he maximum range between the two would be necessary. Thanks!
Simplified example below. Swap c_1
and t_1
for other df
columns to generate Plot2/3
library(ggplot2)
c_1 <- c(rep(c(5),times=1000), rep(c(10),times=50), rep(c(5),times=1000))
t_1 <- c(rep(c(5),times=1000), rep(c(15),times=50), rep(c(5),times=1000))
c_2 <- c(rep(c(5),times=1000), rep(c(75),times=50), rep(c(5),times=1000))
t_2 <- c(rep(c(5),times=1000), rep(c(68),times=50), rep(c(5),times=1000))
c_3 <- c(rep(c(5),times=1000), rep(c(150),times=50), rep(c(5),times=1000))
t_3 <- c(rep(c(5),times=1000), rep(c(140),times=50), rep(c(5),times=1000))
df <- data.frame(position = 1:length(c_1), c_1, t_1, c_2, t_2, c_3,t_3)
Plot1 <- ggplot()+
geom_hline(yintercept = -10, size =0.5, col = "#333333") +
annotate(geom = "rect", ymin = -20.5, max = -0.5, xmin = 525, xmax = 1525, fill = "#333333")+
annotate(geom = "text", y = -10, x = median(1:2050), label = "My_Text", col = "white", size = 5, fontface = 'italic') +
geom_line(data = df, aes(x=position, y=c_1), size = .75, alpha = 0.7)+
geom_line(data = df, aes(x=position, y=t_1,), size = .75, alpha = 0.7)+
ggtitle("Plot 1")
ggsave("~/Desktop/Plot1.png", Plot1, width = 8, height = 1.7)