简体   繁体   English

R quantmod chart_Series:对y轴使用大字体

[英]R quantmod chart_Series: using large fonts for y axis

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 

However, when I do this, only a part of the y axis numbers scale are shown. 但是,当我这样做时,只显示y轴数字刻度的一部分。 Is it possible to shift the chart, so the y axis numbers scale are not cut off. 是否可以移动图表,因此y轴编号比例不会被切断。 Thank you for your help. 谢谢您的帮助。

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. 当我尝试你的代码时(注意这是在R studio中的R 3.1.0中),y轴数字刻度似乎没有被截断。 Nevertheless, you can adjust the chart_pars() (as you've already partly done) and chart_theme() to achieve what you want. 不过,您可以调整chart_pars() (因为您已经部分完成)和chart_theme()来实现您想要的效果。

To reduce crowding on the y-axis, you can adjust the margin parameters, $mar , of chart_pars() . 要减少y轴上的拥挤,可以调整chart_pars()的边距参数$mar Increase the left (and or right) margin parameter values to remove crowding of the y-axis numbers. 增加左(和/或右)边距参数值以消除y轴编号的拥挤。 You can also consider removing either the left or right y-axis scale to save more space. 您还可以考虑删除左或右y轴刻度以节省更多空间。 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: 您将从此代码中获得的图表是: 在此输入图像描述

Furthermore, in case you're interested in editing the number of decimal places displayed on the y-axis scale numbers (eg 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轴刻度数字上显示的小数位数(例如在FX中以1e-4点的点数引用的货币),您可以在某些行编辑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): 例如,要仅在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
  ....
}

And then in your R script to see this effect write something like this: 然后在你的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

This will give you this plot: 这将给你这个情节:

在此输入图像描述

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 . 此外,为了减少在y轴上绘制的刻度数,您还可以修改chart_Series第128行, chart_Series所示: p <- pretty(ylim, n = 5) #' The original source code (quantmod 0.4-0) is p <- pretty(ylim, 10)请参阅有关的约束将R帮助文档n在R函数的参数pretty This gives: 这给出了: 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM