I have the following code snippet:
for(j in 1:10){
max_value[j] <- round(value * 1.1 ^ j, 2)
min_value[j] <- round(value * 0.9 ^ j, 2)
max_estimate[j] <- round(log(max_value[j]) + 0.2775, 3)
min_estimate[j] <- round(log(min_value[j]) + 0.2775, 3)
}
gg <- ggplot(data, aes(x = as.Date(rowname), y = get(ticker))) +
geom_line() +
# value calulation date vline
geom_vline(xintercept = date_value, col = "Gray", size = 1) +
# Index value at value calculation date hline
geom_hline(yintercept = value_value, col = "Gray", size = 1) +
# Max values hlines
geom_hline(yintercept = pre_values_max, col = "Red", size = 1) +
# value and return estimate for the value calculation date
annotate("text", as.Date("2013-12-31"), value_value * 1.02,
label = paste0(value, ", ", percent(return_estimate)), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_max[1] * 1.02,
label = paste0(max_value[1], ", ", percent(max_estimate[1])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_max[2] * 1.02 ^ 1,
label = paste0(max_value[2], ", ", percent(max_estimate[2])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_max[3] * 1.02,
label = paste0(max_value[3], ", ", percent(max_estimate[3])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_max[4] * 1.02,
label = paste0(max_value[4], ", ", percent(max_estimate[4])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_max[5] * 1.02,
label = paste0(max_value[5], ", ", percent(max_estimate[5])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_max[6] * 1.02,
label = paste0(max_value[6], ", ", percent(max_estimate[6])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_max[7] * 1.02 ^ 1,
label = paste0(max_value[7], ", ", percent(max_estimate[7])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_max[8] * 1.02,
label = paste0(max_value[8], ", ", percent(max_estimate[8])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_max[9] * 1.02,
label = paste0(max_value[9], ", ", percent(max_estimate[9])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_max[10] * 1.02,
label = paste0(max_value[10], ", ", percent(max_estimate[10])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_min[1] * 1.02,
label = paste0(min_value[1], ", ", percent(min_estimate[1])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_min[2] * 1.02,
label = paste0(min_value[2], ", ", percent(min_estimate[2])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_min[3] * 1.02,
label = paste0(min_value[3], ", ", percent(min_estimate[3])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_min[4] * 1.02,
label = paste0(min_value[4], ", ", percent(min_estimate[4])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_min[5] * 1.02,
label = paste0(min_value[5], ", ", percent(min_estimate[5])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_min[6] * 1.02,
label = paste0(min_value[6], ", ", percent(min_estimate[6])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_min[7] * 1.02,
label = paste0(min_value[7], ", ", percent(min_estimate[7])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_min[8] * 1.02,
label = paste0(min_value[8], ", ", percent(min_estimate[8])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_min[9] * 1.02,
label = paste0(min_value[9], ", ", percent(min_estimate[9])), color = "Blue") +
annotate("text", as.Date("2013-12-31"), pre_values_min[10] * 1.02,
label = paste0(min_value[10], ", ", percent(min_estimate[10])), color = "Blue") +
# Min values hlines
geom_hline(yintercept = pre_values_min, col = "Green", size = 1) +
ggtitle(paste0(country, ", ", ticker)) +
xlab("Date") + ylab("Index")
print(gg)
As you can see, there are 21 annotations, but their values are made in the first loop. Is there a way to programmatically add the annotations? For loops or if-statements don't seem to work inside ggplot. What if I had a hundred of these values, how could the plot be made then, without having a hundred extra rows? Sorry for no reprex, but the code and the objective should be quite clear.