Adding legend to ggplot (Plug Power evaluation)

Good morning,

Could someone kindly help me correct the following since after many tryouts, I didn't find how to include legend into my code. Everything else seem to work efficiently


# This chunk is to get the data for PLUG from Yahoo finance between a selected time frame

quantmod::getSymbols("PLUG",from = "2020-01-01",to = Sys.Date()) 
  PLUGadjusted = PLUG[,6]
  plugDailyReturn <- dailyReturn(PLUGadjusted)
  PLUGdailyLogReturn <- dailyReturn(PLUGadjusted, type = 'log')
  PLUGdailyLogReturn[,1]
  quantile <- quantile(PLUGdailyLogReturn,0.01)
  LastPlugAdjPrice <- tail(PLUGadjusted,1)

# This chunk is to calculate the Value at Risk. We need to amend the quantity of PLUG we have in portfolio

VarCalculated <- 200000 * (exp(quantile)-1) * LastPlugAdjPrice
VarCalculated

# This chunk is to obtain a daily candlestick chart. We need to change the time frame accordingly to what we would like to observe

chartSeries(PLUG,theme = "white",type = "candlesticks",bar.type = "hlc",up.col = "white",dn.col = "black",subset = '2022-01::2022-09')

# This chunk is to import the data from our PLUG Excel file at LtD+DtD tab that we feed every day from extracting the data on Yahoo finance

Portfolio_evaluation_UPDATED_02_06_2022 <- read_excel("J:/0040_Acc&Treasury&Risk&IT/J&T/Portfolio evaluation UPDATED 02-06-2022.xlsx",sheet = "LtD+DtD",col_types=c("date","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric"))
Data_for_plot <- Portfolio_evaluation_UPDATED_02_06_2022[c(1,15,16)]

# This chunk is to obtain a chart with LtD and DtD PnL from the previous chunk. It will take all the data from our Excel file

chart = ggplot() + geom_line(data = Data_for_plot,mapping = aes(x = Date, y = LtD_PnL_EURFX),color = "blue") +
  geom_point(data = Data_for_plot,mapping = aes(x = Date, y = LtD_PnL_EURFX),color = "blue") +
  geom_line(data = Data_for_plot,mapping = aes(x = Date, y = DtD_PnL_EURFX),color = "red") +
  geom_point(data = Data_for_plot,mapping = aes(x = Date, y = DtD_PnL_EURFX),color = "red") +
  labs(x = "Date",y = ("   LTD_PnL ,    DtD_PnL   (EUR)"),title = "Plug Power PnL (EUR)") + scale_y_continuous(labels = comma) 

my.cols <- c("red", "blue")
my.names <- c("LTD_PnL", "DtD_PnL")
names(my.cols) <- my.names

chart + scale_colour_manual("", 
                        breaks = as.character(my.names),
                        values = my.cols)
chart

Thank you very much for helping.

Are you able to share what's in the data you're plotting, Portfolio_evaluation_UPDATED_02_06_2022? A subset would be sufficient to help troubleshoot.

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.