Y 轴在 R 中使用 Chartseries 截断 [英] Y Axis is cut off using Chartseries in R

查看:36
本文介绍了Y 轴在 R 中使用 Chartseries 截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 chartSeries,但 Y 轴被切断了.我想把右边的价格扩展到小数点后两位.这似乎是边距或字体大小的问题,但在四处搜索后,我找不到任何地方可以调整这些选项.我说边距是因为图表左侧似乎有足够的空间.

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.

有什么想法吗?谢谢.

以下是上图的可重现代码:

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") )

推荐答案

我认为没有办法在不修改源代码的情况下做你想做的事.这是实现您想要的一种方式:

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:

从 quantmod 命名空间中修改 chartSeries 代码.您需要修改的代码区域在一个名为 chartSeries.chob

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"))

在函数内部编辑第 7-8、19 和 117 行.

Inside the function edit lines 7-8, 19, and line 117.

特别是:

--第 7-8 行:修改列表中每个向量的第 4 个元素.这控制了右边距的边距宽度(第一个元素更改底部边距宽度,第二个元素更改左侧,第三个元素更改顶部边距).您可以使用 4 而不是 3,如下所示:

-- 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)))

--第 19 行:将第 4 个元素更改为 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))

当您只绘制价格图而没有在下面绘制任何其他 TA(例如交易量、RSI 等)时,第 19 行的代码是必要的

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)

--现在,第 117 行表示 axis(4).

-- Now, line 117 says axis(4).

改成:

axis(side = 4, at = axTicks(2), labels = sprintf("%.2f", axTicks(2)))

评论:- 参数侧可以是 1, 2, 3, 4,它编辑轴刻度标签的位置.如果您尝试 side = 2,它会将数字放在左边距而不是右边(与左侧的任何 TA 刻度标签对齐).

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).

  • at 参数可让您指定刻度标签的位置.axTicks(2) 是 R 为您拥有的给定数据集设置的默认间距——如果您想更改这些值,您可以在您的 y 范围内执行任何一组数字绘图(例如,您可以尝试 at = c(544.345, 545.05),当您的绘图打印时,您只会看到两个刻度标签).

  • 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 try at = c(544.345, 545.05) and you will see just two tick labels when your plot prints).

labels 参数是一个字符向量(与 at 向量的长度相同),它允许您选择要在由 at 参数指定的位置打印的任何内容.我试图给你一个有用的标签:labels = sprintf("%.2f", axTicks(2)).这使刻度标签保留两位小数(您可以将其更改为 EURUSD 等主要货币对的小数点后 4 位).

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).

保存这些更改.您只需键入 quantmod::chartSeries.chob(您应该看到您的代码修改 - 如果没有,则说明有问题),即可检查是否对当前 R 会话进行了永久性更改.

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).

现在,按照通常的方式重新运行您的 chartSeries 函数:

Now, rerun your chartSeries function in the way you normally would:

    chartSeries(data, type = "bars", theme = chartTheme("white") )

来自:

如何获取assignInNamespace工作

如果您想自动更改 chartSeries.chob 函数(不使用编辑窗口),您可以执行以下操作:

If you want to automate changes to the chartSeries.chob function (without using the edit window) you could do the following:

编辑 chartSeries.chob 函数,在修改后的版本中进行您想要的任何更改.即

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]
}

然后运行这些行:

environment(chartSeries.chob2) <- environment(get("chartSeries.chob", envir = asNamespace("quantmod")))
assignInNamespace(x = "chartSeries.chob", value = chartSeries.chob2, ns = "quantmod")

您的 chartSeries 图现在应该在您的修改下运行(在您当前的 R 会话中).

Your chartSeries plots should run with your modifications now (in your current R session).

编辑 2:

另一种无需编辑 quantmod 源代码即可实现所需内容的方法,可以通过使用 chart_Series(.) 并遵循此答案中的想法来实现R quantmod chart_Series:使用大字体y轴

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

这篇关于Y 轴在 R 中使用 Chartseries 截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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