http://blog.revolutionanalytics.com/2015/09/using-r-with-jupyter-notebooks.html http://blog.revolutionanalytics.com/2016/01/pipelining-r-python.html
conda install -c r ipython-notebook r-irkernel conda install rpy2
%load_ext rpy2.ipython
%%R -o diamonds
#install.packages("ggplot2")
library("ggplot2")
data(diamonds)
head(diamonds)
ggplot(diamonds, aes(x= carat, y= price, color= cut)) +
geom_point(alpha=0.5)
from bokeh.plotting import figure, output_file, show
from rpy2.robjects import pandas2ri
from bokeh.palettes import brewer
import pandas
import bokeh
df = pandas2ri.ri2py_dataframe(diamonds)
print(type(df))
#print(df)
# brute through colors
# look at https://github.com/bokeh/bokeh/issues/5112 for a better way
cuts = list(set(df['cut']))
print(cuts)
colors = brewer["Spectral"][len(cuts)]
# print(colors)
map = { cuts[i]:colors[i] for i in range(len(cuts)) }
print(map)
cutcode = [ map[ci] for ci in df['cut'] ]
# print(cutcode)
bokeh.io.output_notebook() # inline output
# create a new plot with a title and axis labels
p = figure(title="simple plot example", x_axis_label='carat', y_axis_label='price')
# add a circle renderer
p.circle(x=df['carat'], y=df['price'], color=cutcode, size=5, alpha=0.5)
# show the results
show(p)
import datashader as ds
import datashader.transfer_functions as tf
import pandas as pd
from datashader.bokeh_ext import InteractiveImage
import bokeh.plotting as bp
from datashader.bokeh_ext import InteractiveImage
from datashader.bokeh_ext import create_ramp_legend, create_categorical_legend
from datashader.colors import viridis, inferno
from functools import partial
from datashader.colors import colormap_select, Greys9
background = "white"
#bp.output_notebook()
p = bp.figure(tools='pan,wheel_zoom,box_zoom,reset,save', x_range=(0,5), y_range=(0,20000))
#cat_legend = create_categorical_legend(map)
#show(cat_legend)
cm = partial(colormap_select, reverse=(background!="black"))
df['cutcode'] = df['cut'].astype('category')
def image_callback(x_range, y_range, w, h):
cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range)
agg = cvs.points(df, 'carat', 'price', ds.count_cat('cutcode'))
img = tf.shade(agg)
tf.shade(agg, cmap=cm(viridis), how='eq_hist')
return tf.dynspread(img, threshold=1)
InteractiveImage(p, image_callback)
%%R
library("dplyr")
diamonds %>% filter(carat>=3, carat<=3.05, price>=18200, price<=18400)