geom_chart | R Documentation |
Financial charts provide visual cues to open, high, low, and close prices.
Use coord_x_date()
to zoom into specific plot regions.
The following financial chart geoms are available:
geom_barchart(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
colour_up = "darkblue",
colour_down = "red",
fill_up = "darkblue",
fill_down = "red",
...
)
geom_candlestick(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
colour_up = "darkblue",
colour_down = "red",
fill_up = "darkblue",
fill_down = "red",
...
)
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this
layer, either as a |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
colour_up, colour_down |
Select colors to be applied based on price movement
from open to close. If close >= open, |
fill_up, fill_down |
Select fills to be applied based on price movement
from open to close. If close >= open, |
... |
Other arguments passed on to |
The following aesthetics are understood (required are in bold):
x
, Typically a date
open
, Required to be the open price
high
, Required to be the high price
low
, Required to be the low price
close
, Required to be the close price
alpha
group
linetype
size
See individual modeling functions for underlying parameters:
geom_ma()
for adding moving averages to ggplots
geom_bbands()
for adding Bollinger Bands to ggplots
coord_x_date()
for zooming into specific regions of a plot
# Load libraries
library(tidyquant)
library(dplyr)
library(ggplot2)
AAPL <- tq_get("AAPL", from = "2013-01-01", to = "2016-12-31")
# Bar Chart
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_barchart(aes(open = open, high = high, low = low, close = close)) +
geom_ma(color = "darkgreen") +
coord_x_date(xlim = c("2016-01-01", "2016-12-31"),
ylim = c(75, 125))
# Candlestick Chart
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
geom_ma(color = "darkgreen") +
coord_x_date(xlim = c("2016-01-01", "2016-12-31"),
ylim = c(75, 125))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.