问题
How can I remove the Year from the x axis so that it only shows the day and month? Also, is it possible to rotate the x axis dates by 90 degrees?
library(quantmod)
getSymbols("SPY", from="2013-01-01", to=Sys.Date())
chart_Series(SPY,theme=myTheme)
回答1:
cspy <- chart_Series(SPY )
cspy$Env$actions[[3]]
#------------------
expression(axt <- axTicksByTime(xdata[xsubset], format.labels = format.labels),
axis(1, at = axt, labels = names(axt), las = 1, lwd.ticks = 1,
mgp = c(3, 1.5, 0), tcl = -0.4, cex.axis = 0.9))
attr(,"frame")
[1] 1
attr(,"clip")
[1] TRUE
attr(,"env")
<environment: 0x11cddc148>
You need to save the attributes so they can be put back in and you need to change the format.labels to your new specifications and then use the names from the axt vector rather than their value. The las parameter is the rotation indicator for base graphics. See ?par:
attrs <- attributes(cspy$Env$actions[[3]])
cspy$Env$actions[[3]] <-
expression(axt <- axTicksByTime(xdata[xsubset], format.labels = "%b %d"),
axis(1, at = axt, labels = names(axt), las = 2, lwd.ticks = 1,
mgp = c(3, 1.5, 0), tcl = -0.4, cex.axis = 0.9))
attributes(cspy$Env$actions[[3]]) <- attrs
cspy
回答2:
You can do create your theme using
myTheme <- chart_theme()
myTheme$format.labels <- '%b %d'
chart_Series(SPY,theme=myTheme)
That should give you following
来源:https://stackoverflow.com/questions/19200119/chart-series-how-to-format-the-x-axis-to-remove-the-year