# aes_string: set the axis labels and what we're plotting # opts: sets thickness of the lines # note the use of **{} to allow setting "legend.key.size" as a keyword # scale_colour_manual: associate color datasets with actual colors and names # geom_point and geom_line: thicker points and lines # scale_linetype_manual: associate perf types with linetypes for col_i, yscale in enumerate(['log', 'linear']): vp = grid.viewport(**{'layout.pos.col': col_i + 1, 'layout.pos.row': 1}) pp = ggplot2.ggplot(df) + \ ggplot2.aes_string(x='variable', y='Performance', color='color', shape='PerfType', linetype='PerfType') + \ ggplot2.ggtitle('Performance vs. Color') + \ ggplot2.theme(**{'legend.key.size' : ro.r.unit(1.4, "lines") } ) + \ ggplot2.scale_colour_manual("Color", values=colormap, breaks=colormap.names, labels=[elt[1] for elt in colormap_labels]) + \ ggplot2.geom_point(size=3) + \ ggplot2.scale_linetype_manual(values=linemap) + \ ggplot2.geom_line(size=1.5) # custom y-axis lines: major lines ("breaks") are every 10^n; 9 # minor lines ("minor_breaks") between major lines if (yscale == 'log'): pp = pp + \ ggplot2.scale_y_log10(breaks = ro.r("10^(%d:%d)" % (gflops_range[0], gflops_range[1])), minor_breaks = ro.r("rep(10^(%d:%d), each=9) * rep(1:9, %d)" % (gflops_range[0] - 1, gflops_range[1],
# opts: set the title and the thickness of the lines # note the use of **{} to allow setting "legend.key.size" as a keyword # scale_colour_manual: associate color datasets with actual colors and names # geom_point and geom_line: thicker points and lines # scale_linetype_manual: associate perf types with linetypes for col_i, yscale in enumerate(['log', 'linear']): vp = grid.viewport(**{'layout.pos.col':col_i+1, 'layout.pos.row': 1}) pp = ggplot2.ggplot(df) + \ ggplot2.aes_string(x='Date', y='Performance', color='color', shape='PerfType', linetype='PerfType') + \ ggplot2.opts(**{'title' : 'Performance vs. Color', 'legend.key.size' : ro.r.unit(1.4, "lines") } ) + \ ggplot2.scale_colour_manual("Color", values=colormap, breaks=colormap.names, labels=[elt[1] for elt in colormap_labels]) + \ ggplot2.geom_point(size=3) + \ ggplot2.scale_linetype_manual(values=linemap) + \ ggplot2.geom_line(size=1.5) # custom y-axis lines: major lines ("breaks") are every 10^n; 9 # minor lines ("minor_breaks") between major lines if (yscale == 'log'): pp = pp + \ ggplot2.scale_y_log10(breaks = ro.r("10^(%d:%d)" % (gflops_range[0], gflops_range[1])), minor_breaks = ro.r("rep(10^(%d:%d), each=9) * rep(1:9, %d)" % (gflops_range[0] - 1, gflops_range[1],
def plot_volcano_with_r( data, xlabel='Estimated effect (change in H/L ratio)', title='', max_labels=20, color_background='#737373', color_significant='#252525', color_significant_muted='#252525', label_only_large_fc=False, special_labels=None, special_palette=None, base_size=12, label_size=3, x='logFC', y='neg_log10_p_adjust', special_labels_mode='all', xlim=None, skip_labels=None, nudges=None, ): r_data, r_like_data = transform_data_for_ggplot( data, label_only_large_fc=label_only_large_fc, special_labels=special_labels, max_labels=max_labels, special_labels_mode=special_labels_mode, skip_labels=skip_labels, nudges=nudges) plot = r_ggplot2.ggplot(r_data) plot += r_ggplot2.theme_minimal(base_size=base_size) plot += r_ggplot2.theme( **{ 'panel.grid.major': r_ggplot2.element_blank(), 'panel.grid.minor': r_ggplot2.element_blank(), 'panel.border': r_ggplot2.element_rect(fill=robjects.rinterface.NA, color="black") }) plot += r_ggplot2.theme( text=r_ggplot2.element_text(family='Helvetica', face='plain')) plot += r_ggplot2.theme( **{ 'plot.title': r_ggplot2.element_text(hjust=0.5), # 'axis.title.y': r_ggplot2.element_text((t = 0, r = 20, b = 0, l = 0)), }) aes_points = r_ggplot2.aes_string(x=x, y=y, color='group') scale_points = r_ggplot2.scale_colour_manual( aes_points, values=r_label_palette( r_like_data, special_palette, color_background=color_background, color_significant=color_significant, color_significant_muted=color_significant_muted)) plot += aes_points plot += scale_points if xlim is not None: plot += r_ggplot2.scale_x_continuous( labels=r_custom.formatterFunTwoDigits, limits=robjects.r.c(*xlim)) else: plot += r_ggplot2.scale_x_continuous( labels=r_custom.formatterFunTwoDigits) plot += r_ggplot2.scale_y_continuous(labels=r_custom.formatterFunOneDigit) plot += r_ggplot2.geom_hline( yintercept=float(-np.log10(FDR_THRESHOLD_RESPONSE)), color='#BDBDBD', alpha=.3) plot += r_ggplot2.geom_vline(xintercept=float(FC_THRESHOLD_RESPONSE), color='#BDBDBD', alpha=.3) plot += r_ggplot2.geom_vline(xintercept=-float(FC_THRESHOLD_RESPONSE), color='#BDBDBD', alpha=.3) plot += r_ggplot2.geom_point(**{'show.legend': False}) aes_text = r_ggplot2.aes_string(label='label') plot += aes_text plot += r_ggrepel.geom_text_repel( aes_text, nudge_x=r_dollar(r_data, 'nudgex'), nudge_y=r_dollar(r_data, 'nudgey'), size=label_size, family='Helvetica', **{ 'show.legend': False, 'point.padding': 0.25, 'min.segment.length': 0, #'max.iter':0, 'segment.color': '#BDBDBD' }, ) plot += r_ggplot2.labs(x=xlabel, y='Adjusted p value (-log10)', title=title) plot.plot()
group='variable', colour='variable', shape='variable', linetype='variable') line = ggplot2.geom_line() point = ggplot2.geom_point() vert_line_onset = ggplot2.geom_vline(xintercept=-1.5, linetype=2, colour="#999999") vert_line_exhaust = ggplot2.geom_vline(xintercept=5.5, linetype=2, colour="#999999") vert_line_exhaust_FL = ggplot2.geom_vline(xintercept=3.5, linetype=2, colour="#999999") colors = ggplot2.scale_colour_manual(values=robjects.r.palette_lines) hollow = ggplot2.scale_shape_manual( values=robjects.r('c(16,17,15,18,6,7,9,3)')) xlab = ggplot2.labs(x="Months Since First UI Check") loc_default = robjects.r('c(1,0)') legend_f = lambda loc=loc_default: ggplot2.theme(**{ 'legend.position': loc, 'legend.justification': loc }) ggsave = lambda filename, plot: robjects.r.ggsave( filename="../out/" + filename + ".pdf", plot=plot, width=6, height=4) colors_alt = ggplot2.scale_colour_manual(values=robjects.r.palette_lines[1]) shape_alt = ggplot2.scale_shape_manual(values=17) ggplot2_env = robjects.baseenv['as.environment']('package:ggplot2')
#alternatives that tried and failed #base_plot = lambda gr_name = 'variable': ggplot2.aes_string(x='x', y='value',group=gr_name,colour=gr_name, shape = gr_name) #colors = ggplot2.scale_colour_manual(values=robjects.r.palette_lines, name = ltitle) pandas2ri.activate() #set up basic, repetitive plot features base_plot = ggplot2.aes_string(x='x', y='value',group='variable',colour='variable', shape = 'variable') line = ggplot2.geom_line() point = ggplot2.geom_point() bar = ggplot2.geom_bar(stat="identity") vert_line_onset = ggplot2.geom_vline(xintercept=-1, linetype=2, colour="red", alpha=0.25) vert_line_exhaust = ggplot2.geom_vline(xintercept=5, linetype=2, colour="red", alpha=0.25) ltitle = "crazy" ltitle_default = 'Variable' #colors = lambda ltitle = ltitle_default: ggplot2.scale_colour_manual(values=robjects.r.palette_lines, name = ltitle) colors = ggplot2.scale_colour_manual(values=robjects.r.palette_lines) legend_t_c = lambda ltitle = ltitle_default: ggplot2.scale_color_discrete(name = ltitle) legend_t_s = lambda ltitle = ltitle_default: ggplot2.scale_shape_discrete(name = ltitle) loc_default = robjects.r('c(1,0)') legend_f = lambda loc = loc_default: ggplot2.theme(**{'legend.position':loc, 'legend.justification':loc}) ggsave = lambda filename, plot: robjects.r.ggsave(filename=out_path + filename + ".pdf", plot=plot, width = 6, height = 4) colors_alt = ggplot2.scale_colour_manual(values=robjects.r.palette_lines[1]) shape_alt = ggplot2.scale_shape_manual(values=17) #create a new function straight from the R enviornment #xx in principle it should be possible to load class using what's already been imported #above, but I'm not going to worry about that for now ggplot2_env = robjects.baseenv['as.environment']('package:ggplot2') class GBaseObject(robjects.RObject):
p.save('./plots/taxi_pickups%02i.png' % i, width=4, height=4.5) # Create animated .gif of pickups by hour import imageio file_names = sorted((fn for fn in os.listdir('./plots') if fn.startswith('taxi_pickups'))) file_names = ['plots/' + s for s in file_names] images = [] for filename in file_names: images.append(imageio.imread(filename)) imageio.mimsave('./plots/pickups_movie.gif', images, duration=0.4) # total pickups by date, color p1 = ggplot2.ggplot(pandas2ri.py2ri(date_avgs)) + \ ggplot2.aes_string(x='date', y='total_pickups', color='type') + \ ggplot2.scale_colour_manual(values = robjects.StrVector(['green', 'yellow'])) + \ ggplot2.geom_line() + \ ggplot2.theme(legend_position='bottom') + \ ggplot2.labs(y='Total Pickups', x='Date', title='Total Pickups by Date') p1.save('./plots/pickups_by_date.png', width=6, height=5) # average fare and tip by date, color p2 = ggplot2.ggplot(pandas2ri.py2ri(date_avgs)) + \ ggplot2.aes_string(x='date', y='fare_amount', color='type') + \ ggplot2.scale_colour_manual(values = robjects.StrVector(['green', 'yellow'])) + \ ggplot2.geom_line() + \ ggplot2.theme(legend_position='bottom') + \ ggplot2.labs(y='Average Fare ($)', x='Date', title='Average Fare by Date') p2.save('./plots/fares_by_date.png', width=6, height=5) p3 = ggplot2.ggplot(pandas2ri.py2ri(date_avgs)) + \