{"id":612,"date":"2012-02-28T10:47:34","date_gmt":"2012-02-28T18:47:34","guid":{"rendered":"http:\/\/blog.light42.com\/wordpress\/?p=612"},"modified":"2012-06-28T17:19:16","modified_gmt":"2012-06-29T00:19:16","slug":"one-more-graph-sql-to-r","status":"publish","type":"post","link":"http:\/\/blog.light42.com\/wordpress\/?p=612","title":{"rendered":"One More Graph, SQL to R"},"content":{"rendered":"<p><code><br \/>\nimport psycopg2<br \/>\nconn = psycopg2.connect( \"dbname=example_db\" )<br \/>\ncurs = conn.cursor()<\/p>\n<p>tSQL = '''<br \/>\nselect emp_gden_min, res_units_gden_min, emp_gden_max, res_units_gden_max<br \/>\n from p_translation_table;<br \/>\n'''<br \/>\ncurs.execute( tSQL )<br \/>\nresT = curs.fetchall()<\/p>\n<p>from rpy2.robjects.packages import importr<br \/>\ngrdevices = importr( 'grDevices' )<br \/>\ngrdevices.pdf(file='\/home\/shared\/aab.pdf')<\/p>\n<p>r_plot_str = '''<br \/>\nplot(c(0,1000),c(0,2000),type=\"n\",log=\"xy\",xlim=c(0.1,1000),ylim=c(0.1,2000), xlab=\"employment\", ylab=\"residence\")<br \/>\n'''<br \/>\nrpy2.robjects.r( r_plot_str )<\/p>\n<p>tC = 0<br \/>\nfor n in resT:<br \/>\n&nbsp;&nbsp;tStr2 = \"rect( %s,%s,%s,%s, density=10, col=rgb(%s,%s,%s) )\" \\<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;% (n[0],n[1],n[2],n[3], tC,tC,tC)<br \/>\n&nbsp;&nbsp;rpy2.robjects.r( tStr2 )<br \/>\n&nbsp;&nbsp;tC += 1.0\/len(resT)<\/p>\n<p>grdevices.dev_off()<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import psycopg2 conn = psycopg2.connect( &#8220;dbname=example_db&#8221; ) curs = conn.cursor() tSQL = &#8221;&#8217; select emp_gden_min, res_units_gden_min, emp_gden_max, res_units_gden_max from p_translation_table; &#8221;&#8217; curs.execute( tSQL ) resT = curs.fetchall() from rpy2.robjects.packages import importr grdevices = importr( &#8216;grDevices&#8217; ) grdevices.pdf(file=&#8217;\/home\/shared\/aab.pdf&#8217;) r_plot_str = &#8221;&#8217; plot(c(0,1000),c(0,2000),type=&#8221;n&#8221;,log=&#8221;xy&#8221;,xlim=c(0.1,1000),ylim=c(0.1,2000), xlab=&#8221;employment&#8221;, ylab=&#8221;residence&#8221;) &#8221;&#8217; rpy2.robjects.r( r_plot_str ) tC = 0 for n in resT: &nbsp;&nbsp;tStr2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4,6],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/612"}],"collection":[{"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=612"}],"version-history":[{"count":6,"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/612\/revisions"}],"predecessor-version":[{"id":619,"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/612\/revisions\/619"}],"wp:attachment":[{"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=612"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}