Add a vertical line to histogram with ggplot2

Hi guys,
I need your help in creating graphs with ggplot in R Studio! In the histogram I added in the attachment, I would like to add a vertical line exactly at the value 0.8 for in-sample and 0.7 for out-of-sample with the label "S&P500".
My current code looks like this:
{r} ggplot(data=data) + geom_histogram(binwidth=0.10, aes(x=sahrpe_ratio, fill=Strategie), colour="Black") + facet_grid(.~Sample)

Can anyone help me here? Thanks a lot in advance!

gute frage

1 Like

make a dataset of annotations. add them with geom_vline

annot_df <- data.frame(x=c(.8,.7),
                       Sample=c('In-Sample','Out-of-Sample'),
                       Line = rep("S&P 500",2))

#then 
~your graphing code~ +
  geom_vline(data = annot_df,
              mapping = aes(xintercept=x,color=Line)) +
  scale_color_manual(values = c("S&P 500"="black"))
2 Likes

WOW thanks a lot, it works! It's almost perfect.
Maybe you also know how to change the color of the line into yellow and make it a bit thicker?

Thank you so much :slight_smile:

add a size param, the bigger the thicker

geom_vline(data = annot_df,
              mapping = aes(xintercept=x,color=Line),size=2)

change black to yellow here

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.