Example #1
0
def line_plot(pdf_file,
              data,
              x,
              y,
              var,
              null_label="N/A",
              linetype=None,
              title=None,
              xlab=None,
              ylab=None,
              colorname=None,
              linename=None,
              **extra_aes_params):

    pdf(pdf_file, width=11.7, height=8.3, paper="a4r")
    if any(data[x].isnull()):
        labels = [null_label] + map(str, sorted(set(
            data[data[x].notnull()][x])))
        labels = robjects.StrVector(labels)
        nulls = data[x].isnull()
        label_vals = dict(zip(labels, range(len(labels))))
        data[x] = data[x].astype("str")
        data[x][nulls] = null_label
        data['sortcol'] = data[x].map(label_vals.__getitem__)
        data.sort('sortcol', inplace=True)
    else:
        labels = None

    if linetype and linetype != var:
        data['group'] = data[var].map(str) + data[linetype].map(str)
    else:
        data['group'] = data[var]

    rdata = common.convert_to_r_dataframe(data)
    if labels:
        ix = rdata.names.index(x)
        rdata[ix] = ordered(rdata[ix], levels=labels)

    gp = gg2.ggplot(rdata)
    pp = (
        gp + gg2.geom_point(size=3) +
        gg2.scale_colour_hue(name=(colorname or var)) +
        #gg2.scale_colour_continuous(low="black") +
        gg2.aes_string(x=x, y=y, color=var, variable=var) +
        ggtitle(title or "") + xlabel(xlab or x) + ylabel(ylab or y)  #+
        #gg2.scale_y_continuous(breaks=seq(0.0, 1.0, 0.05))
    )

    # line type stuff
    if linetype:
        pp += gg2.geom_path(gg2.aes_string(group='group', linetype=linetype),
                            size=0.5)
        pp += gg2.scale_linetype(name=(linename or linetype))
    else:
        pp += gg2.geom_path(gg2.aes_string(group='group'), size=0.5)

    pp.plot()
    dev_off()
Example #2
0
def line_plot(
    pdf_file,
    data,
    x,
    y,
    var,
    null_label="N/A",
    linetype=None,
    title=None,
    xlab=None,
    ylab=None,
    colorname=None,
    linename=None,
    **extra_aes_params
):

    pdf(pdf_file, width=11.7, height=8.3, paper="a4r")
    if any(data[x].isnull()):
        labels = [null_label] + map(str, sorted(set(data[data[x].notnull()][x])))
        labels = robjects.StrVector(labels)
        nulls = data[x].isnull()
        label_vals = dict(zip(labels, range(len(labels))))
        data[x] = data[x].astype("str")
        data[x][nulls] = null_label
        data["sortcol"] = data[x].map(label_vals.__getitem__)
        data.sort("sortcol", inplace=True)
    else:
        labels = None

    if linetype and linetype != var:
        data["group"] = data[var].map(str) + data[linetype].map(str)
    else:
        data["group"] = data[var]

    rdata = common.convert_to_r_dataframe(data)
    if labels:
        ix = rdata.names.index(x)
        rdata[ix] = ordered(rdata[ix], levels=labels)

    gp = gg2.ggplot(rdata)
    pp = (
        gp
        + gg2.geom_point(size=3)
        + gg2.scale_colour_hue(name=(colorname or var))
        +
        # gg2.scale_colour_continuous(low="black") +
        gg2.aes_string(x=x, y=y, color=var, variable=var)
        + ggtitle(title or "")
        + xlabel(xlab or x)
        + ylabel(ylab or y)  # +
        # gg2.scale_y_continuous(breaks=seq(0.0, 1.0, 0.05))
    )

    # line type stuff
    if linetype:
        pp += gg2.geom_path(gg2.aes_string(group="group", linetype=linetype), size=0.5)
        pp += gg2.scale_linetype(name=(linename or linetype))
    else:
        pp += gg2.geom_path(gg2.aes_string(group="group"), size=0.5)

    pp.plot()
    dev_off()
Example #3
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 #4
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