我正在尝试为视力不佳的人使用更大的字体。
library(quantmod)
getSymbols("SPY", from="2013-11-01", to=Sys.Date())
chart_Series(SPY)
myPars <-chart_pars()
myPars$cex<-1.5
chart1 <- chart_Series(SPY, pars=myPars)
chart1
但是,当我这样做时,只显示y轴数量刻度的一部分。 是否可以移动图表,因此y轴编号比例不会被切断。 谢谢你的帮助。
答案 0 :(得分:6)
当我尝试你的代码时(注意这是在R studio中的R 3.1.0中),y轴数字刻度似乎没有被截断。不过,您可以调整chart_pars()
(因为您已经部分完成)和chart_theme()
来实现您的目标。
为了减少y轴上的拥挤,您可以调整$mar
的边距参数chart_pars()
。增加左(和/或右)边距参数值以消除y轴编号的拥挤。您还可以考虑删除左或右y轴刻度以节省更多空间。这是一个例子,附有解释:
library(quantmod)
getSymbols("SPY", from="2013-11-01", to=Sys.Date())
myPars <- chart_pars()
myPars$mar <- c(3, 2, 0, .2) # default is c(3, 1, 0, 1) # bottom, left, top, right
myPars$cex <- 1.5 #' Increase font size of both x and y axis scale ticks
mychartTheme <- chart_theme()
mychartTheme$rylab = FALSE #' Don't show y-axis on right side of plot to save space
# mychartTheme$lylab = TRUE #' Show y-axis ticks on left side of plot? Default is TRUE for both left and right sides.
chart1 <- chart_Series(SPY, pars=myPars, theme = mychartTheme)
chart1
您从此代码中获得的图表是:
此外,如果您对编辑y轴刻度数字上显示的小数位数感兴趣(例如在FX中以1e-4点数报价的货币),您可以编辑源代码chart_Series在某些行上获得你想要的东西。
例如,仅在LEFT y轴上绘制4个小数位,并将y轴数字的打印偏移到左侧(因此它们不会在左边缘附近的条形图下绘制)你可以编辑chart_Series的第143-147行(使用以下编辑创建chart_Series的副本):
#' my.chart_Series is identical to the definition of chart_Series, with these minor edits
my.chart_Series <- function (x, name = deparse(substitute(x)), type = "candlesticks",
subset = "", TA = "", pars = chart_pars(), theme = chart_theme(),
clev = 0, ...)
{
cs <- new.replot()
....
[lines 143-147]: if (theme$lylab) {
cs$add(expression(text(1 - 1/3 - max(strwidth(alabels)),
alabels, sprintf("%.4f", alabels), #alabels, noquote(format(alabels, justify = "left", digits = 4)),
col = theme$labels, offset = -2, cex = 0.9, pos = 4,
xpd = TRUE)), expr = TRUE)
} #' default offset = 0
....
}
然后在你的R脚本中看到这个效果写成这样的东西:
source('my.chart_Series.R')
environment(my.chart_Series) <- environment(get("chart_Series", envir = asNamespace("quantmod")))
assignInNamespace(x = "chart_Series", value = my.chart_Series, ns = "quantmod")
myPars <- chart_pars()
myPars$mar <- c(3, 3, 0, .2) # default is c(3, 1, 0, 1) # bottom, left, top, right
myPars$cex <- 1.0 #' Increase font size of both x and y axis scale ticks
mychartTheme <- chart_theme()
mychartTheme$rylab = FALSE #' Don't show y-axis on right side of plot to save space
# mychartTheme$lylab = TRUE #' Show y-axis ticks on left side of plot? Default is TRUE for both left and right sides.
chart1 <- quantmod:::chart_Series(SPY, pars=myPars, theme = mychartTheme) #' Note the need to prepend the quantmod namespace to the modified visible chart_Series function in quantmod.
chart1
这将给你这个情节:
另外,为了减少y轴上绘制的刻度数,您还可以修改chart_Series
中的第128行,如下所示:
p <- pretty(ylim, n = 5) #' The original source code (quantmod 0.4-0) is p <- pretty(ylim, 10)
有关R函数n
中pretty
参数的约束,请参阅R帮助文档。这给出了: