R中的轴功能

时间:2014-01-16 10:44:39

标签: r plot

:) 我想问你关于R中plot的一些事情。如果有人能帮助我,我会很高兴!!!

我写了Heston Model in R的代码。它生成了一个选项价格向量,我们称之为H1

每个期权价格(向量H1的每个元素)对应一天。矢量很长(3556个元素)。我想绘制它并分析图表。我使用了函数plot(.....)。然后我想在axis x上获得dates以及axis y prices我的选项。所以我使用了功能轴(1,z)(其中z是包含所有3556个日期的向量)和轴(2,H1)(其中H1包含所有3556选项价格)。
 关键是所有日期和所有期权价格都包含在我的图表中:/并且它看起来非常糟糕,没有人能够清楚地看到任何东西,因为轴x的日期数量巨大,轴y的期权价格巨大。如何减少日期和期权价格?我的意思是用一些间隔写出来吗?

如果不清楚请写信给我,我会发送整个代码。

非常感谢!!!!!!!!!!!!!!!!!!!!!!!!!! :)

1 个答案:

答案 0 :(得分:0)

如何使用ggplot2库与plyr::ddply()cut()一起绘制以将其缩短为20(或其他)间隔?

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

相关问题