gpt4 book ai didi

r - OHLC 图表的 GGPlot 使用条形图,而不是蜡烛图

转载 作者:行者123 更新时间:2023-12-03 20:20:36 25 4
gpt4 key购买 nike

我正在 R 中学习 ggplot 并试图创建一个股票图表。我已经能够创建烛台图,现在我想制作条形图。这包括一条从低价到高价的垂直线。然后,线的左侧是开盘价的刻度,右侧是收盘价的刻度。我不知道如何添加这个勾号。我能想到的最接近的东西是 geom_errorbar,但这完全是另一回事, mustache 是双向的。

此外,由于这是每日数据,ggplot 为周末留出了不必要的空间。有没有办法去除这些空格?我在搜索的任何轴格式文章中都没有看到它。

谢谢。!

FOSL chart

用于创建带有低高条的图表的可重现代码。

library(ggplot2)
library(quantmod)
FOSL <- getSymbols("FOSL", from="2015-01-01", auto.assign=FALSE)
names(FOSL) <- gsub("^.+\\.","",names(FOSL)) # remove "FOSL." from column names
FOSL <- data.frame(Date=as.Date(index(FOSL)), FOSL[,1:4])

ggplot(FOSL, aes(x=Date))+
geom_linerange(aes(ymin=Low, ymax=High)) +
labs(title="FOSL")

最佳答案

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

# Bar chart:

ggplot(data=FOSL, aes(x=Date, colour = chg)) +
theme_bw() +
geom_linerange(aes(ymin=Low, ymax=High)) +
geom_segment(aes(y = Open, yend = Open, xend = Date - width / 2 )) +
geom_segment(aes(y = Close, yend = Close, xend = Date + width / 2)) +
scale_colour_manual(values = c("dn" = "darkred", "up" = "darkgreen")) + guides(colour = FALSE)

enter image description here

对于像 scale_x_datetime 这样的 x 轴刻度的 ggplot 图,周末的空间不能轻易去除。 .这是因为 ggplot 将 POSIXct 和日期数据解释为来自 x-axis 上原点的数字。 .例如as.numeric(as.POSIXct("2015-09-25")) 将是 ggplot 中 x 轴上的值。对于围绕此的“黑客”,您可以查看此 SO 答案,其中日期被视为因素:

R + ggplot2: how to hide missing dates from x-axis?

**编辑 2016-05,从 ggplot 时间序列图中删除周末间隔 **

这是一个绘制 ggplot OHLC 条的工作示例,该条删除了周末(此函数很容易修改以绘制蜡烛而不是条 example here)。
library(ggplot2)
library(quantmod)
library(lubridate)
# prepare data:
md <- getSymbols("FOSL", from="2015-01-01", auto.assign=FALSE)
names(md) <- gsub("^.+\\.","",names(md)) # remove "md." from column names
rng <- "2015-08"
md <- md[rng]
# Use ceiling date to ensure "Date" is rounded to 00:00:00 in POSIXct numeric scale from 1970, not Date scale.
md <- data.frame(Date = ceiling_date(as.POSIXct(index(md)), "day"), md[,1:4])
md$time_idx <- 1:NROW(md)



# Check special case of drawing of flat bars:
md[10, 2:5] <- md[10, 5]

gg_build_candlechart_without_weekends <- function(md, # in data.frame format, with a Date column
x_scale = 0.8, # Controls thickness of bars
width = 1
)
{
stopifnot("Date" %in% colnames(md))
md$chg <- ifelse(Cl(md) > Op(md), "up", "dn")
md$time_idx <- 1:NROW(md)
md$width <- width

pl <- ggplot(md, aes(x = time_idx, colour = chg)) +
geom_linerange(aes(ymin=Low, ymax=High)) +
geom_segment(aes(y = Open, yend = Open, xend = time_idx - width / 2 * x_scale)) +
geom_segment(aes(y = Close, yend = Close, xend = time_idx + width / 2 * x_scale)) +
scale_colour_manual(values = c("dn" = "darkred", "up" = "darkgreen")) + guides(colour = FALSE) + labs(x = "Time", y = "Price")

Nr <- NROW(md)
maj_breaks <- round(Nr * c(0.05, 0.25, 0.5, 0.75, .95))

periodicity <- quantile(diff(as.numeric(md$Date)), 0.5)
if (periodicity < 86400) {
label_timestamp <- "%b %d %H:%M:%S"
} else {
label_timestamp <- "%y' %b %d"
}


pl <- pl + scale_x_continuous(limits = c(0, Nr + 1), expand = c(0, 0), breaks = maj_breaks, labels = format(md$Date[maj_breaks], label_timestamp), minor_breaks = Nr * seq(0.1, 0.9, by = 0.2))

# draw flat bars as special case:
md_flatbars <- md[md$High == md$Low, ]
if (NROW(md_flatbars) > 0) {
pl <- pl + geom_segment(data = md_flatbars, aes(x = time_idx - width / 2 * x_scale, y = Close, yend = Close, xend = time_idx + width / 2 * x_scale))
}

pl
}

gg_build_candlechart_without_weekends(md, x_scale = 0.8, width = 1)

No weekend gaps

关于r - OHLC 图表的 GGPlot 使用条形图,而不是蜡烛图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28201587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com