Tuesday, July 22, 2014

Chart from R + Color from Javascript

Another color experiment combining resources from R and Javascript.  I just wish I could do Mean Phylogenetic Distance in Javascript like rPlotter.  I enjoyed using d3.js zoom behavior to pan and zoom the image on canvas.  Also, filedrop.js made the drag and drop image easy.  There are lots of mini lessons in this code for someone who want to pick inside the code.

You might also notice the reference color from my last post Pick a Color Site built in R with Shiny tags %$%.


Thursday, July 17, 2014

Alternate Price Plots | ggplot2 + magrittr

Just some quick experiments with ggplot2 + magrittr to plot prices differently than the traditional ways. We wi€™ll get daily data on the S&P 500 from FRED using getSymbols and then push it through a magrittr pipe to various alternative plots.

A couple notes for those who might be interested in magrittr. If you want to -> or <- but do not want the pipe fun to end, then you can use assign like this.

%T>% assign( x="sp_df", value = ., envir = .GlobalEnv ) %>%

Also for those who want to use the special + inside a pipe or just want your + on a different line for tidy code and easy commenting, then you can do like this.

ggplot( ) %>%
+ geom_point()

Let me know if I have it all wrong, and now some plots as promised.


getSymbols("SP500", src="FRED", from = "1900-01-01", auto.assign=F) %>%
na.omit %>%
date = index(.)
) %T>% assign( x="sp_df", value = ., envir = .GlobalEnv ) %>%
mutate( year = format(date,"%Y") ) %>%
ggplot( aes( x=SP500, group = year ) ) %>%
+ geom_density() %>%
+ facet_wrap(~year,nrow=1) %>%
+ coord_flip() %>%
+ theme_bw() %>%
+ theme(

plot of chunk unnamed-chunk-3

sp_df %>%
ggplot( aes( x = format(date,"%Y"), y = SP500 ) ) %T>%
( function(x){ print( x + geom_line() ) } ) %T>%
( function(x){ print( x + geom_point() ) } ) %T>%
( function(x){ print( x + geom_hex( bins = 20 ) ) } ) %>%
+ geom_violin()

plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3

If you are interested in seeing the source Rmd, then it is here. One more note is this is copied/pasted with only minor changes from Rstudio knitted Rmd.