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