chart_Series:如何格式化x轴以删除年份?

时间:2013-10-05 16:39:13

标签: r quantmod

如何从x轴中删除年份,使其仅显示日期和月份?此外,是否可以将x轴日期旋转90度?

library(quantmod)
getSymbols("SPY", from="2013-01-01", to=Sys.Date())
chart_Series(SPY,theme=myTheme)

2 个答案:

答案 0 :(得分:5)

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>

您需要保存属性以便将它们放回原状,然后您需要将format.labels更改为新规范,然后使用axt向量中的名称而不是它们的值。 las参数是基本图形的旋转指示器。见?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

答案 1 :(得分:3)

您可以使用

创建主题
myTheme <- chart_theme()
myTheme$format.labels <- '%b %d'
chart_Series(SPY,theme=myTheme)

那应该给你以下

enter image description here