How to show gaps in R/quantmod's chartSeries/candleChart plots - r

I am trying to show "gaps" in financial data using the plotting functions in the excellent quantmod package for R.
Normally R allows you to show gaps in plots using NA values, as with:
x<-1:10
y<-2*x
y[4:7]<-NA
plot(x,y,type="l")
I would like to do something similar with R/quantmod's candleChart plots. However, rows of data containing NA's are removed before plotting (there is a na.omit command in the chartSeries code that does this) so I cannot see how to do this.
An example is:
require(quantmod)
#Make some pretend data
x<-0:30
y<-100+20*sin(x)
y.open<-y[-length(y)]
y.close<-y[-1]
val<-as.xts(cbind(y.open,y.open+5,y.close-5,y.close,1000),order.by=as.POSIXct(paste("2011-01-",x[-1],sep='')))
colnames(val)<-c("Open","High","Low","Close","Volume")
#Plot this pretend data
candleChart(val,theme="white")
#Now try and make a "gap" in the middle of the data and plot it
val2<-val
val2[5:20,]<-NA
candleChart(val2,theme="white")
What is the "correct" way to do this? I guess I could overwrite chartSeries with my own version of this function (identical but without the na.omit() call), but that seems quite drastic.
Is there perhaps an option to do this kind of thing available? I have been unable to google anything useful...
Thanks,
fttb

The answer is not to use chartSeries, but rather the newer variant (still in development technically) chart_Series. Note the underscore.
chart_Series(val2)
If you're looking for more details on quantmod and using R in finance, we are hosting a large conference in Chicago at the end of this month. More info can be found here: R/Finance 2011
Hope that helps, and hope to see you in Chicago!!

Related

R plot data.frame to get more effective overview of data

At work when I want to understand a dataset (I work with portfolio data in life insurance), I would normally use pivot tables in Excel to look at e.g. the development of variables over time or dependencies between variables.
I remembered from university the nice R-function where you can plot every column of a dataframe against every other column like in:
For the dependency between issue.age and duration this plot is actually interesting because you can clearly see that high issue ages come with shorter policy durations (because there is a maximum age for each policy). However the plots involving the issue year iss.year are much less "visual". In fact you cant see anything from them. I would like to see with once glance if the distribution of issue ages has changed over the different issue.years, something like
where you could see immediately that the average age of newly issue policies has been increasing from 2014 to 2016.
I don't want to write code that needs to be customized for every dataset that I put in because then I can also do it faster manually in Excel.
So my question is, is there an easy way to plot each column of a matrix against every other column with more flexible chart types than with the standard plot(data.frame)?
The ggpairs() function from the GGally library. It has a lot of capability for visualizing columns of all different types, and provides a lot of control over what to visualize.
For example, here is a snippet from the vignette linked to above:
data(tips, package = "reshape")
ggpairs(tips)

Adding additional y-axis on time series data formulated by xts package

I know the similar ones have been discussed once again. But my question is on the wholly new scenario. And I applied these old methods, which does not work for me.
I use Rstudio 0.97551 under Windows XP.
I used xts package to draw multiple (usually two) time series into one plot and it works fine. However, it seems thatxts() and xtsExtra() did not support the normal plot functions in R and I cannot use usual commands to add additional y-axis on it.
My time series data is some finance data with irregular time intervals and that's the reason I feel using specific time series package to plot data would be better.
Here's the sample code and I use the data Canada under library vars to ensure it is replicable:
library(timeSeries)
library(timeDate)
library(xts)
library(xtsExtra)
library(vars)
data(Canada)
plot.xts(Canada,screens=1)
So I am really eager to ask some plausible ways to add additional y-axis on the xts-based time series plot. Hope my words were not so rookie enough...

Finding patterns through better visualization in R

