Rpy2 and drawing pie diagram

Here is code that I come up with for drawing a pie diagram using rpy2. The function takes two lists. Function takes two lists: data contains frequencies and labels labels. The argument title set the title text of chart. Radius sets the radius of pie (max 1.0). Font size 1 is default, 1.5 is 50% bigger, 0.5 is 50% smaller. If file name is provided the chart will be saved to png file. Radius, font_size, and file name are optional parameters.

import rpy2.robjects as robjects
from rpy2.robjects.packages import importr

def draw_pie(data, labels, title, radius=0.8, font_size=1.0, file_name=None):

    total = 0
    for x in data: total = total + x

    labels_with_percentages = []
    for td, tl in zip(data, labels):
        percentage = 100.0 * float(td)/float(total)
        labels_with_percentages.append("%s %.1f%%" % (tl, percentage))

    l = robjects.StrVector(labels_with_percentages)
    d = robjects.IntVector(data)

    grdevices = importr('grDevices')
    if file_name:
        grdevices.png(file_name)
    font = robjects.r['par'](cex=font_size)
    robjects.r.pie(d,l, main=title, radius=radius)
    if file_name:
        grdevices.dev_off()

To use this we can do:

draw_pie([10,8],["Male", "Female"], "Sex", font_size=1.5, file_name="chart.png")
Advertisements

rpy2 and plotting

I had a problem to plot in R or rpy2 when I started the R on Terminal on Mac OSX (R works fine if started by clicking it on Lauchpad ). R throws an following error if locale is set to C.

Error in axis(side = side, at = at, labels = labels, ...) : 
  could not find any X11 fonts
Check that the Font Path is correct.
In addition: Warning messages:
1: In function (display = "", width, height, pointsize, gamma, bg,  :
  locale not supported by Xlib: some X ops will operate in C locale
2: In function (display = "", width, height, pointsize, gamma, bg,  :
  X cannot set locale modifiers

Running command “export LC_ALL=en_US.UTF-8” on Terminal before starting R or python fix this (or adding it to .profile file).

Calculating correlation with rpy2

I spend some time trying to figure out how to use R via Rpy2. Here are a short python code that I used to calculate correlation using Rpy2

import rpy2.robjects as robjects

def get_rcorr(floatData, n_rows, method=”spearman”):

“””
floatData: list of floats,
number of rows in the data matrix
Hmisc library needs to be loaded before calling this
function ( rcorr() comes from there)..
Library can be loaded with command:  robjects.r(“library(Hmisc)”)
“””
v = robjects.FloatVector(floatData)
dm = robjects.r[‘matrix’](v, nrow = n_rows)
res = robjects.r.rcorr(dm, type=method)
return res

robjects.r(“library(Hmisc)”) # for rcorr()

print (get_rcorr([1.0,2.0,4.0,5.0,1.0,1.0,1.0,0.0], 4)

get_rcorr([1,2,4,5,1,1,1,0], 4) will create then matrix as follows

  1.0  1.0
  2.0  1.0
  4.0  1.0
  5.0  0.0

and calculate correlation (R, p) for that.