def ggplot2_options(): def normal_text(): return ggplot2.theme_text(family='serif', size=15) def bold_text(): return ggplot2.theme_text(family='serif', face='bold', size=15) def rotated_text(): return ggplot2.theme_text(family='serif', face='bold', size=15, angle=90, vjust=0.2) return ggplot2.opts( **{ 'axis.title.x': ggplot2.theme_blank(), 'axis.title.y': rotated_text(), 'axis.text.x': normal_text(), 'axis.text.y': normal_text(), 'legend.title': bold_text(), 'legend.text': normal_text(), 'aspect.ratio': 0.6180339888, 'strip.text.x': normal_text(), })
def ggplot2_options (): def normal_text(): return ggplot2.theme_text(family = 'serif', size = 15) def bold_text(): return ggplot2.theme_text(family = 'serif', face = 'bold', size = 15) def rotated_text(): return ggplot2.theme_text(family = 'serif', face = 'bold', size = 15, angle=90, vjust=0.2) return ggplot2.opts (**{'axis.title.x' : ggplot2.theme_blank(), 'axis.title.y' : rotated_text(), 'axis.text.x' : normal_text(), 'axis.text.y' : normal_text(), 'legend.title' : bold_text(), 'legend.text' : normal_text(), 'aspect.ratio' : 0.6180339888, 'strip.text.x' : normal_text(), })
def ggplot2_options(): return ggplot2.opts( **{ 'axis.title.x': ggplot2.theme_blank(), 'axis.title.y': ggplot2.theme_text( family='serif', face='bold', size=15, angle=90, vjust=0.2), 'axis.text.x': ggplot2.theme_text(family='serif', size=15), 'axis.text.y': ggplot2.theme_text(family='serif', size=15), 'legend.title': ggplot2.theme_text(family='serif', face='bold', size=15), 'legend.text': ggplot2.theme_text(family='serif', size=15), 'aspect.ratio': 0.6180339888, })
grdevices = importr('grDevices') grdevices.png(file='/Users/user/Downloads/data/mapplot.png', width=1300, height=1000) ## plot the map ## note that the order matters when we add another layer in ggplot (here IL_railroads): first aes, then data, that's different from R ## (see http://permalink.gmane.org/gmane.comp.python.rpy/2349) ## note that we use dictionary to set the opts to be able to set options as keywords, for example legend.key.size p_map = ggplot2.ggplot(IL_final) + \ ggplot2.geom_polygon(ggplot2.aes(x = 'long', y = 'lat', group = 'group', color = 'ObamaShare', fill = 'ObamaShare')) + \ 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') + \
width=1300, height=1000) ## plot the map ## note that the order matters when we add another layer in ggplot (here IL_railroads): first aes, then data, that's different from R ## (see http://permalink.gmane.org/gmane.comp.python.rpy/2349) ## note that we use dictionary to set the opts to be able to set options as keywords, for example legend.key.size p_map = ggplot2.ggplot(IL_final) + \ ggplot2.geom_polygon(ggplot2.aes(x = 'long', y = 'lat', group = 'group', color = 'ObamaShare', fill = 'ObamaShare')) + \ 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') + \