Skip to content
 

One More Graph, SQL to R


import psycopg2
conn = psycopg2.connect( "dbname=example_db" )
curs = conn.cursor()

tSQL = '''
select emp_gden_min, res_units_gden_min, emp_gden_max, res_units_gden_max
from p_translation_table;
'''
curs.execute( tSQL )
resT = curs.fetchall()

from rpy2.robjects.packages import importr
grdevices = importr( 'grDevices' )
grdevices.pdf(file='/home/shared/aab.pdf')

r_plot_str = '''
plot(c(0,1000),c(0,2000),type="n",log="xy",xlim=c(0.1,1000),ylim=c(0.1,2000), xlab="employment", ylab="residence")
'''
rpy2.robjects.r( r_plot_str )

tC = 0
for n in resT:
  tStr2 = "rect( %s,%s,%s,%s, density=10, col=rgb(%s,%s,%s) )" \
    % (n[0],n[1],n[2],n[3], tC,tC,tC)
  rpy2.robjects.r( tStr2 )
  tC += 1.0/len(resT)

grdevices.dev_off()