股票蜡烛图绘制与geom_boxplot(R)问题 [英] Stock candlestick drawing issues with geom_boxplot (R)

查看:513
本文介绍了股票蜡烛图绘制与geom_boxplot(R)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 geom_boxplot 来使用股票市场数据绘制烛台。问题在于,单个箱形图的上边缘和下边缘以及上部晶须端点在y轴上显示出高于其对应值的方式。虽然每个箱形图的下部晶须的相对高度(上下边缘之间的差值)和终点是很好的。这里是我的代码:

$ p $ lt; code> candlestickPlot< - function(x){

library(ggplot2 )

#x是包含'date','open','high','low','close'列的data.frame
x $ candleLower < - pmin(x $打开,x $ close)
x $ candleUpper < - pmax(x $ open,x $ close)
x $ candleMiddle < - NA
x $ fill < - red
x $ fill [x $ open< x $ close =green

#绘制烛台
g < - ggplot(x,aes(x = date,lower = candleLower,middle = candleMiddle,upper = candleUpper,ymin =低,ymax =高))
g < - g + geom_boxplot(stat ='identity',aes(group = date,fill = fill))
g
}

以下是x:

 日期收盘价高开低
5 2013-12-30 25.82 3525026 27.30 27.76 25.7
4 2013-12-31 27.41 5487204 25.25 27.70 25.25
3 2014-01-02 30.70 7835374 29.25 31.24 29.21
2 2014-01-03 30.12 4577278 31.49 31.80 30.08
1 2014-01-06 30.65 4042724 30.89 31.88 30.37

我在这里做错了什么?

解决方案

有更有效的方法来创建使用 ggplot2 的OHLC烛台比您使用 geom_boxplot 描述的方式。您的代码与链接中的示例看起来非常相似:


I am using geom_boxplot to draw candlesticks using stock market data. The problem is that the individual boxplot's upper and lower edges as well as the upper whisker end point show up way higher on the y-axis than their corresponding values. The relative height (difference between upper and lower edges) and the end point of the lower whisker of each boxplot are fine though. Here's my code :

candlestickPlot <- function(x){

library("ggplot2")

# x is a data.frame with columns 'date','open','high','low','close'
x$candleLower <- pmin(x$open, x$close)
x$candleUpper <- pmax(x$open, x$close)
x$candleMiddle <- NA
x$fill <- "red"
x$fill[x$open < x$close] = "green"

# Draw the candlesticks
g <- ggplot(x, aes(x=date, lower=candleLower, middle=candleMiddle, upper=candleUpper, ymin=low, ymax=high)) 
g <- g + geom_boxplot(stat='identity', aes(group=date, fill=fill))
g 
}

Here's x :

    date     close volume  open  high   low
5 2013-12-30 25.82 3525026 27.30 27.76  25.7
4 2013-12-31 27.41 5487204 25.25 27.70 25.25
3 2014-01-02 30.70 7835374 29.25 31.24 29.21
2 2014-01-03 30.12 4577278 31.49 31.80 30.08
1 2014-01-06 30.65 4042724 30.89 31.88 30.37

Am I doing something wrong here?

解决方案

There are more efficient ways to create OHLC candlesticks with ggplot2 than the way you have described using geom_boxplot. Your code seems very similar to the example in the link: http://www.perdomocore.com/2012/using-ggplot-to-make-candlestick-charts-alpha/

It seems many people are putting ggplot candlestick examples on the net that are based on the example in that link using geom_boxplot. But the problem with plotting with geom_boxplot is that the plotting itself gets slow at producing plots as the number of bars plotted increases.

Here is one computationally faster solution for plotting financial data using candlesticks/OHLC bars:

library(ggplot2)
library(quantmod)
FOSL <- getSymbols("FOSL", from="2015-01-01", auto.assign=FALSE)
names(FOSL) <- gsub("^.+\\.","",names(FOSL))  # remove "FOSL." from column names

rng <- "2015-08"
FOSL <- FOSL[rng]
FOSL <- data.frame(Date=as.POSIXct(index(FOSL)), FOSL[,1:4])

FOSL$chg <- ifelse(Cl(FOSL) > Op(FOSL), "up", "dn")
FOSL$width <- as.numeric(periodicity(FOSL)[1])
FOSL$flat_bar <- FOSL[, "High"] == FOSL[, "Low"]

# Candle chart:
pl <- ggplot(FOSL, aes(x=Date))+
  geom_linerange(aes(ymin=Low, ymax=High)) +
  theme_bw() +
  labs(title="FOSL") +
  geom_rect(aes(xmin = Date - width/2 * 0.9, xmax = Date + width/2 * 0.9, ymin = pmin(Open, Close), ymax = pmax(Open, Close), fill = chg)) + guides(fill = FALSE, colour = FALSE) + scale_fill_manual(values = c("dn" = "darkred", "up" = "darkgreen"))

# Handle special case of drawing a flat bar where OHLC = Open:
if (any(FOSL$flat_bar)) pl <- pl + geom_segment(data = FOSL[FOSL$flat_bar,], aes(x = Date - width / 2 * 0.9, y = Close, yend = Close, xend = Date + width / 2 * 0.9))

print(pl)

这篇关于股票蜡烛图绘制与geom_boxplot(R)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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