I saw in a book a monochrome barchart where closing prices are connected to next days' opening prices. Kind of like this https://www.trade.education/wp-content/uploads/ohlc-chart-bar-chart.jpg , but the opening and closing prices connected, so that it looks like one continuous line. Is it possible to do it in R? The closest I can come is with barChart
with theme='white.mono'
however opening and closing prices are not connected.
Not entirely clear what you want, but here are some possibilities using 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()
Edit: Response to OPs comment.
Like this?
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()