这样就可以了:
# load data:
theData = read.csv2(file = "sample.csv",header = TRUE,
stringsAsFactors = FALSE,sep = ",",
colClasses = c("character",rep("numeric",5)),dec=".")
# We want to plot a custom x-axis, so stop the default
# x-axis being drawn usign xaxt="n":
plot(theData$CLOSE,type="l",xaxt="n")
# Lets say you want to put a date label in 8 different locations:
locations = floor(seq(from=1,to=nrow(theData),by=nrow(theData)/8))
# Now draw the x-axis on your plot like this:
axis(side = 1, at = locations, labels=theData$DATE[locations],las=2)
在上面,side=1 表示在底部绘制轴。 at=locations 表示我们希望在我们之前创建的位置向量中给定的位置显示刻度标签。 labels=theData$DATE[locations] 提供了我们想要放置在我们放置标签的位置的标签。 las=2 表示您要旋转刻度标签。也可以尝试 las=1 进行不同的轮换。
但是,这些日期有点长,因此您可能希望创建更小的日期,如下所示:
# Convert your long dates to smaller dates like YYYY-MM-DD, and stick
# the results to the end of your data frame.
theData = cbind(theData,
"FormattedDate"=as.Date(theData$DATE,"%A, %B %e, %Y"))
# Replot and use your smaller dates. (ces.axis=0.8 makes
# the font smaller)
plot(theData$CLOSE,type="l",xaxt="n")
axis(side = 1, at = locations,
labels=theData$FormattedDate[locations],las=1,cex.axis=0.8)
最后,您还可以使用一些不错的时间序列包来轻松创建更好的图:
install.packages("xts")
library(xts)
xtsData = xts(theData[,"OPEN"],order.by = theData[,"FormattedDate"])
plot.zoo(xtsData)
# or
plot.xts(xtsData)