R quantmod chart_Series:y 轴使用大字体 [英] R quantmod chart_Series: using large fonts for y axis

查看:49
本文介绍了R quantmod chart_Series:y 轴使用大字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为视力不佳的人使用更大的字体.

I am trying to use a larger font size for those who have poor eyesight.

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 轴数字刻度不会被切断.感谢您的帮助.

However, when I do this, only a part of the y axis numbers scale are shown. Is it possible to shift the chart, so the y axis numbers scale are not cut off. Thank you for your help.

推荐答案

当我尝试你的代码时(注意这是在 R studio 的 R 3.1.0 中),y 轴数字刻度似乎没有被切断.不过,您可以调整 chart_pars()(因为您已经部分完成了)和 chart_theme() 以实现您想要的效果.

When I try your code (Note this is in R 3.1.0 in R studio though), the y axis numbers scale doesn't appear to be cut off. Nevertheless, you can adjust the chart_pars() (as you've already partly done) and chart_theme() to achieve what you want.

为了减少 y 轴上的拥挤,您可以调整 chart_pars() 的边距参数 $mar.增加左(和或右)边距参数值以消除 y 轴数字的拥挤.您还可以考虑删除左侧或右侧 y 轴比例以节省更多空间.这是一个示例,并附有说明:

To reduce crowding on the y-axis, you can adjust the margin parameters, $mar, of chart_pars(). Increase the left (and or right) margin parameter values to remove crowding of the y-axis numbers. You can also consider removing either the left or right y-axis scale to save more space. Here's an example, with explanations:

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

您将从这段代码中得到的图是:

The plot you'll get from this code is:

此外,如果您有兴趣编辑 y 轴刻度数字上显示的小数位数(例如,对于以 1e-4 点为单位报价的货币的外汇),您可以在以下位置编辑 chart_Series 的源代码某些行来获得您想要的东西.

Furthermore, in case you're interested in editing the number of decimal places displayed on the y-axis scale numbers (e.g. in FX for currencies quoted in pips at 1e-4), you can edit the source code of chart_Series at certain lines to get what you desire.

例如,仅在左 y 轴上绘制到 4 个小数位,并将 y 轴数字的打印向左偏移(因此它们不会绘制在图左边距附近的条形下方),您可以像这样编辑 chart_Series 的第 143-147 行(使用以下编辑创建 chart_Series 的副本):

For example, to plot to 4 decimal places on the LEFT y-axis only, and offset the printing of the y-axis numbers to the left (so they don't plot under the bars near the left margin of the plot), you could edit lines 143-147 of chart_Series like so (create a copy of chart_Series with the following edits):

    #' 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 脚本中看到这个效果,这样写:

And then in your R script to see this effect write something like this:

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) #' 原始源码(quantmod 0.4-0)是 p <-pretty(ylim, 10)有关 R 函数 prettyn 参数的约束,请参阅 R 帮助文档.这给出:

Also, to reduce the number of ticks drawn on the y axis, you can also modify line 128 in chart_Series like so: p <- pretty(ylim, n = 5) #' The original source code (quantmod 0.4-0) is p <- pretty(ylim, 10) See the R help documentation for constraints on the n argument in the R function pretty. This gives:

这篇关于R quantmod chart_Series:y 轴使用大字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