In [1]:
%load_ext rpy2.ipython
In [2]:
%%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)
In [3]:
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)
<class 'pandas.core.frame.DataFrame'>
[1, 2, 3, 4, 5]
{1: '#2b83ba', 2: '#abdda4', 3: '#ffffbf', 4: '#fdae61', 5: '#d7191c'}
Loading BokehJS ...
In [4]:
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)
//anaconda/lib/python3.5/site-packages/datashader/transfer_functions.py:258: RuntimeWarning: invalid value encountered in true_divide
  r = (data.dot(rs)/total).astype(np.uint8)
//anaconda/lib/python3.5/site-packages/datashader/transfer_functions.py:259: RuntimeWarning: invalid value encountered in true_divide
  g = (data.dot(gs)/total).astype(np.uint8)
//anaconda/lib/python3.5/site-packages/datashader/transfer_functions.py:260: RuntimeWarning: invalid value encountered in true_divide
  b = (data.dot(bs)/total).astype(np.uint8)
Out[4]:
In [5]:
%%R
library("dplyr")
diamonds %>% filter(carat>=3, carat<=3.05, price>=18200, price<=18400)
//anaconda/lib/python3.5/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: 
Attaching package: ‘dplyr’


  warnings.warn(x, RRuntimeWarning)
//anaconda/lib/python3.5/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: The following objects are masked from ‘package:stats’:

    filter, lag


  warnings.warn(x, RRuntimeWarning)
//anaconda/lib/python3.5/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union


  warnings.warn(x, RRuntimeWarning)
# A tibble: 5 × 10
  carat     cut color clarity depth table price     x     y     z
  <dbl>   <ord> <ord>   <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1  3.01 Premium     I     SI2  60.2    59 18242  9.36  9.31  5.62
2  3.01    Fair     I     SI2  65.8    56 18242  8.99  8.94  5.90
3  3.01    Fair     I     SI2  65.8    56 18242  8.99  8.94  5.90
4  3.01    Good     I     SI2  63.9    60 18242  9.06  9.01  5.77
5  3.01    Good     I     SI2  63.9    60 18242  9.06  9.01  5.77
In [ ]: