我正在使用geom_boxplot
使用股票市场数据来绘制烛台。问题是,单个箱形图的上边缘和下边缘以及上胡须端点在y轴上显示的位置比它们的相应值要高得多。不过,每个盒子图的相对高度(上边和下边之间的差异)和下边须的终点都很好。下面是我的代码:
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
}
下面是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
我是不是做错了什么?
发布于 2015-09-21 21:34:24
与使用geom_boxplot
描述的方式相比,使用ggplot2
创建OHLC烛台有更有效的方法。您的代码看起来与链接中的示例非常相似:http://www.perdomocore.com/2012/using-ggplot-to-make-candlestick-charts-alpha/
似乎很多人都在使用geom_boxplot
将基于该链接中的示例的ggplot烛台示例放在网上。但是使用geom_boxplot
绘图的问题是,随着绘图条形图数量的增加,绘图本身在生成绘图时变得很慢。
这里有一个计算速度更快的解决方案,用于使用烛台/OHLC条绘制财务数据:
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)
发布于 2016-12-01 03:29:36
感谢FXQuantTrader为R中的烛台酒吧介绍了一种漂亮而快速的替代方法!太棒了,简明扼要,易于阅读!以下是FXQuantTrader解决方案的一个改进版本,其中包括:
*-将其包装到一个函数中
**-支持较低的分辨率(最低可达1秒条形)
*-将蜡烛的胡须颜色从黑色更改为合适的颜色
== -为带有Close == Open的酒吧添加小水平线
== -将第三种颜色(蓝色)添加到关闭==打开的条形图中
-添加'alpha‘参数,允许您使整个烛台图表更加透明,因此当您在顶部绘制一些布林线和/或移动平均线时,条形图将不会那么分散注意力(更像背景)
她来了:
library(ggplot2)
library(quantmod)
draw_candles <- function(df, title_param, alpha_param = 1){
df$change <- ifelse(df$Close > df$Open, "up", ifelse(df$Close < df$Open, "down", "flat"))
# originally the width of the bars was calculated by FXQuantTrader with use of 'periodicity()', which
# seems to work ok only with: ‘minute’,‘hourly’, ‘daily’,‘weekly’, ‘monthly’,
# ‘quarterly’, and ‘yearly’, but can not do 1 sec bars while we want arbitrary bar size support!-)
# df$width <- as.numeric(periodicity(df)[1])
# So let us instead find delta (seconds) between 1st and 2nd row and just
# use it for all other rows. We check 1st 3 rows to avoid larger "weekend gaps"
width_candidates <- c(as.numeric(difftime(df$Date[2], df$Date[1]), units = "secs"),
as.numeric(difftime(df$Date[3], df$Date[2]), units = "secs"),
as.numeric(difftime(df$Date[4], df$Date[3]), units = "secs"))
df$width_s = min(width_candidates) # one (same) candle width (in seconds) for all the bars
# define the vector of candle colours either by name or by rgb()
#candle_colors = c("down" = "red", "up" = "green", "flat" = "blue")
candle_colors = c("down" = rgb(192,0,0,alpha=255,maxColorValue=255), "up" = rgb(0,192,0,alpha=255,maxColorValue=255), "flat" = rgb(0,0,192,alpha=255,maxColorValue=255))
# Candle chart:
g <- ggplot(df, aes(x=Date))+
geom_linerange(aes(ymin=Low, ymax=High, colour = change), alpha = alpha_param) + # candle whiskerss (vertical thin lines:)
theme_bw() +
labs(title=title_param) +
geom_rect(aes(xmin = Date - width_s/2 * 0.9, xmax = Date + width_s/2 * 0.9, ymin = pmin(Open, Close), ymax = pmax(Open, Close), fill = change), alpha = alpha_param) + # cabdke body
guides(fill = FALSE, colour = FALSE) +
scale_color_manual(values = candle_colors) + # color for line
scale_fill_manual(values = candle_colors) # color for candle fill
# Handle special cases: flat bar and Open == close:
if (any(df$change == "flat")) g <- g + geom_segment(data = df[df$change == "flat",], aes(x = Date - width_s / 2 * 0.9, y = Close, yend = Close, xend = Date + width_s / 2 * 0.9, colour = change), alpha = alpha_param)
#print(g)
g
}
发布于 2014-01-10 16:51:27
我不能完全理解你的问题,但这似乎工作得很好:
http://www.perdomocore.com/2012/using-ggplot-to-make-candlestick-charts-alpha/
https://stackoverflow.com/questions/21045866
复制