giorgos kapetanidis giorgos kapetanidis - 3 years ago 157
R Question

I would like to plot a specific (given x,y) line on a ggplot that I have already construct [R]

I have the following code:

library("ggplot2")

strike<-c(44,46,47,48,49,50,51,52,53,54,55,56,
57,58,60,64,46,47,48,49,50,51,52,53,
54,56,58,35,40,44,45,46,48,50,51,52,
53,54,55,56,58,60,70,80,35,40,45,46,
48,50,52,54,56,58,60,70,40,45,50,52,
54,60,70)



optionprice <- c(7.25,7.25,5.1,5.2,4.47,3.45,1.95,2,1.3,
0.8,0.44,0.28,0.14,0.26,0.08,0.25,5.48,
4.35,5.5,4.82,3.25,2.67,2.55,1.9,1.4,
0.85,0.4,18.16,11.31,9.5,7.35,7.05,5.25,
4.2,2.44,2.9,1.6,1.9,1.4,1.15,0.65,0.4,
0.08,0.06,15.7,12.25,8.5,7.6,6.6,5.15,3.9,
2.85,1.95,1.45,1.1,0.22,12.65,8.5,
5.07,4.3,3.55,1.63,0.44)

Expiration <- c('2017-10-20','2017-10-20','2017-10-20','2017-10-20',
'2017-10-20','2017-10-20','2017-10-20','2017-10-20',
'2017-10-20','2017-10-20','2017-10-20','2017-10-20',
'2017-10-20','2017-10-20','2017-10-20','2017-10-20',
'2017-11-17','2017-11-17','2017-11-17','2017-11-17',
'2017-11-17','2017-11-17','2017-11-17','2017-11-17',
'2017-11-17','2017-11-17','2017-11-17',
'2017-12-15','2017-12-15','2017-12-15',
'2017-12-15','2017-12-15','2017-12-15',
'2017-12-15','2017-12-15','2017-12-15',
'2017-12-15','2017-12-15','2017-12-15',
'2017-12-15','2017-12-15','2017-12-15',
'2017-12-15','2017-12-15',
'2018-03-16', '2018-03-16', '2018-03-16', '2018-03-16', '2018-03-16', '2018-03-16',
'2018-03-16', '2018-03-16', '2018-03-16', '2018-03-16', '2018-03-16', '2018-03-16',
'2018-06-15', '2018-06-15', '2018-06-15', '2018-06-15', '2018-06-15', '2018-06-15',
'2018-06-15')


Philips <- data.frame(strike, optionprice, as.factor(Expiration))


IrisPlot.shape <- ggplot(Philips, aes(strike, optionprice, shape =
Expiration, colour = Expiration))+ geom_point(size = 4)+labs(x =
"Strike/Exercise price")+labs(y = "Option Price")



print(IrisPlot.shape + scale_shape_manual(values = c(15, 16,
17,18,19)) + scale_colour_manual(values = c("chartreuse4",
"chocolate", "slateblue4","black","purple")))


and I would like to add in this plot a specific line which has x,y values respectively:

striking=seq(10, 53.49, length.out=63)
calling=53.49-striking


I would like this line to be included in the first plot and also a legend which it will say: Payoff . I attach a picture of this plot to see what I have already done. Thanks in advance.

this is the figure I have created

Answer Source

Just add a geom_line and turn of inheritance of aesthetics:

ggplot(Philips, aes(strike, optionprice, shape = Expiration, colour = Expiration)) + 
  geom_line(aes(x = striking, y = calling, lty = 'payoff'), inherit.aes = FALSE) +
  geom_point(size = 4, alpha = 0.4) + 
  labs(x = "Strike/Exercise price", y = "Option Price") +
  scale_shape_manual(values = c(15, 16, 17,18,19)) + 
  scale_colour_manual(values = c("chartreuse4", "chocolate", "slateblue4","black","purple"))

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download