Graphics

This is a sample for plotting with R.

[1]:
library(quantmod)
# initialization
symbols = c("AMZN", "FB", "NFLX", "MSFT")
start <- as.Date("2017-01-01")
end <- as.Date("2020-01-01")
getSymbols(Symbols = symbols, src = "yahoo", from = start, to = end)
# get only Close values of AMZN and MSFT symbols
stocks <- as.xts(data.frame(AMZN = AMZN[,"AMZN.Close"], MSFT = MSFT[,"MSFT.Close"]))
Loading required package: xts

Loading required package: zoo


Attaching package: ‘zoo’


The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric


Loading required package: TTR

Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo

‘getSymbols’ currently uses auto.assign=TRUE by default, but will
use auto.assign=FALSE in 0.5-0. You will still be able to use
‘loadSymbols’ to automatically load data. getOption("getSymbols.env")
and getOption("getSymbols.auto.assign") will still be checked for
alternate defaults.

This message is shown once per session and may be disabled by setting
options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.


  1. 'AMZN'
  2. 'FB'
  3. 'NFLX'
  4. 'MSFT'
[2]:
# plotting
library(plotly)
plot(AMZN[,"AMZN.Close"], main = "AMZN") # prints linear graph
Loading required package: ggplot2


Attaching package: ‘plotly’


The following object is masked from ‘package:ggplot2’:

    last_plot


The following object is masked from ‘package:stats’:

    filter


The following object is masked from ‘package:graphics’:

    layout


../_images/r_graphics_2_1.png
[3]:
# create a custom theme
my_theme <- chart_theme()
my_theme$col$up.col <- "darkgreen"
my_theme$col$up.border <- "black"
my_theme$col$dn.col <- "darkred"
my_theme$col$dn.border <- "black"
my_theme$rylab <- FALSE
my_theme$col$grid <- "lightgrey"
[4]:
# using the custom theme with a range
chart_Series(AMZN, subset = "2017-10::2018-09", theme = my_theme)
../_images/r_graphics_4_0.png
[5]:
# using the custom theme with a range of one month
chart_Series(AMZN, subset = "2018-09", theme = my_theme)
../_images/r_graphics_5_0.png
[6]:
# using the black theme of quantmod
barChart(AMZN, theme = chartTheme('black'))
../_images/r_graphics_6_0.png
[7]:
# using the candle theme of quantmod
candleChart(AMZN, up.col = "green", dn.col = "red", theme = "white")
../_images/r_graphics_7_0.png
[8]:
# zoom of graph on one month
zoomChart("2018-09")
../_images/r_graphics_8_0.png
[9]:
# zoom of graph on one year
zoomChart("2018")
../_images/r_graphics_9_0.png
[10]:
# add indicators
addSMA(n = c(20, 50, 200)) # adds simple moving averages
../_images/r_graphics_10_0.png
[11]:
addBBands(n = 20, sd = 2, ma = "SMA", draw = "bands", on = -1) # sd = standard deviation, ma = average
../_images/r_graphics_11_0.png
[12]:
AMZN_ema_21 <- EMA(Cl(AMZN), n=21) # exponencial moving average
addTA(AMZN_ema_21, on = 1, col = "red")
../_images/r_graphics_12_0.png
[13]:
AMZN_ema_50 <- EMA(Cl(AMZN), n=50) # exponencial moving average
addTA(AMZN_ema_50, on = 1, col = "blue")
../_images/r_graphics_13_0.png
[14]:
AMZN_ema_200 <- EMA(Cl(AMZN), n=200) # exponencial moving average
addTA(AMZN_ema_200, on = 1, col = "orange")
../_images/r_graphics_14_0.png
[15]:
addRSI(n = 14, maType = "EMA", wilder = TRUE) # relative strength index
../_images/r_graphics_15_0.png