# grdevices.dev_off() grdevices.png('../../_static/graphics_ggplot2map_polygon.png', width=612, height=612, antialias="subpixel", type="cairo") #-- ggplot2mappolygon-begin map = importr('maps') fr = ggplot2.map_data('france') # add a column indicating which region names have an "o". fr = fr.cbind(fr, has_o=base.grepl('o', fr.rx2("region"), ignore_case=True)) p = ggplot2.ggplot(fr) + \ ggplot2.geom_polygon(ggplot2.aes(x = 'long', y = 'lat', group = 'group', fill = 'has_o'), col="black") p.plot() #-- ggplot2mappolygon-end grdevices.dev_off() grdevices.png('../../_static/graphics_ggplot2mtcars_coordtrans.png', width=936, height=624, antialias="subpixel", type="cairo") #-- ggplot2mtcarscoordtrans-begin from rpy2.robjects.lib import grid grid.newpage() grid.viewport(layout=grid.layout(2, 3)).push()
# #-- ggplot2coordtransreverse-end # grdevices.dev_off() grdevices.png('../../_static/graphics_ggplot2map_polygon.png', width=612, height=612, antialias=ANTIALIAS, type="cairo") #-- ggplot2mappolygon-begin map = importr('maps') fr = ggplot2.map_data('france') # add a column indicating which region names have an "o". fr = fr.cbind(has_o=base.grepl('o', fr.rx2("region"), ignore_case=True)) p = (ggplot2.ggplot(fr) + ggplot2.geom_polygon(ggplot2.aes_string( x='long', y='lat', group='group', fill='has_o'), col="black")) p.plot() #-- ggplot2mappolygon-end grdevices.dev_off() grdevices.png('../../_static/graphics_ggplot2mtcars_coordtrans.png', width=936, height=624, antialias=ANTIALIAS, type="cairo") #-- ggplot2mtcarscoordtrans-begin from rpy2.robjects.lib import grid grid.newpage() grid.viewport(layout=grid.layout(2, 3)).push()
## loaded data sets can now be accessed through R handle ## note that different from R dot . is not valid for Python variable names! IL_railroads = robjects.r('IL.railroads') IL_final = robjects.r('IL.final') ## import device driver from R with importr to plot to PNG ## we can then call any function in the grdevices package 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()
# #-- ggplot2coordtransreverse-end # grdevices.dev_off() grdevices.png('../../_static/graphics_ggplot2map_polygon.png', width = 612, height = 612, antialias="subpixel", type="cairo") #-- ggplot2mappolygon-begin map = importr('maps') fr = ggplot2.map_data('france') # add a column indicating which region names have an "o". fr = fr.cbind(fr, has_o = base.grepl('o', fr.rx2("region"), ignore_case = True)) p = ggplot2.ggplot(fr) + \ ggplot2.geom_polygon(ggplot2.aes(x = 'long', y = 'lat', group = 'group', fill = 'has_o'), col="black") p.plot() #-- ggplot2mappolygon-end grdevices.dev_off() grdevices.png('../../_static/graphics_grid.png', width = 612, height = 612, antialias="subpixel", type="cairo") #-- grid-begin grid.newpage() # create a rows/columns layout lt = grid.layout(2, 3) vp = grid.viewport(layout = lt)
IL_railroads = robjects.r('IL.railroads') IL_final = robjects.r('IL.final') ## import device driver from R with importr to plot to PNG ## we can then call any function in the grdevices package 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()
pickups_late = pickups_late.groupby(['nbhd', 'period'])['passenger_count'].sum().reset_index() temp_perc = pickups_late.groupby(['nbhd'])['passenger_count'].apply(lambda x: 100*x/float(x.sum())).reset_index() temp_perc = temp_perc.rename(columns={'passenger_count':'relative_percent'}) pickups_late = pd.concat([pickups_late, temp_perc['relative_percent']], axis=1) pickups_late = pickups_late[pickups_late['period'] == 'late'] pickups_late = pd.merge(pickups_late, nbhd_borders, how='right', on=['nbhd']).dropna() # Find top 10 neighborhoods with largest late night % of pickups print(tabulate(pickups_late[['relative_percent', 'nbhd', 'borough']].drop_duplicates().sort(['relative_percent'], ascending=False).head(10), tablefmt='pipe', headers='keys', showindex=False)) p4 = ggplot2.ggplot(pandas2ri.py2ri(pickups_late)) + \ ggplot2.aes_string(x='lon', y='lat', group='nbhd', fill='relative_percent') + \ ggplot2.geom_polygon() + \ ggplot2.theme(legend_position='bottom') + \ ggplot2.labs(x='', y='', title='Late Night Pickups (% of All Pickups)') p4.save('./plots/late_night_pickups.png', width=5, height=6) # Plot % change (2010 to 2015) in pickups by neighborhood pickups_change = pickups_hdl.groupby(['nbhd', 'borough', 'year'])['passenger_count'].sum().reset_index() pickups_change = pickups_change[pickups_change['year'].isin([2010, 2015])].reset_index() pickups_change = pickups_change.sort_values(['nbhd', 'year']).reset_index() temp_change = pickups_change.groupby(['nbhd'])['passenger_count'].apply(lambda x: x.pct_change()).reset_index() temp_change = temp_change.rename(columns={'passenger_count':'percent_change'}) pickups_change = pd.concat([pickups_change, temp_change['percent_change']], axis=1) pickups_change = pickups_change.dropna() # Find top 10 neighborhoods with largest percent change print(tabulate(pickups_change[['percent_change', 'nbhd',