def makeImage(rates): out = tempfile.NamedTemporaryFile(suffix='.png') grdevices = importr('grDevices') grdevices.png(file=out.name, width=512, height=512) try: grid.newpage() lt = grid.layout(1, 1) vp = grid.viewport(layout=lt) vp.push() vp = grid.viewport(**{'layout.pos.col': 1, 'layout.pos.row': 1}) for row, (year, count) in enumerate(sorted(rates['byYear'].items())): grid.rect(x=grid.unit(.5, "npc"), y=grid.unit(row / len(rates['byYear']), "npc"), width=grid.unit(count / 15000, "npc"), height=grid.unit(.95 / len(rates['byYear']), "npc"), vp=vp).draw() finally: grdevices.dev_off() return open(out.name).read()
def makeImage(rates): out = tempfile.NamedTemporaryFile(suffix='.png') grdevices = importr('grDevices') grdevices.png(file=out.name, width=512, height=512) try: grid.newpage() lt = grid.layout(1, 1) vp = grid.viewport(layout = lt) vp.push() vp = grid.viewport(**{'layout.pos.col':1, 'layout.pos.row': 1}) for row, (year, count) in enumerate(sorted(rates['byYear'].items())): grid.rect(x=grid.unit(.5, "npc"), y=grid.unit(row / len(rates['byYear']), "npc"), width=grid.unit(count / 15000, "npc"), height=grid.unit(.95 / len(rates['byYear']), "npc"), vp = vp).draw() finally: grdevices.dev_off() return open(out.name).read()
def test_viewport(): v = grid.viewport()
pp = gp + \ ggplot2.aes_string(x='wt', y='mpg') + \ ggplot2.geom_point() pp.plot() #-- ggplot2mtcars-end grdevices.dev_off() grdevices.png('../../_static/graphics_ggplot2geombin2d.png', width=1000, height=350, antialias="subpixel", type="cairo") grid.newpage() grid.viewport(layout=grid.layout(1, 3)).push() vp = grid.viewport(**{'layout.pos.col': 1, 'layout.pos.row': 1}) #-- ggplot2geombin2d-begin gp = ggplot2.ggplot(dataf_rnorm) pp = gp + \ ggplot2.aes_string(x='value', y='other_value') + \ ggplot2.geom_bin2d() + \ ggplot2.ggtitle('geom_bin2d') pp.plot(vp=vp) #-- ggplot2geombin2d-end vp = grid.viewport(**{'layout.pos.col': 2, 'layout.pos.row': 1}) #-- ggplot2geomdensity2d-begin gp = ggplot2.ggplot(dataf_rnorm)
ggplot2.scale_fill_gradient(high = 'blue', low = 'red') + \ ggplot2.scale_fill_continuous(name = "Obama Vote Share") + \ ggplot2.scale_colour_continuous(name = "Obama Vote Share") + \ ggplot2.opts(**{'legend.position': 'left', 'legend.key.size': robjects.r.unit(2, 'lines'), 'legend.title' : ggplot2.theme_text(size = 14, hjust=0), \ 'legend.text': ggplot2.theme_text(size = 12), 'title' : "Obama Vote Share and Distance to Railroads in IL", \ 'plot.title': ggplot2.theme_text(size = 24), 'plot.margin': robjects.r.unit(robjects.r.rep(0,4),'lines'), \ 'panel.background': ggplot2.theme_blank(), 'panel.grid.minor': ggplot2.theme_blank(), 'panel.grid.major': ggplot2.theme_blank(), \ 'axis.ticks': ggplot2.theme_blank(), 'axis.title.x': ggplot2.theme_blank(), 'axis.title.y': ggplot2.theme_blank(), \ 'axis.title.x': ggplot2.theme_blank(), 'axis.title.x': ggplot2.theme_blank(), 'axis.text.x': ggplot2.theme_blank(), \ 'axis.text.y': ggplot2.theme_blank()} ) + \ ggplot2.geom_line(ggplot2.aes(x='long', y='lat', group='group'), data=IL_railroads, color='grey', size=0.2) + \ ggplot2.coord_equal() p_map.plot() ## add the scatterplot ## define layout of subplot with viewports vp_sub = grid.viewport(x = 0.19, y = 0.2, width = 0.32, height = 0.4) p_sub = ggplot2.ggplot(RR_distance) + \ ggplot2.aes_string(x = 'OBAMA_SHAR', y= 'NEAR_DIST') + \ ggplot2.geom_point(ggplot2.aes(color='OBAMA_SHAR')) + \ ggplot2.stat_smooth(color="black") + \ ggplot2.opts(**{'legend.position': 'none'}) + \ ggplot2.scale_x_continuous("Obama Vote Share") + \ ggplot2.scale_y_continuous("Distance to nearest Railroad") p_sub.plot(vp=vp_sub) grdevices.dev_off()
width = 612, height = 612, antialias="subpixel", type="cairo") #-- ggplot2mtcars-begin gp = ggplot2.ggplot(mtcars) pp = gp + \ ggplot2.aes_string(x='wt', y='mpg') + \ ggplot2.geom_point() pp.plot() #-- ggplot2mtcars-end grdevices.dev_off() grdevices.png('../../_static/graphics_ggplot2geombin2d.png', width = 1000, height = 350, antialias="subpixel", type="cairo") grid.newpage() grid.viewport(layout=grid.layout(1, 3)).push() vp = grid.viewport(**{'layout.pos.col':1, 'layout.pos.row': 1}) #-- ggplot2geombin2d-begin gp = ggplot2.ggplot(dataf_rnorm) pp = gp + \ ggplot2.aes_string(x='value', y='other_value') + \ ggplot2.geom_bin2d() + \ ggplot2.opts(title = 'geom_bin2d') pp.plot(vp = vp) #-- ggplot2geombin2d-end vp = grid.viewport(**{'layout.pos.col':2, 'layout.pos.row': 1}) #-- ggplot2geomdensity2d-begin gp = ggplot2.ggplot(dataf_rnorm)
ggplot2.scale_fill_gradient(high = 'blue', low = 'red') + \ ggplot2.scale_fill_continuous(name = "Obama Vote Share") + \ ggplot2.scale_colour_continuous(name = "Obama Vote Share") + \ ggplot2.opts(**{'legend.position': 'left', 'legend.key.size': robjects.r.unit(2, 'lines'), 'legend.title' : ggplot2.theme_text(size = 14, hjust=0), \ 'legend.text': ggplot2.theme_text(size = 12), 'title' : "Obama Vote Share and Distance to Railroads in IL", \ 'plot.title': ggplot2.theme_text(size = 24), 'plot.margin': robjects.r.unit(robjects.r.rep(0,4),'lines'), \ 'panel.background': ggplot2.theme_blank(), 'panel.grid.minor': ggplot2.theme_blank(), 'panel.grid.major': ggplot2.theme_blank(), \ 'axis.ticks': ggplot2.theme_blank(), 'axis.title.x': ggplot2.theme_blank(), 'axis.title.y': ggplot2.theme_blank(), \ 'axis.title.x': ggplot2.theme_blank(), 'axis.title.x': ggplot2.theme_blank(), 'axis.text.x': ggplot2.theme_blank(), \ 'axis.text.y': ggplot2.theme_blank()} ) + \ ggplot2.geom_line(ggplot2.aes(x='long', y='lat', group='group'), data=IL_railroads, color='grey', size=0.2) + \ ggplot2.coord_equal() p_map.plot() ## add the scatterplot ## define layout of subplot with viewports vp_sub = grid.viewport(x=0.19, y=0.2, width=0.32, height=0.4) p_sub = ggplot2.ggplot(RR_distance) + \ ggplot2.aes_string(x = 'OBAMA_SHAR', y= 'NEAR_DIST') + \ ggplot2.geom_point(ggplot2.aes(color='OBAMA_SHAR')) + \ ggplot2.stat_smooth(color="black") + \ ggplot2.opts(**{'legend.position': 'none'}) + \ ggplot2.scale_x_continuous("Obama Vote Share") + \ ggplot2.scale_y_continuous("Distance to nearest Railroad") p_sub.plot(vp=vp_sub) grdevices.dev_off()
def r_grid(nrows, ncols): lt = grid.layout(nrows, ncols) vp = grid.viewport(layout = lt) vp.push() return vp