Example #1
0
File: test.py Project: dvu4/udacity
     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()
Example #2
0
     ggplot2.geom_abline(intercept = 30) + \
     ggplot2.geom_abline(intercept = 15)
pp.plot()
#-- qplot3addline-end
grdevices.dev_off()

grdevices.png('../../_static/graphics_ggplot2addsmooth.png',
              width=612,
              height=612,
              antialias="subpixel",
              type="cairo")
#-- ggplot2addsmooth-begin
pp = gp + \
     ggplot2.aes_string(x='wt', y='mpg') + \
     ggplot2.geom_point() + \
     ggplot2.stat_smooth(method = 'lm')
pp.plot()

#-- ggplot2addsmooth-end
grdevices.dev_off()

grdevices.png('../../_static/graphics_ggplot2addsmoothmethods.png',
              width=1024,
              height=340,
              antialias="subpixel",
              type="cairo")

#-- ggplot2addsmoothmethods-begin
grid.newpage()
grid.viewport(layout=grid.layout(1, 3)).push()
Example #3
0
     ggplot2.aes_string(x='wt', y='mpg') + \
     ggplot2.geom_point() + \
     ggplot2.geom_abline(intercept = 30) + \
     ggplot2.geom_abline(intercept = 15)
pp.plot()
#-- qplot3addline-end
grdevices.dev_off()


grdevices.png('../../_static/graphics_ggplot2addsmooth.png',
              width = 612, height = 612, antialias="subpixel", type="cairo")
#-- ggplot2addsmooth-begin
pp = gp + \
     ggplot2.aes_string(x='wt', y='mpg') + \
     ggplot2.geom_point() + \
     ggplot2.stat_smooth(method = 'lm')
pp.plot()

#-- ggplot2addsmooth-end
grdevices.dev_off()


grdevices.png('../../_static/graphics_ggplot2addsmoothmethods.png',
              width = 1024, height = 340, antialias="subpixel", type="cairo")

#-- ggplot2addsmoothmethods-begin
grid.newpage()
grid.viewport(layout=grid.layout(1, 3)).push()

