Q: Add a custom Axis label with unique color and size I have the following chart:
df<- data.frame(year=c(1960,1961,1962),value=c(10,18,20))
ggplot(df,aes(x=year,y=value))+geom_col()+
geom_hline(yintercept =18,linetype="dashed", color = "red",size=1)


I would like to add a red label in the Y axis with the text "Actual Value" where the red line is.
I know i cann add the text changing breaks, but then i could not have the new text in red.
There is any way I can change only that one's colour and size but not the other legends in the axis?
I am looking for something like this:

Thank you!

A: You can use geom_text :
library(ggplot2)

ggplot(df,aes(x=year,y=value))+geom_col()+
  geom_hline(yintercept =18,linetype="dashed", color = "red",size=1) +
  geom_text(aes(1960,18,label = 'Actual Value', vjust = -1),
           color = 'red', size = 8)



A: Here's an approach with annoate:

*

*Use annoate to specify the text you want to plot. Provide x =  and y =  arguments to indicate where to plot.

*Use coord_cartesian with clip = 'off' to plot outside the plot area and use xlim =  to fix the x axis to the data rather than also including the coordinates of the annotate call.

*Use plot.margin to widen the left margin so there's room for the text.

You may need to modify the xlim and x =  arguments to get it just right.
ggplot(df,aes(x=year,y=value)) + 
  geom_col() +
  geom_hline(yintercept =18,linetype="dashed", color = "red",size=1) + 
  coord_cartesian(clip = 'off',
                  xlim = c(1959.5,1962.5)) +
  annotate(geom = "text", y = 18, x = 1959.1, color = "red", label = "Actual Value") +
  theme(plot.margin = unit(c(1,1,1,3), "lines"))


Edit:
You can use \n in the label to make the text appear on two lines.
ggplot(df,aes(x=year,y=value)) + 
  geom_col() +
  geom_hline(yintercept =18,linetype="dashed", color = "red",size=1) + 
  coord_cartesian(clip = 'off',
                  xlim = c(1959.5,1962.5)) +
  annotate(geom = "text", y = 18, x = 1959.2, color = "red", label = "Actual\nValue") +
  theme(plot.margin = unit(c(1,1,1,2), "lines"))