I have the following time series data. It has 60 data points shown below. Please see a simple plot of this data below. I am using R for plotting this. I think that if I draw a moving average curve on the points in the graph, then we can better understand the patterns in the data. I don't know how to do it in R. Could some one help me to do that. Additionally, I am not sure whether this is a good way to identify patterns or not. Please also suggest me if there is any better way. Thank you.
x <- c(18,21,18,14,8,14,10,14,14,12,12,14,10,10,12,6,10,8,
14,10,10,6,6,4,6,2,8,6,2,6,4,4,2,8,6,6,8,12,8,8,6,6,2,2,4,
4,4,8,14,8,6,6,2,6,6,4,4,8,6,6)
To answer your question about moving averages, you could accomplish it with the help of rollmean which is in package zoo.
From Joshua's comment: You could also look into TTR package that depends on xts that depends on zoo. Also, there are other moving averages in the package TTR: check ?MA.
require(TTR)
# assuming your vector is loaded in dat
# sliding window / moving average of size 5
dat.k5 <- rollmean(dat, k=5)
One reasonable possibility:
d <- data.frame(x=scan("tmp.dat"))
qplot(x=seq(nrow(d)),x,data=d)+geom_smooth(method="loess")
edit: moved from comment to answer, based on https://meta.stackexchange.com/questions/164783/why-was-a-seemingly-relevant-non-offensive-comment-removed
With regard to "is this a good way to identify patterns" (which is a little off-topic for StackOverflow, but whatever); I think rolling means are perfectly respectable, although more sophisticated methods (such as the locally-weighted regression [loess/lowess] shown here) do exist. However, it doesn't look to me as though there is much of a complicated pattern to detect here: the data seem to initially decline with time, then level off. Rolling means and more sophisticated approaches may look prettier, but I don't think they will identify any deeper patterns in this data set ...
If you want to do this sort of thing for multiple data sets at once (as indicated in your comment), you may like ggplot's capabilities for automatically producing multi-line or faceted versions of the same plot.

How to output a chart from Nielsen?

I saw an interesting chart on engadget today made by Nielsen:
http://www.engadget.com/2011/07/28/nielsen-android-leads-us-smartphone-market-with-39-percent-shar/
original source: http://blog.nielsen.com/nielsenwire/online_mobile/in-u-s-smartphone-market-android-is-top-operating-system-apple-is-top-manufacturer/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+NielsenWire+%28Nielsen+Wire%29
I'd love for someone to replicate it if possible and show the R code. Basic packages or ggplot2 would be great.
I like that the boxes are proportional, that's a key feature :)
Thanks!
You can find several implementations in R under the name of 'mosaic chart'. E.g.:
require("vcd")
data(HairEyeColor)
mosaic(HairEyeColor, shade = TRUE)
Se some examples on e.g. quickR, but searching the R graph gallery is also a good option.
In ggplot2, you can find a sample on learnr's blog.
I have also done some tweaks in ggplot2, please find the attached plot below. It is in Hungarian, but if you are interested, I could clean up the code and post is somewhere.
UPDATE: I have searched for my old script based on comment and uploaded it to pastebin. Sorry, no code clean up and it is quite messy, as I had to make it up for mass reporting from SPSS data files, but I hope you could use it. The usage is simple: load all functions (e.g.: run all lines in R with the source(...) function), and you could generate a mosaic chart of any data frame by specifying two variable names in the parameters of ggMosaicChart(). The plot will be saved to a png file in the working directory (no easy resize in R of the plot as lots of manual tweaks are done to arrange text nicely).
I have translated the strings to English, a basic example (included in the above code) of the mtcars data set:
Count, row- and column percent and also Pearson residuals are shown for each cell.
It's called treemap. R project has packages named "treemap" or "portfolio" for it. Here is how to do: http://flowingdata.com/2010/02/11/an-easy-way-to-make-a-treemap/

Convert a Graph to a Data Frame in R

So a while back (6 months+) I saw a blog post where the author took a line graph someone had posted on the internet, fed the image into R, and used a function to convert the image into a data frame.
I've looked everywhere, and I can't seem to find this blog post (even though I'm sure I bookmarked it). So I was wondering if any of you had also read said blog post, or if someone knew of a quick and easy way to convert a line graph to a data frame in R?
Was this it? I searched for "R digitize plot". The package used is "ReadImages". For completeness, the steps listed were (see link):
library(ReadImages) #Load package
mygraph <- read.jpeg('plot.jpg') #Import image
plot(mygraph) # Plot the image
calpoints <- locator(n=4,type='p',pch=4,col='blue',lwd=2) # Calibrate the plot by selecting known coordinates
data <- locator(type='p',pch=1,col='red',lwd=1.2,cex=1.2) # Collect the data points in a dataframe
When you say 'the image as a data frame', do you mean you want to get back to the original data that made the line?
It's not R, but I've used Engauge Digitizer for this sort of thing:
http://digitizer.sourceforge.net/
Also look at the updateusr function in the TeachingDemos package. Once you have the image displayed as in Benjamin's post, you can use the updateusr function with the known points to change the user coordinates so that then the results from the locator function do not need any additional transformation.
As i write this, the digitize package and the ReadImages package are no longer available for R 3.0.2. Engauge Digitizer is a good option but if you still want to do this sort of thing in R, take a loook at http://rscriptsandtips.blogspot.no/
You can also use im2graph to convert graphs to data. It's free and available of Windows and Linux (http://www.im2graph.co.il).

Resources