params = (('lm', 'y ~ x'),
          ('lm', 'y ~ poly(x, 2)'),
def plot_collectors_curve(args, start_times, read_lengths):
	"""
	Use rpy2 to create a collectors curve of the run
	"""
	r = robjects.r
	r.library("ggplot2")
	grdevices = importr('grDevices')

	# set t_0 as the first measured time for the read.
	t_0 = start_times[0]

	# adjust times to be relative to t_0
	r_start_times = robjects.FloatVector([float(t - t_0) / float(3600) + 0.00000001 \
		for t in start_times])
	r_read_lengths = robjects.IntVector(read_lengths)

	# compute the cumulative based on reads or total base pairs
	if args.plot_type == 'reads':
		y_label = "Total reads"
		cumulative = \
			r.cumsum(robjects.IntVector([1] * len(start_times)))
	elif args.plot_type == 'basepairs':
		y_label = "Total base pairs"
		cumulative = r.cumsum(r_read_lengths)

	# make a data frame of the lists
	d = {'start': r_start_times, 
		'lengths': r_read_lengths,
		'cumul': cumulative}
	df = robjects.DataFrame(d)

	if args.savedf:
		robjects.r("write.table")(df, file=args.savedf, sep="\t")

	# title
	total_reads = len(read_lengths)
	total_bp = sum(read_lengths)
	plot_title = "Yield: " \
		+ str(total_reads) + " reads and " \
		+ str(total_bp) + " base pairs."

	# plot
	gp = ggplot2.ggplot(df)
	pp = gp + ggplot2.aes_string(x='start', y='cumul') \
		+ ggplot2.geom_step(size=2) \
		+ ggplot2.scale_x_continuous('Time (hours)') \
		+ ggplot2.scale_y_continuous(y_label) \
		+ ggplot2.ggtitle(plot_title)

        # extrapolation
	if args.extrapolate:
		start = robjects.ListVector({'a': 1, 'b': 1})
                pp = pp + ggplot2.stat_smooth(fullrange='TRUE', method='nls',
                                              formula='y~a*I((x*3600)^b)',
                                              se='FALSE', start=start) \
                        + ggplot2.xlim(0, float(args.extrapolate))

	if args.theme_bw:
		pp = pp + ggplot2.theme_bw()	

	if args.saveas is not None:
		plot_file = args.saveas
		if plot_file.endswith(".pdf"):
			grdevices.pdf(plot_file, width = 8.5, height = 8.5)
		elif plot_file.endswith(".png"):
			grdevices.png(plot_file, width = 8.5, height = 8.5, 
				units = "in", res = 300)
		else:
			logger.error("Unrecognized extension for %s!" % (plot_file))
			sys.exit()

		pp.plot()
		grdevices.dev_off()
	else:
		pp.plot()
		# keep the plot open until user hits enter
		print('Type enter to exit.')
		raw_input()
Example #5
0
y_lab = r("expression(Discharge (m^{3}/s))")
x_lab = r("expression(Area (km^{2}))")
annotate1 = r('annotate("text", x = '+str(max(areas)-30)+', y = 0.5, color = "red", label = "Mean Annual", parse=FALSE)')
annotate2 = r('annotate("text", x = '+str(max(areas)-30)+', y = 0.42, label = "'+r_sq_lab+'", color = "red", parse=TRUE)')
annotate3 = r('annotate("text", x = '+str(max(areas)-30)+', y = 0.34, label = "slope~'+sl+'", color = "red", parse=TRUE)')

annotate4 = r('annotate("text", x = '+str(max(areas)-150)+', y = 0.7, color = "blue", label = "LGM", parse=FALSE)')
annotate5 = r('annotate("text", x = '+str(max(areas)-150)+', y = 0.6, color = "blue", label = "'+r_sq_lab_lgm+'", parse=TRUE)')
annotate6 = r('annotate("text", x = '+str(max(areas)-150)+', y = 0.5, color = "blue", label = "slope~'+sl_lgm+'", parse=TRUE)')

pp = ggplot2.ggplot(dat_frame) + \
    ggplot2.aes_string(y='discharge', x='areas') + \
    ggplot2.ggtitle('Area vs. Sediment Flux') + \
    ggplot2.scale_x_log10(x_lab) + \
    ggplot2.theme_bw() + \
    ggplot2.stat_smooth(method = "lm", formula = 'y ~ x') + \
    ggplot2.scale_y_log10(y_lab) + \
    annotate1 + \
    annotate2 + \
    annotate3 + \
    annotate4 + \
    annotate5 + \
    annotate6 + \
    ggplot2.geom_point(color='blue') + \
    ggplot2.geom_errorbar(ggplot2.aes_string(ymin='min',ymax='max'), data=dat_frame, width=.02, alpha=.3) + \
    ggplot2.geom_point(data=dat_frame2,color='red',show_guide='FALSE' ) + \
    ggplot2.stat_smooth(data=dat_frame2, method = "lm", formula = 'y ~ x', color='red')

grdevices = importr('grDevices')

grdevices.pdf(file="area_qs.pdf")
                          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",
                            method='auto') + \ 
        ggplot2.theme(**{ '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()

""" ----------------------------------------------------------------------------
  
    End note:
        (end note starts here)     
   
============================================================================ """
Example #7
0
def plot_collectors_curve(args, start_times, read_lengths):
    """
	Use rpy2 to create a collectors curve of the run
	"""
    r = robjects.r
    r.library("ggplot2")
    grdevices = importr('grDevices')

    # set t_0 as the first measured time for the read.
    t_0 = start_times[0]

    # adjust times to be relative to t_0
    r_start_times = robjects.FloatVector([float(t - t_0) / float(3600) + 0.00000001 \
     for t in start_times])
    r_read_lengths = robjects.IntVector(read_lengths)

    # compute the cumulative based on reads or total base pairs
    if args.plot_type == 'reads':
        y_label = "Total reads"
        cumulative = \
         r.cumsum(robjects.IntVector([1] * len(start_times)))
    elif args.plot_type == 'basepairs':
        y_label = "Total base pairs"
        cumulative = r.cumsum(r_read_lengths)

    step = args.skip
    # make a data frame of the lists
    d = {
        'start':
        robjects.FloatVector(
            [r_start_times[n] for n in xrange(0, len(r_start_times), step)]),
        'lengths':
        robjects.IntVector(
            [r_read_lengths[n] for n in xrange(0, len(r_read_lengths), step)]),
        'cumul':
        robjects.IntVector(
            [cumulative[n] for n in xrange(0, len(cumulative), step)])
    }
    df = robjects.DataFrame(d)

    if args.savedf:
        robjects.r("write.table")(df, file=args.savedf, sep="\t")

    # title
    total_reads = len(read_lengths)
    total_bp = sum(read_lengths)
    plot_title = "Yield: " \
     + str(total_reads) + " reads and " \
     + str(total_bp) + " base pairs."

    # plot
    gp = ggplot2.ggplot(df)
    pp = gp + ggplot2.aes_string(x='start', y='cumul') \
     + ggplot2.geom_step(size=2) \
     + ggplot2.scale_x_continuous('Time (hours)') \
     + ggplot2.scale_y_continuous(y_label) \
     + ggplot2.ggtitle(plot_title)

    # extrapolation
    if args.extrapolate:
        start = robjects.ListVector({'a': 1, 'b': 1})
        pp = pp + ggplot2.stat_smooth(fullrange='TRUE', method='nls',
                                      formula='y~a*I((x*3600)^b)',
                                      se='FALSE', start=start) \
                + ggplot2.xlim(0, float(args.extrapolate))

    if args.theme_bw:
        pp = pp + ggplot2.theme_bw()

    if args.saveas is not None:
        plot_file = args.saveas
        if plot_file.endswith(".pdf"):
            grdevices.pdf(plot_file, width=8.5, height=8.5)
        elif plot_file.endswith(".png"):
            grdevices.png(plot_file,
                          width=8.5,
                          height=8.5,
                          units="in",
                          res=300)
        else:
            logger.error("Unrecognized extension for %s!" % (plot_file))
            sys.exit()

        pp.plot()
        grdevices.dev_off()
    else:
        pp.plot()
        # keep the plot open until user hits enter
        print('Type enter to exit.')
        raw_input()
Example #8
0
    def plot(self,
             fn,
             x='x',
             y='y',
             col=None,
             group=None,
             w=1100,
             h=800,
             size=2,
             smooth=True,
             point=True,
             jitter=False,
             boxplot=False,
             boxplot2=False,
             title=False,
             flip=False,
             se=False,
             density=False,
             line=False):
        df = self.df
        #import math, datetime

        grdevices = importr('grDevices')

        if not title:
            title = fn.split("/")[-1]

        grdevices.png(file=fn, width=w, height=h)
        gp = ggplot2.ggplot(df)
        pp = gp
        if col and group:
            pp += ggplot2.aes_string(x=x, y=y, col=col, group=group)
        elif col:
            pp += ggplot2.aes_string(x=x, y=y, col=col)
        elif group:
            pp += ggplot2.aes_string(x=x, y=y, group=group)
        else:
            pp += ggplot2.aes_string(x=x, y=y)

        if boxplot:
            if col:
                pp += ggplot2.geom_boxplot(ggplot2.aes_string(fill=col),
                                           color='blue')
            else:
                pp += ggplot2.geom_boxplot(color='blue')

        if point:
            if jitter:
                if col:
                    pp += ggplot2.geom_point(ggplot2.aes_string(fill=col,
                                                                col=col),
                                             size=size,
                                             position='jitter')
                else:
                    pp += ggplot2.geom_point(size=size, position='jitter')
            else:
                if col:
                    pp += ggplot2.geom_point(ggplot2.aes_string(fill=col,
                                                                col=col),
                                             size=size)
                else:
                    pp += ggplot2.geom_point(size=size)

        if boxplot2:
            if col:
                pp += ggplot2.geom_boxplot(ggplot2.aes_string(fill=col),
                                           color='blue',
                                           outlier_colour="NA")
            else:
                pp += ggplot2.geom_boxplot(color='blue')

        if smooth:
            if smooth == 'lm':
                if col:
                    pp += ggplot2.stat_smooth(ggplot2.aes_string(col=col),
                                              size=1,
                                              method='lm',
                                              se=se)
                else:
                    pp += ggplot2.stat_smooth(col='blue',
                                              size=1,
                                              method='lm',
                                              se=se)
            else:
                if col:
                    pp += ggplot2.stat_smooth(ggplot2.aes_string(col=col),
                                              size=1,
                                              se=se)
                else:
                    pp += ggplot2.stat_smooth(col='blue', size=1, se=se)

        if density:
            pp += ggplot2.geom_density(ggplot2.aes_string(x=x, y='..count..'))

        if line:
            pp += ggplot2.geom_line(position='jitter')

        pp += ggplot2.opts(
            **{
                'title': title,
                'axis.text.x': ggplot2.theme_text(size=24),
                'axis.text.y': ggplot2.theme_text(size=24, hjust=1)
            })
        #pp+=ggplot2.scale_colour_brewer(palette="Set1")
        pp += ggplot2.scale_colour_hue()
        if flip:
            pp += ggplot2.coord_flip()

        pp.plot()
        grdevices.dev_off()
        print ">> saved: " + fn
Example #9
0
     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()
Example #10
0
	def plot(self, fn, x='x', y='y', col=None, group=None, w=1100, h=800, size=2, smooth=True, point=True, jitter=False, boxplot=False, boxplot2=False, title=False, flip=False, se=False, density=False, line=False):
		df=self.df
		#import math, datetime
		

		grdevices = importr('grDevices')

		if not title:
			title=fn.split("/")[-1]

		grdevices.png(file=fn, width=w, height=h)
		gp = ggplot2.ggplot(df)
		pp = gp	
		if col and group:
			pp+=ggplot2.aes_string(x=x, y=y,col=col,group=group)
		elif col:
			pp+=ggplot2.aes_string(x=x, y=y,col=col)
		elif group:
			pp+=ggplot2.aes_string(x=x, y=y,group=group)
		else:
			pp+=ggplot2.aes_string(x=x, y=y)	

		if boxplot:
			if col:
				pp+=ggplot2.geom_boxplot(ggplot2.aes_string(fill=col),color='blue')
			else:
				pp+=ggplot2.geom_boxplot(color='blue')	

		if point:
			if jitter:
				if col:
					pp+=ggplot2.geom_point(ggplot2.aes_string(fill=col,col=col),size=size,position='jitter')
				else:
					pp+=ggplot2.geom_point(size=size,position='jitter')
			else:
				if col:
					pp+=ggplot2.geom_point(ggplot2.aes_string(fill=col,col=col),size=size)
				else:
					pp+=ggplot2.geom_point(size=size)


		if boxplot2:
			if col:
				pp+=ggplot2.geom_boxplot(ggplot2.aes_string(fill=col),color='blue',outlier_colour="NA")
			else:
				pp+=ggplot2.geom_boxplot(color='blue')

		if smooth:
			if smooth=='lm':
				if col:
					pp+=ggplot2.stat_smooth(ggplot2.aes_string(col=col),size=1,method='lm',se=se)
				else:
					pp+=ggplot2.stat_smooth(col='blue',size=1,method='lm',se=se)
			else:
				if col:
					pp+=ggplot2.stat_smooth(ggplot2.aes_string(col=col),size=1,se=se)
				else:
					pp+=ggplot2.stat_smooth(col='blue',size=1,se=se)

		if density:
			pp+=ggplot2.geom_density(ggplot2.aes_string(x=x,y='..count..'))

		if line:
			pp+=ggplot2.geom_line(position='jitter')


		pp+=ggplot2.opts(**{'title' : title, 'axis.text.x': ggplot2.theme_text(size=24), 'axis.text.y': ggplot2.theme_text(size=24,hjust=1)} )
		#pp+=ggplot2.scale_colour_brewer(palette="Set1")
		pp+=ggplot2.scale_colour_hue()
		if flip:
			pp+=ggplot2.coord_flip()



		pp.plot()
		grdevices.dev_off()
		print ">> saved: "+fn
Example #11
0
#!/usr/bin/env python2

import cPickle
import numpy
from rpy2.robjects import FloatVector, DataFrame
from rpy2.robjects.lib import ggplot2
from rpy2.robjects.packages import importr

pscores = cPickle.load(open('bootstrap.pickle'))
pscores.sort()
proportion = numpy.linspace(1, len(pscores), len(pscores)) / len(pscores)
dataf = DataFrame({
    'pscore': FloatVector(pscores),
    'proportion': FloatVector(proportion),
})

grdevices = importr('grDevices')
#grdevices.postscript(file="pscores.eps", width=512, height=512)
grdevices.postscript(file='pscores.eps')
(
    ggplot2.ggplot(dataf)
    + ggplot2.aes_string(y='pscore', x='proportion')
    + ggplot2.geom_point()
    + ggplot2.scale_x_log10()
    + ggplot2.scale_y_log10()
    + ggplot2.stat_smooth(method='lm')
).plot()
grdevices.dev_off()