chart_Series: How to format the x axis to remove t

2019-07-24 19:04发布

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)

标签: r quantmod
2条回答
2楼-- · 2019-07-24 19:26

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

enter image description here

查看更多
爷的心禁止访问
3楼-- · 2019-07-24 19:27
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

enter image description here

查看更多
登录 后发表回答