问题
I am using chartSeries and the Y axis is getting cut off. I'd like the price on the right to extend to 2 decimal places. It seems to be an issue of margins or font size, but after doing some searching around, I can't find anywhere to adjust these options. I say margins since there seems to be plenty of space on the left hand side of the chart.
Any ideas? Thanks.
Here is reproducible code for the above chart:
require (zoo)
require(quantmod)
data <- structure(list(Date = structure(list(sec = c(0, 0, 0, 0, 0, 0
), min = 0:5, hour = c(15L, 15L, 15L, 15L, 15L, 15L), mday = c(3L,
3L, 3L, 3L, 3L, 3L), mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(114L,
114L, 114L, 114L, 114L, 114L), wday = c(5L, 5L, 5L, 5L, 5L, 5L
), yday = c(2L, 2L, 2L, 2L, 2L, 2L), isdst = c(0L, 0L, 0L, 0L,
0L, 0L)), .Names = c("sec", "min", "hour", "mday", "mon", "year",
"wday", "yday", "isdst"), class = c("POSIXlt", "POSIXt")), Open = c(544.95,
544.8, 544.84, 544.8, 544.75, 544.78), High = c(545.1, 544.89,
544.9, 544.8, 544.8, 545.03), Low = c(544.8, 544.77, 544.79,
544.6, 544.66, 544.76), Close = c(544.86, 544.79, 544.8, 544.69,
544.75, 545.01)), .Names = c("Date", "Open", "High", "Low", "Close"
), row.names = 330:335, class = "data.frame")
data$Date <- as.POSIXct(strptime(data$Date, format = "%Y-%m-%d %H:%M:%S"))
data <- read.zoo(data, FUN=as.POSIXct)
chartSeries(data, type = "bars", theme = chartTheme("white") )
回答1:
I don't think there is a way to do what you want without modifying the source code. This is one way to achieve what you want:
Modify the chartSeries code from within the quantmod namespace. The code area you need to modify is in a function called chartSeries.chob
fixInNamespace(x = "chartSeries.chob", pos = as.environment("package:quantmod"))
Inside the function edit lines 7-8, 19, and line 117.
Specifically:
-- lines 7-8: Modify the 4th element of each vector in the list. This controls the margin width for the right margin ( the first element changes the bottom margin width, the second element the left, the third the top margin). You could use 4 instead of 3, like so:
par.list <- list(list(mar = c(0, 3.5, 2, 4)), list(mar = c(0,
3.5, 0, 4)), list(mar = c(3.5, 3.5, 0, 4)))
-- line 19: change the 4th element to say 4 to pull in the left margin, e.g.:
else par(mar = c(3.5, 3.5, 2, 4))
The code on line 19 is necessary when you're just doing a price plot without any additional TAs plotted below, (such as volume, RSI, etc)
--
Now, line 117 says axis(4)
.
Change it to say:
axis(side = 4, at = axTicks(2), labels = sprintf("%.2f", axTicks(2)))
Comments: - the argument side can be 1, 2, 3, 4 which edits where the axis tick labels go. If you try side = 2, it'll put the numbers on the left margin instead of the right (lining up with any TA tick labels on the left side).
The
at
argument lets you specify where your tick labels will go.axTicks(2)
is the default spacing set by R for the given data set you have -- if you want to change these values you can, do any set of numbers that are within the y range of your plot (e.g. you could tryat = c(544.345, 545.05)
and you will see just two tick labels when your plot prints).The
labels
argument is a character vector (same lenght as the at vector) which lets you chose whatever you want to be printed at the locations specified by your at argument. I've tried to give you a helpful label:labels = sprintf("%.2f", axTicks(2))
. This gives the tick labels to two decimal places (you could change this to say 4 decimal places for a major currency pair like EURUSD etc).
Save these changes. You can check that the changes were permanently made for the current R session by just typing quantmod:::chartSeries.chob
(you should see your code modifications - if you don't, something is wrong).
Now, rerun your chartSeries function in the way you normally would:
chartSeries(data, type = "bars", theme = chartTheme("white") )
EDIT:
From:
how to get assignInNamespace to work
If you want to automate changes to the chartSeries.chob function (without using the edit window) you could do the following:
Edit the chartSeries.chob
function making whatever changes you want, in a modified version. i.e.
chartSeries.chob2 <- function (x)
{
old.par <- par(c("pty", "mar", "xpd", "bg", "xaxs", "las",
"col.axis", "fg"))
on.exit(par(old.par))
LAYOUT <- ifelse(is.null(x@layout), FALSE, TRUE)
....[add whatever changes]
}
Then run these lines:
environment(chartSeries.chob2) <- environment(get("chartSeries.chob", envir = asNamespace("quantmod")))
assignInNamespace(x = "chartSeries.chob", value = chartSeries.chob2, ns = "quantmod")
Your chartSeries plots should run with your modifications now (in your current R session).
EDIT 2:
Another way of achieving what you want, without editing the quantmod source code, can be achieved by using chart_Series(.)
and following the ideas in this answer
R quantmod chart_Series: using large fonts for y axis
来源:https://stackoverflow.com/questions/23032224/y-axis-is-cut-off-using-chartseries-in-r