0

This is the code that I have so far. The idea with this was to investigate if there was a correlation between celebrity appearances in THY's commercials and if that had any impact on stock price. I have the dates of the Superbowl in question. What I would like to do is to highlight and then label when the Superbowl happened in relation to the plot, making it easier to illustrate if there was any correlation between the two. The dataframe: THY_2_2016 is the final stock price by day for February 2016 for THY from Yahoo Finance. I downloaded this data and named the THY_2_2016 object the object that contains that data. I also have similar code and plots for each following year up until February 2020.

library(ggplot2)
library(dplyr)
library(ggdark)

THY_2_2016 %>% ggplot(aes(Date, Close)) + 
  geom_line(color = "orange") +  
  labs(title = "Turkish Airlines' Final Daily Stock Price for February 2016", 
       subtitle = "Source: Yahoo Finance",
       y = "Final Stock Price (in Dollars)") + 
  dark_theme_gray() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

I know that the 2016 Superbowl happened on February 7, 2016, I would just like to label this even on the same plot and perhaps have a vertical line showing it. Otherwise, the plot just looks like a basic line graph that has no meaning. Could it be as simple as creating a new data frame with only one entry being February 7 and then adding that to the ggplot code? Doing so seems simple enough, but will that still allow me to label or point to that very easily? Any input would be greatly appreciated!

Plot of final daily stock price

Mehmet
  • 31
  • 1
  • 9
  • 1
    One way would be to use an `xline` to mark the day: http://www.sthda.com/english/wiki/ggplot2-add-straight-lines-to-a-plot-horizontal-vertical-and-regression-lines – J. Doe. Nov 25 '20 at 08:57
  • 1
    I would say this a duplicate: https://stackoverflow.com/questions/12876501/r-ggplot2-labelling-a-horizontal-line-on-the-y-axis-with-a-numeric-value – J. Doe. Nov 25 '20 at 08:58
  • @J.Doe. The `geom_vline()` function is what I was looking for exactly and I'm embarrassed that I haven't thought of it! The post that you referenced was also very helpful! Thank you so much! – Mehmet Nov 25 '20 at 09:04

2 Answers2

0

all I think I figured it out! I played around with the annotate function as well as some arguments in the geom_text function but I couldn't get the results that I wanted. I discovered that the geom_label function is what I was really after. The function is simple and intuitive. I noticed that it was as simple using the x and y values that you wanted. This means that the geom_label function could be placed anywhere!

libraryggplot2)
library(dplyr)
library(ggdark)

THY_2_2016 %>% ggplot(aes(Date, Close)) + 
  geom_line(color = "orange") + 
  geom_vline(aes(xintercept = as.Date.character("2016-02-07"))) +
  geom_label(x = as.Date.character("2016-02-07"), 
             y = 6.7, label = "Superbowl 50", color = "white") +
  labs(title = "Turkish Airlines' Final Daily Stock Price for February 2016", 
       subtitle = "Source: Yahoo Finance",
       y = "Final Stock Price (in Dollars)") + 
  dark_theme_gray() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

I noticed that the geom_vline function is really finicky. I left the color argument alone and it looks like R or ggplot automatically selected a color that would work for it.

corrected plot

Mehmet
  • 31
  • 1
  • 9
-4

Labelling a specific portion in graph using R is not posibble, you can alternatively take screenshot and edit pic

  • 2
    It’s definitely possible. We call these “annotations” [here](https://www.r-graph-gallery.com/233-add-annotations-on-ggplot2-chart.html) is a link with basic annotation examples – Daniel_j_iii Nov 25 '20 at 14:12
  • This isn't correct. text(), mtext()... these are base operations. – quickreaction Nov 25 '20 at 14:30