Wednesday, July 30, 2014

Refresh Old rCharts+flickr post with httr and pipeR

The R world keeps moving, and I noticed this old post didn’t work anymore, so I have rewritten it to use Hadley Wickham's httr instead of Rflickr for two reasons:

  1. Rflickr is not working for me anymore
  2. httr is a very helpful package for navigating the "what was scary to me" world of http and oauth

In addition to the changes above, I will also demonstrate use of the pipeR package from Kun Ren who has been quite prolific lately. I feel pretty strongly I will be rewriting this post one more time in the near future (already done) employing his rlist package.

image

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 %$%.

image

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.

mydata
%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.

require(ggplot2)
require(dplyr)
require(magrittr)
require(quantmod)

getSymbols("SP500", src="FRED", from = "1900-01-01", auto.assign=F) %>%
na.omit %>%
data.frame(
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(
axis.line=element_blank()
,axis.text.x=element_blank()
,axis.title.x=element_blank()
)

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.