R/quantmod: multiple charts all using the same y-axis
With chartSeries
, you can set the layout
argument to NULL
to prevent the layout()
command from being called: this is what disables the mfrow
setting.
library(quantmod)
getSymbols("AA")
op <- par(mfrow=c(3,2))
for(i in 1:6) {
chartSeries(
AA["2011-01"], "candlesticks",
TA=NULL, # No volume plot
layout=NULL,
yrange=c(15,18)
)
}
par(op)
If you want to keep the volume, you can call layout
instead of setting mfrow
: it does basically the same thing, but allows you to have plots of different sizes and choose the order in which they are plotted.
layout( matrix( c(
1, 3,
2, 4,
5, 7,
6, 8,
9, 11,
10, 12
), nc=2, byrow=TRUE),
heights = rep( c(2,1), 3 )
)
#layout.show(12) # To check that the order is as desired
for(i in 1:6) {
chartSeries(
AA[sprintf("2011-%02d",i)],
"candlesticks", layout=NULL, yrange=c(15,19)
)
}
Adding Multiple Chart Series in Quantmod R
You could use chart_Series
instead of chartSeries
.
chart_Series(Cl(data$GLD))
add_TA(Cl(data$GDX), on = 1)
And then if you want RSI below in a sub panel, just add add_RSI()
.
Another approach is to use version >= 0.10.0 of xts
(i.e. don't use quantmod
at all), which you can get from https://github.com/joshuaulrich/xts (0.10.0 is not yet on CRAN). The new plot
function in xts
is very friendly with plotting multiple columns of an xts
object all at once. Check out ?plot.xts
for examples of new functionality.
Edit #2:
To see relative changes more easily, you can normalise your price series in many ways. This is a typical approach (using a 0 origin is what Google charts does):
normalise_series <- function(xdat) xdat / coredata(xdat)[1]
getSymbols("USO")
window <- "2013/"
# Define colour of default chart line to chart_Series in mytheme object
# which is passed to chart_Series:
mytheme <- chart_theme()
mytheme$col$line.col <- "darkgreen"
chart_Series(normalise_series(Cl(data$GLD)[window]) - 1, theme = mytheme)
add_TA(normalise_series(Cl(data$GDX)[window]) - 1, on = 1, col = "red", lty = 3)
add_TA(normalise_series(Cl(USO)[window]) - 1, on = 1, col = "blue", lty =2)
add_TA(RSI(Cl(data$GLD)), on = NA, col = "darkgreen")
add_TA(RSI(Cl(data$GDX)), on = 2, col = "red", lty = 3)
# Or add RSIs on different subpanels to improve readability of charts:
add_TA(RSI(Cl(USO)), on = NA, col = "blue", lty = 2)
get multiple chart using chart_Series of quantmod package
I don't have access to a Bloomberg terminal, so your example is not reproducible for me. My guess is that crcy.ohcl
has missing and/or NaN
values.
You can test with range(crcy.ohcl)
. If the output is [1] NA NA
, then you need to take care of the missing values in your data with something like na.omit(crcy.ohcl)
, na.locf(crcy.ohcl)
, etc.
EDIT: Thanks to GSee, I can replicate this via:
require(quantmod)
data(sample_matrix)
x <- as.xts(sample_matrix)
y <- as.quantmod.OHLC(x, col.names=c("Open", "High", "Low", "Close"))
chart_Series(y) # error
chart_Series(as.xts(y)) # works
So it looks like chart_Series
expects an xts object, but as.quantmod.OHLC
doesn't have an xts
method, so it returns a zoo
object if you pass it a xts
object.
R quantmod chartSeries: Add multiple TA overlay to single chart
Well, on=1
is what you need. I'm having trouble getting the TA="" to work, with the data I had to hand, but this did work:
chartSeries(x,TA=NULL);addTA(EMA(x$Close),on=1)
(The TA=NULL
is to remove the volume chart.)
Or, use newTA
to define your TA with all the desired parameters in advance, and then the TA
argument does accept it:
myEMA = newTA(EMA, Cl, on=1, col=7)
chartSeries(x, TA="myEMA()")
(See ?newTA
, which is where I stole that first line from!)
quantmod barChart (or chartSeries) formatting options
- This functionality isn't available (patches welcome).
- This functionality isn't available (patches welcome).
- This functionality isn't available (patches welcome).
- See the sparse documentation for
?addTA
, specifically theon
argument. - Plot the bottom chart as two separate up/down series, using two different colors, or perhaps
chartTheme
. - Not sure what you mean; just don't plot the bottom chart...
- See the sparse documentation for the
major.ticks
argument tochartSeries
. I don't think you can change the y axis grid line spacings, and the x axis spacing will be the same for the top and bottom chart. - See
?png
and?pdf
.
How to overlay multiple TA in new plot using quantmod?
Here is an option which preserves largely your original code.
You can obtain the desired result using the option on=2
for each TA after the first:
library(quantmod)
getSymbols("PSEC")
price <- Cl(PSEC)
I1 <- SMA(price,3)
I2 <- SMA(price,10)
I3 <- SMA(price,15)
chartSeries(price, TA=list("addTA(I1, col=2)", "addTA(I2, col=4, on=2)",
"addTA(I3, col=5, on=2)"), subset = "last 6 months")
If you want to overlay the price and the SMAs in one chart, you can use the option on=1
for each TA.
Thanks to @hvollmeier who made me realize with his answer that I had misunderstood your question in the previous version of my answer.
PS: Note that several options are described in ?addSMA()
, including with.col
which can be used to select a specific column of the time series (Cl is the default column).
Multiple Charts Same Plot
using layout
, it is easy once you create the right matrix
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1 1 1 1 1 1 1 1 1
[2,] 2 3 4 5 6 7 8 9 10 11
layout(matrix(c(rep(1,10),2:11),nrow=2,byrow=T))
x=c(1,2,3324,324)
y=c(1,2,32,2323)
plot(x,y)
replicate(n=10,plot(x,y))
EDIT give different widths
mat <- matrix(c(rep(1,10),2:11),nrow=2,byrow=T)
layout(mat, widths = c(rep(1,5),rep(2,3),rep(3,2)))
I like the layout.show
function. It is very convenient to control your layout.
layout.show(n = 11)
Overlay of multiple time series in Quantmod in R
The green blob is what you asked R to plot. c(PSEC, ARCC)
is the same as rbind(PSEC, ARCC)
, which creates a single OHLCVA object with two observations per index value (one for PSEC, one for ARCC). You said you tried addTA
, but it "didn't work". Saying something "doesn't work" does not help people help you, especially when you don't even show the code that "doesn't work".
Here's a chartSeries
call that uses addTA
, and a corresponding chart_Series
version. Note that chart_Series
automatically handles the y-axis range for you.
> chartSeries(Cl(PSEC), TA="addTA(Cl(ARCC), on=1)")
> chart_Series(Cl(PSEC), TA="add_TA(Cl(ARCC), on=1)")
Related Topics
Rcmdr Launch Error in Yosemite (Os X 10.10)
How Do Add a Column in a Data Frame in R
Observeevent Shiny Function Used in a Module Does Not Work
Convert Begin and End Coordinates into Spatial Lines in R
Ggplot2 Time Series Plotting: How to Omit Periods When There Is No Data Points
Control Transparency of Smoother and Confidence Interval
Multiple Condition If-Else Using Dplyr, Custom Function, or Purrr
How to Convert .Rdata Format into Text File Format
How to Use Custom Functions in Mutate (Dplyr)
Plotting Survival Curves in R with Ggplot2
How to Fill in the Contour Fully Using Stat_Contour
Generating a Heatmap That Depicts the Clusters in a Dataset Using Hierarchical Clustering in R
Linear Interpolate Missing Values in Time Series
What Are Helpful Optimizations in R for Big Data Sets
Add Rows to Grouped Data with Dplyr