问题
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. Is it possible to shift the chart, so the y axis numbers scale are not cut off. Thank you for your help.
回答1:
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.
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:
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.
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
....
}
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
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
. This gives:
来源:https://stackoverflow.com/questions/20337851/r-quantmod-chart-series-using-large-fonts-for-y-axis