R and Rpy

R is a strong language. And there is a way in python with that R can be used that is called Rpy http://rpy.sourceforge.net/

its easy to install. here I will show you an example to create simple graphics using rpy

from rpy import *

def create_simple_bar_chart(file_name,size=5,bottom=1,left=0.5,top=0,right=0):
if bottom>left:
graphics_width = size+bottom;
graphics_height = size+bottom;
if left>bottom:
graphics_width = size+left;
graphics_height = size+left;
if left==bottom:
graphics_width = size+left;
graphics_height = size+left;

r.pdf(file_name,graphics_width,graphics_height,paper=”a4″,pagecentre=r.F)
r.par(omi=r.c(bottom,left,top,right))
r.par(pty = “s”)
r.plot(r.table(r.rpois(150,7)),ylab=”hospital”,xlab=”patient”, type = “h”, col = “red”, lwd=10,main=”number of patients per hospital”)
r.dev_off()

def create_hist_with_R(file_name,size=5,bottom=1,left=1,top=0,right=0):
x = [24,60,18,63,32,47,88,64,72,87,100,16,37,46,50,\
64,11,45,77,35,38,39,84,90,6,87,25,67,51,71,52,\
98,57,5,65,75,8,68,35,75,95,29,50,44,30,1,92,93,\
56,56]

if bottom>left:
graphics_width = size+bottom;
graphics_height = size+bottom;
if left>bottom:
graphics_width = size+left;
graphics_height = size+left;
if left==bottom:
graphics_width = size+left;
graphics_height = size+left;

r.pdf(file_name,graphics_width,graphics_height,paper=”a4″,pagecentre=r.F)
r.par(omi=r.c(bottom,left,top,right))
r.par(pty = “s”)
#r.plot(r.table(r.rpois(100,5)),xlab=”frequency”, type = “h”, col = “red”, lwd=10,main=”rpois(100,lambda=5)”)
r.hist(x, xlab=”frequency”, ylab=”count”, main=”histogram example using R”, col=’cyan’)
r.dev_off()

def create_simple_bar_chart_rainbow(file_name,size=5,bottom=1,left=0.5,top=0,right=0):
if bottom>left:
graphics_width = size+bottom;
graphics_height = size+bottom;
if left>bottom:
graphics_width = size+left;
graphics_height = size+left;
if left==bottom:
graphics_width = size+left;
graphics_height = size+left;

r.pdf(file_name,graphics_width,graphics_height,paper=”a4″,pagecentre=r.F)
r.par(omi=r.c(bottom,left,top,right))
r.par(pty = “s”)

r.plot(r.cos, -r.pi, 2*r.pi,col=r.rainbow(20),ylab=”biologic”,xlab=”total”,main=”Test Data”)

r.dev_off()
def draw_pie_chart(file_name,size=5,bottom=1,left=0.5,top=0,right=0):
if bottom>left:
graphics_width = size+bottom;
graphics_height = size+bottom;
if left>bottom:
graphics_width = size+left;
graphics_height = size+left;
if left==bottom:
graphics_width = size+left;
graphics_height = size+left;

r.pdf(file_name,graphics_width,graphics_height,paper=”a4″,pagecentre=r.F)
r.par(omi=r.c(bottom,left,top,right))
r.par(pty = “s”)

r.pie(r.rep(1, 24), col = r.rainbow(24), radius = 0.9,main=”test pie chart”)

r.dev_off()

draw_pie_chart(“rgraphics_pie.pdf”,5,1,1,0,0)

create_hist_with_R(“rgraphics_hist.pdf”,5,1,1,0,0)

create_simple_bar_chart(“rgraphics_bar.pdf”,4,5.5,0.5,0,0)

Advertisements
About

Work is fun!

Tagged with: , , , ,
Posted in Linux, python, Ubuntu

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Disclaimer
All content provided on this "tareqalam.com" blog is for informational purposes only. The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner of [tareqalam.wordpress.com] will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.
September 2008
M T W T F S S
« Jul   Oct »
1234567
891011121314
15161718192021
22232425262728
2930  

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 23 other followers

%d bloggers like this: