0

:) I would like to ask you something about the plot in R. I would be very happy if someone could help me!!!

I wrote a code of Heston Model in R. It produced a vector of Option Prices, lets call it H1.

Each Option price (each element of the vector H1) corresponds to one day. The vector is very long ( 3556 elements ). I wanted to plot it and analyse the graph. I used the function plot(.....). Then I wanted on the axis x to have the dates and on the axis y the prices of my options. So I used the function axis(1, z) ( where z is the vector which contains all 3556 dates) and axis(2,H1) ( where H1 contains all 3556 option prices).
The point is all dates and all option prices are contained on my graph :/ and it looks very badly and none can see clearly anything because of the huge amount of dates in axis x and the huge amount of option prices in axis y. How can I reduce the number of dates and the option prices? I mean to write them with some interval?

If it is not clear write me please and I will send the whole code.

Thank you very much!!!!!!!!!!!!!!!!!!!!!!!!!! :)

Prasanna Nandakumar
  • 4,295
  • 34
  • 63
  • 2
    Welcome to SO! Yes a little bit of code would be perfect (make sure it is [reproducible](http://stackoverflow.com/a/5963610/2886003)). Also if you can explain what have you tried it would be perfect – llrs Jan 16 '14 at 10:51

1 Answers1

0

How about using the ggplot2 library for plotting along with plyr::ddply() and cut() to reduce it to 20 (or whatever) intervals?

prices<-10+runif(3000)*3+(1:3000)/3000
dates<-as.Date(1:3000,origin="1980-01-01")

df<-data.frame(prices,dates)

#required libraries
require(plyr) # library for ddply call
require(ggplot2)# plotting library

#call explained below
plotdata<-ddply(df,.(date_group=cut(dates,20)),summarise,avg_price=mean(prices))

ggplot(plotdata) + # base plot
  geom_line(aes(date_group,avg_price,group=1)) + # line
  geom_smooth(aes(date_group,avg_price,group=1)) + # smooth fit with CI
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) # rotate x axis labels

#explanation of ddply function
ddply(                          #call ddply function, takes a dataframe, returns a dataframe
  df,                           #input data data.frame 'df'
  .(date_group=cut(dates,20)),  #summarise by cut() - cuts the date into 20 blocks
  summarise,                    #tell to summarise
  avg_price=mean(prices)        #for each value of the cut (each group), average the prices
  )

enter image description here

Troy
  • 8,581
  • 29
  • 32