I want to have a label corresponding to the year when most of the events occurred in the x axis of this plot in R plot()
:
with one of the main clusters around 2008. However, the software decides on labeling the tick mark for 2010 instead.
There are posts explaining how to select some given labels; yet, how can I increase the density of labeled tick marks on the xaxis to get an approximate year for the spikes if I don't know these years ahead of the plotting? [CLARIFICATION: I don't want to have to interrogate the data to find out that the cluster is in 2008 - I just want to increase the number of labeled tick marks so as to have one of them fall close to the spike.]
Here is the code ready to copy and paste:
require(RCurl)
require(foreign)
x <- getURL("https://raw.githubusercontent.com/RInterested/datasets/gh-pages/%5EDJI.csv")
DJI <- read.csv(text = x, sep =",")
DJI$Date <- as.Date(DJI$Date, format = "%m/%d/%Y") # Formatting Date as.Date
rownames(DJI) <- DJI$Date # Assigning Date to row names
DJI.raw <- DJI
DJI$Date <- NULL # Removing the Date column
chartSeries(DJI, type="auto", theme=chartTheme('white'))
# Function to calculate % change in closing price between days:
D2D = function (x) {
days = nrow(x)
delta = numeric(days)
for(i in 2:days){
delta[i] <- (100*((x[i,1] - x[i - 1,1])/(x[i - 1,1])))
}
delta
}
z <- as.data.frame(DJI$Adj.Close) # Subsetting closing price
DJI$InterDay <- D2D(z) # Included as add'l column to VTI.
DJI.raw$InterDay <- DJI$InterDay
plot(DJI.raw$Date, DJI.raw$InterDay < -4, pch=19, col=2, type='h',
xlab="Year", ylab="Days with > 4% change",
cex.axis=.7, cex.main=.8, cex.lab =.8,las=2,
main = "Clustering of big drop days")
FOLLOW-UP QUESTION:
If instead of formatting the data as above, I tried to consolidate it as an xts
object as follows:
require(RCurl)
require(foreign)
x <- getURL("https://raw.githubusercontent.com/RInterested/datasets/gh-pages/%5EDJI.csv")
DJI <- read.csv(text = x, sep =",")
DJI$Date <- as.Date(DJI$Date, format = "%m/%d/%Y") # Formatting Date as.Date
rownames(DJI) <- DJI$Date # Assigning Date to row names
DJI$Date <- NULL # Removing the Date column
DJI <- as.xts(DJI)
chartSeries(DJI, type="auto", theme=chartTheme('white'))
time(DJI)[DJI$Close == min(DJI$Close)]
# Function to calculate % change in closing price between days:
D2D = function (x) {
days = nrow(x)
delta = numeric(days)
for(i in 2:days){
delta[i] <- (100*((x[i,1] - x[i - 1,1])/(x[i - 1,1])))
}
delta
}
z <- as.data.frame(DJI$Adj.Close) # Subsetting closing price
DJI$InterDay <- D2D(z) # Included as add'l column to VTI.
DJI.raw$InterDay <- DJI$InterDay
plot(time(DJI), DJI$InterDay < -4, col=2, type='h',
xlab="Year", ylab="Days with > 4% change",
cex.axis=.7, cex.main=.8, cex.lab =.8,las=2,
main = "Clustering of big drop days")
How could I achieve a more informative x axis?