创建连接开盘价和收盘价的条形图
Creating a barchart connecting opening and closing prices
我在一本书中看到一个单色条形图,其中收盘价与第二天的开盘价相关联。有点像这样 https://www.trade.education/wp-content/uploads/ohlc-chart-bar-chart.jpg ,但是开盘价和收盘价是相连的,所以它看起来像一条连续的线。有可能在 R 中做到吗?我能得出的最接近的是 barChart
和 theme='white.mono'
但是开盘价和收盘价没有关联。
您 post 中的 link 显示了一个简单的条形图。这是 quantmod
.
中的标准显示
您可能指的是 Kagi charts,它们的构造方式不同。它们代表具有特定步骤的连续线。这些步骤看起来好像一天的收盘价与第二天的开盘价相连,但这种解释是错误的。这不仅仅是一个图形问题,它们是完全不同的图表类型,没有间隙。
我不知道 quantmod
包中提供了 Kagi 图表类型,我建议您不要使用它们,除非您确定它们表示数据的方式。
不完全清楚你想要什么,但这里有一些使用 ggplot
的可能性。
# grab sample data
library(quantmod)
aapl <- getSymbols("AAPL", from="2015-06-01",auto.assign=FALSE)
names(aapl) <- sub("[^\.]+\.","",names(aapl)) # remove `AAPL.` from column names
df <- data.frame(date=index(aapl),aapl) # ggplot needs a data.frame
# basic OHLC plot
library(ggplot2)
library(scales) # for date_format
ggplot(df, aes(x=date))+
geom_linerange(aes(ymin=Low, ymax=High, color=ifelse(Close>Open,"Gain","Loss")))+
geom_segment(aes(xend=date-0.3, y=Open, yend=Open))+
geom_segment(aes(xend=date+0.3, y=Close, yend=Close))+
scale_color_manual(guide="none",values=c(Gain="green", Loss="red"))+
scale_x_date(labels=date_format("%b-%Y"))+
labs(x="",y="", title="AAPL")+
theme_bw()
# Intraday/Overnight plot
library(reshape2) # for melt(...)
df.melt <- melt(subset(df,select=c(date,Open,Close)),id="date",value.name="price")
df.melt <- df.melt[order(df.melt$date),]
ggplot(df.melt, aes(x=date, y=price)) +
geom_line()+
scale_x_date(labels=date_format("%b-%Y"))+
labs(x="",y="", title="AAPL")+
theme_bw()
# same, color coded
ggplot(df.melt, aes(x=date, y=price)) +
geom_line(aes(color=ifelse(c(diff(price),NA)>0,"Gain","Loss"), group=NA))+
scale_color_manual(guide="none",values=c(Gain="Green", Loss="Red"))+
scale_x_date(labels=date_format("%b-%Y"))+
labs(x="",y="", title="AAPL")+
theme_bw()
编辑:回复 OP 评论。
像这样?
df.melt <- melt(subset(df,select=c(date,Open,High,Low,Close)),id="date",value.name="price")
df.melt <- df.melt[order(df.melt$date),]
ggplot(df.melt, aes(x=date, y=price)) +
geom_line()+
scale_x_date(labels=date_format("%b-%Y"))+
labs(x="",y="", title="AAPL")+
theme_bw()
我在一本书中看到一个单色条形图,其中收盘价与第二天的开盘价相关联。有点像这样 https://www.trade.education/wp-content/uploads/ohlc-chart-bar-chart.jpg ,但是开盘价和收盘价是相连的,所以它看起来像一条连续的线。有可能在 R 中做到吗?我能得出的最接近的是 barChart
和 theme='white.mono'
但是开盘价和收盘价没有关联。
您 post 中的 link 显示了一个简单的条形图。这是 quantmod
.
您可能指的是 Kagi charts,它们的构造方式不同。它们代表具有特定步骤的连续线。这些步骤看起来好像一天的收盘价与第二天的开盘价相连,但这种解释是错误的。这不仅仅是一个图形问题,它们是完全不同的图表类型,没有间隙。
我不知道 quantmod
包中提供了 Kagi 图表类型,我建议您不要使用它们,除非您确定它们表示数据的方式。
不完全清楚你想要什么,但这里有一些使用 ggplot
的可能性。
# grab sample data
library(quantmod)
aapl <- getSymbols("AAPL", from="2015-06-01",auto.assign=FALSE)
names(aapl) <- sub("[^\.]+\.","",names(aapl)) # remove `AAPL.` from column names
df <- data.frame(date=index(aapl),aapl) # ggplot needs a data.frame
# basic OHLC plot
library(ggplot2)
library(scales) # for date_format
ggplot(df, aes(x=date))+
geom_linerange(aes(ymin=Low, ymax=High, color=ifelse(Close>Open,"Gain","Loss")))+
geom_segment(aes(xend=date-0.3, y=Open, yend=Open))+
geom_segment(aes(xend=date+0.3, y=Close, yend=Close))+
scale_color_manual(guide="none",values=c(Gain="green", Loss="red"))+
scale_x_date(labels=date_format("%b-%Y"))+
labs(x="",y="", title="AAPL")+
theme_bw()
# Intraday/Overnight plot
library(reshape2) # for melt(...)
df.melt <- melt(subset(df,select=c(date,Open,Close)),id="date",value.name="price")
df.melt <- df.melt[order(df.melt$date),]
ggplot(df.melt, aes(x=date, y=price)) +
geom_line()+
scale_x_date(labels=date_format("%b-%Y"))+
labs(x="",y="", title="AAPL")+
theme_bw()
# same, color coded
ggplot(df.melt, aes(x=date, y=price)) +
geom_line(aes(color=ifelse(c(diff(price),NA)>0,"Gain","Loss"), group=NA))+
scale_color_manual(guide="none",values=c(Gain="Green", Loss="Red"))+
scale_x_date(labels=date_format("%b-%Y"))+
labs(x="",y="", title="AAPL")+
theme_bw()
编辑:回复 OP 评论。
像这样?
df.melt <- melt(subset(df,select=c(date,Open,High,Low,Close)),id="date",value.name="price")
df.melt <- df.melt[order(df.melt$date),]
ggplot(df.melt, aes(x=date, y=price)) +
geom_line()+
scale_x_date(labels=date_format("%b-%Y"))+
labs(x="",y="", title="AAPL")+
theme_bw()