user3079834 - 1 month ago 12
R Question

# R quantmod manipulate x-axis

I'm using

`R`
with
`quantmod`
to visualize some data

``````library(quantmod)

# calculate the dates before and after date of interest
t1 <- as.Date(as.character(20101018),"%Y%m%d") - 25
t2 <- as.Date(as.character(20101018),"%Y%m%d") + 10

# get historical data
current_Stock <- getSymbols("AAPL", src="yahoo", from=t1, to=t2, auto.assign = FALSE)

# plot this stuff
chartSeries(current_Stock, name = "AAPL", TA = NULL)
# mark day of interest
``````

which yields the following chart:

I now want the the Dates on the
`axis`
to be vertical (rotated by 90 Degree) and without the year like in the answer of this thread and then print the
`day of interest`
(where the gray bar is) in
`bold`
and in
`gray`
.

The result of the other thread has the following chart:

But that solution is for
`chart_Series`
and I like the design and coloring of
`chartSeries`
from
`quantmod`
better.

How do I make

• The xaxis like in the second picture?

• Print the day under the grey bar in bold and gray?

You can suppress x-axis using rough method, `error` (As far as I know, it is the easiest) and `chartSeries(..., TA="add.TA.obj")` (because `addTA` makes an axis). Then all thatâ€™s left is to make the axis what you want.

``````library(quantmod)

### preparation of data (the same as OP's)
t1 <- as.Date(as.character(20101018),"%Y%m%d") - 25
t2 <- as.Date(as.character(20101018),"%Y%m%d") + 10
current_Stock <- getSymbols("AAPL", src="yahoo", from=t1, to=t2, auto.assign = FALSE)

### Tentative plot to read parameter
chartSeries(current_Stock, name = "AAPL", TA = NULL)

### preparation of main plot data (I used 2010-10-15 as an interest day)
no_axis <- x <- chartSeries(current_Stock, name = "AAPL", TA = "add_ta", plot=F)

### make plot without x-axis
no_axis@x.labels <- "a"                 # throw something to x.labels to cause error.
quantmod:::chartSeries.chob(no_axis)    # The error suppresses x-axis

### make x-axis (I used "x@colors\$border" but original is "x@colors\$major.tick")
interest <- which(strptime(x@x.labels, "%b %d %Y") == "2010-10-15")  # index of the day

par(mar = c(3.5, 3.5, 0, 3))   # I modified lab.position a little by padj.
axis(1, at = (1 + x@bp * x@spacing - x@spacing)[-interest], padj = 0.4,
labels = (format(strptime(x@x.labels, "%b %d %Y"), "%b %d"))[-interest],
las = 2, mgp = c(3, 0.7, 0), col = x@colors\$border, col.axis = x@colors\$border)
axis(1, at = (1 + x@bp * x@spacing - x@spacing)[interest], padj = 0.4,
labels = (format(strptime(x@x.labels, "%b %d %Y"), "%b %d"))[interest],
las = 2, mgp = c(3, 0.7, 0), col = x@colors\$border, col.axis = "gray", font.axis=2)
``````

Source (Stackoverflow)