def plot_combo(combo, outname, mids): ggdat = {"data": combo, "outname": outname, "mids": mids} command = """ outname = jdata$outname mids = jdata$mids # labs = c("Gene", "Repeat_content") # names(labs) = c("Genes", "Repeats") scale = 2 pdf(outname, height=10*4*scale, width=20*scale) aplot = ggplot(data = data, aes(plotpos, true_density)) + geom_point() + xlab("Chromosome") + ylab("Features per basepair") + ## y label from qqman::qq #scale_color_manual(values = c(gray(0.5), gray(0))) + ## instead of colors, go for gray ggtitle("Chromosome-wide feature density") + theme_bw() + #scale_y_continuous(breaks=seq(from=min(data$true_density), to=max(data$true_density), length.out=3)) + facet_grid(Feature~., scales="free_y") + scale_x_continuous(breaks = mids$mid, labels = mids$Scaffold_number) + ## add new x labels theme(text = element_text(size=32)) print(aplot) dev.off() # facet_grid(Feature~., scales="free_y", labeller=labeller(Feature=labs)) + # facet_wrap(.~NAME, ncol = 2) + # guides(colour=FALSE) + ## remove legend """ pygg.ggplot(ggdat, command)
def testLibs(self): p = pygg.ggplot('diamonds', pygg.aes(x='carat', y='price')) p += pygg.geom_point() tmpfile = tempfile.NamedTemporaryFile(suffix='.pdf').name pygg.ggsave(tmpfile, p, data=None, libs=['grid'], quiet=True) self.assertTrue(os.path.exists(tmpfile)) self.assertTrue(os.path.getsize(tmpfile) > 0)
def testBasicDataggplot(self): data = [dict(x=x, y=y) for x, y in zip(range(10), range(10))] p = pygg.ggplot(data, pygg.aes(x='x', y='y')) p += pygg.geom_point() p += pygg.geom_smooth() p += pygg.ggtitle(pygg.esc('Test title')) self.check_ggsave(p)
def plot_combo(combo, outname, mids): ggdat = {"data": combo, "outname": outname, "mids": mids} command = """ library(plyr) outname = jdata$outname mids = jdata$mids # labs = c("Gene", "Repeat_content") # names(labs) = c("Genes", "Repeats") scale = 2 print(str(data$Feature)) print(levels(factor(data$Feature))) temp = factor(data$Feature) print(str(temp)) print(levels(factor(temp))) temp = revalue(temp, c("Low_complexity" = "Low\ncomp-\nlexity", "Simple_repeat" = "Simple\nrepeat")) print(str(temp)) print(levels(factor(temp))) data$Feature = as.character(temp) print(str(data$Feature)) print(levels(factor(data$Feature))) lf_feature = levels(factor(data$Feature)) dummy = data.frame(Feature=lf_feature, true_density = 0, plotpos = min(data$plotpos)) pdf(outname, height=3*4.6*scale, width=20*scale) aplot = ggplot(data = data, aes(plotpos, true_density)) + geom_point() + xlab("Chromosome") + ylab("Feature bases per genome base") + ## y label from qqman::qq #scale_color_manual(values = c(gray(0.5), gray(0))) + ## instead of colors, go for gray #ggtitle("Chromosome-wide feature density") + theme_bw() + scale_y_continuous(labels = function(x) sprintf("%.5f", x)) + #scale_y_continuous(labels = function(x) format(x, scientific = TRUE)) + #scale_y_continuous(breaks=seq(from=min(data$true_density), to=max(data$true_density), length.out=3)) + facet_grid(Feature~., scales="free_y") + scale_x_continuous(breaks = mids$mid, labels = mids$Scaffold_number) + ## add new x labels geom_blank(data=dummy) + theme(text = element_text(size=48), panel.spacing = unit(1.8, "lines"), plot.margin = unit(c(.5,.5,.5,.5), "cm")) print(aplot) dev.off() # facet_grid(Feature~., scales="free_y", labeller=labeller(Feature=labs)) + # facet_wrap(.~NAME, ncol = 2) + # guides(colour=FALSE) + ## remove legend """ pygg.ggplot(ggdat, command)
def testPandasDFggplot(self): data = pandas.read_csv(StringIO.StringIO(IRIS_DATA_CSV)) self.assertIsInstance(data, pandas.DataFrame) p = pygg.ggplot( data, pygg.aes(x='SepalLength', y='PetalLength', color='Name')) p += pygg.geom_point() p += pygg.geom_smooth() p += pygg.ggtitle(pygg.esc('Test title')) self.check_ggsave(p)
def testPandasDF(self): data = pandas.read_csv(StringIO.StringIO(IRIS_DATA_CSV)) self.assertIsInstance(data, pandas.DataFrame) p = pygg.ggplot('data', pygg.aes(x='SepalLength', y='PetalLength', color='Name')) p += pygg.geom_point() p += pygg.geom_smooth() p += pygg.ggtitle(pygg.esc('Test title')) self.check_ggsave(p, data)
def testE2E(self): """Test end-to-end creation of figures with outputs to pdf and png""" p = pygg.ggplot('diamonds', pygg.aes(x='carat', y='price', color='clarity')) p += pygg.geom_point(alpha=0.5, size = .75) p += pygg.scale_x_log10() p += pygg.theme_bw() self.check_ggsave(p, None, ext=".pdf") self.check_ggsave(p, None, ext=".png") self.check_ggsave(p, None, ext=".jpg")
def testE2E(self): """Test end-to-end creation of figures with outputs to pdf and png""" p = pygg.ggplot('diamonds', pygg.aes(x='carat', y='price', color='clarity')) p += pygg.geom_point(alpha=0.5, size=.75) p += pygg.scale_x_log10() p += pygg.theme_bw() self.check_ggsave(p, None, ext=".pdf") self.check_ggsave(p, None, ext=".png") self.check_ggsave(p, None, ext=".jpg")
def testIPython(self): """Test that gg_ipython returns a IPython formatted Image""" p = pygg.ggplot('diamonds', pygg.aes(x='carat', y='price')) p += pygg.geom_point() img = pygg.gg_ipython(p, data=None) self.assertIsNotNone(img.data) self.assertEqual(img.format, "jpeg") self.assertEqual(img.width, pygg.IPYTHON_IMAGE_SIZE) self.assertEqual(img.height, pygg.IPYTHON_IMAGE_SIZE) img = pygg.gg_ipython(p, data=None, width=600, height=400) self.assertEqual(img.width, 600) self.assertEqual(img.height, 400) img = pygg.gg_ipython(p, data=None, width=600) self.assertEqual(img.width, 600) self.assertEqual(img.height, 600)
def testNativeRDataset(self): p = pygg.ggplot('diamonds', pygg.aes(x='carat', y='carat')) + pygg.geom_point() self.check_ggsave(p, None)
def testLimits(self): p = pygg.ggplot('diamonds', pygg.aes(x='carat', y='price', color='clarity')) p += pygg.geom_point(alpha=0.5, size=.75) p += pygg.scale_x_log10(limits=[1, 2]) self.check_ggsave(p, None)
#!/usr/bin/env python3 import pandas as pd import pygg import json data = pd.read_csv("aeds_prot.txt", sep="\t", header=0) data = data.sort_values("aed") datalen = data.shape[0] data["frac"] = [float(x) / datalen for x in range(data.shape[0])] print(data) jdata = {"data": data} ggc = """ a = ggplot(data=data, aes(aed, frac)) + geom_line() + geom_vline(xintercept=.5, linetype="dashed") + theme_bw() + labs(y="Cumulative proportion of all transcripts", x="AED", title="Annotation AED scores") pdf("aeds4_final.pdf", height=3, width=4) print(a) dev.off() """ pygg.ggplot(jdata, ggc)
def testBadGGPlotFails(self): p = pygg.ggplot('diamonds', pygg.aes(x='MISSING')) + pygg.geom_point() with self.assertRaises(ValueError): tmpfile = tempfile.NamedTemporaryFile(suffix=".png").name pygg.ggsave(tmpfile, p, data=None, quiet=True)
def testFacets2(self): p = pygg.ggplot('diamonds', pygg.aes(x='carat', y='price')) p += pygg.geom_point() p += pygg.facet_wrap("~clarity") self.check_ggsave(p, None)
def testLimits(self): p = pygg.ggplot('diamonds', pygg.aes(x='carat', y='price', color='clarity')) p += pygg.geom_point(alpha=0.5, size = .75) p += pygg.scale_x_log10(limits=[1, 2]) self.check_ggsave(p, None)
def plot_manhat(combo, outname, mids, val_col, title="Manhattan plot", dims=(20.0, 10.0), scale=2, text_size=32, xname="Chromosome", yname="Value", color_col=None, facet_col=None): ggdat = { "data": combo, "outname": outname, "mids": mids, "title": title, "dims": dims, "scale": scale, "text_size": text_size, "xname": xname, "yname": yname, "color_col": color_col, "facet_col": facet_col } preamble = """ outname = jdata$outname mids = jdata$mids scale = jdata$scale pdf(outname, height=jdata$dims[2]*scale, width=jdata$dims[1]*scale) """ if color_col: aes = "aes(plotpos, " + val_col + ", color = " + color_col + ")" else: aes = "aes(plotpos, " + val_col + ")" plot_command = """ aplot = ggplot(data = data, """ + aes + """) + geom_point() + xlab(jdata$xname) + ylab(jdata$yname) + ## y label from qqman::qq #scale_color_manual(values = c(gray(0.5), gray(0))) + ## instead of colors, go for gray ggtitle(jdata$title) + theme_bw() + #scale_y_continuous(breaks=seq(from=min(data$""" + val_col + """), to=max(data$""" + val_col + """), length.out=3)) + scale_x_continuous(breaks = mids$mid, labels = mids$Scaffold_number) + ## add new x labels theme(text = element_text(size=jdata$text_size))""" if facet_col: plot_command = plot_command + """ + facet_grid(""" + facet_col + """~., scales="free_y")""" plot_command = plot_command + "\n" ending = """ print(aplot) dev.off() # facet_grid(Feature~., scales="free_y", labeller=labeller(Feature=labs)) + # facet_wrap(.~NAME, ncol = 2) + # guides(colour=FALSE) + ## remove legend """ command = preamble + plot_command + ending pygg.ggplot(ggdat, command)