Last active
March 9, 2016 02:33
-
-
Save andypetrella/451263d8dc92e25d8341 to your computer and use it in GitHub Desktop.
Super basic qplot implementation on Scala collection, using R backend as renderer... in 74 lines :-)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata" : { | |
"name" : "QPlot", | |
"user_save_timestamp" : "1970-01-01T01:00:00.000Z", | |
"auto_save_timestamp" : "1970-01-01T01:00:00.000Z", | |
"language_info" : { | |
"name" : "scala", | |
"file_extension" : "scala", | |
"codemirror_mode" : "text/x-scala" | |
}, | |
"trusted" : true, | |
"customLocalRepo" : null, | |
"customRepos" : null, | |
"customDeps" : null, | |
"customImports" : null, | |
"customArgs" : null, | |
"customSparkConf" : null | |
}, | |
"cells" : [ { | |
"metadata" : { | |
"id" : "2D571E3F82424F4080D5BB2E222D57C1" | |
}, | |
"cell_type" : "markdown", | |
"source" : "# QPlot" | |
}, { | |
"metadata" : { | |
"id" : "8DFC9A1BAD974742BE5B1E25828F19F7" | |
}, | |
"cell_type" : "markdown", | |
"source" : "> Warning:\n>\n> You'll need `R` install locally, \n> you'll also need `ggplot2` \n> * ```install.packages(\"ggplot2\")```\n> * or on ubuntu, ```apt-get install r-cran-ggplot2```.\n>" | |
}, { | |
"metadata" : { | |
"trusted" : true, | |
"input_collapsed" : false, | |
"collapsed" : false, | |
"id" : "106C73B4ED4A47E4AC31C2176A5467B9" | |
}, | |
"cell_type" : "code", | |
"source" : "import scala.language.postfixOps\nimport SamplerImplicits._\ndef qplot[C:ToPoints:Sampler](\n c:C, \n x:String=\"\", y:String=\"\", \n geom:List[String]=Nil, fill:String=\"\", \n method:String=\"\", formula:String=\"\", color:String=\"\",\n main:String=\"\", xlab:String=\"\", ylab:String=\"\",\n width:String=\"150px\", height:String=\"150px\"\n) = {\n val toPoints = implicitly[ToPoints[C]]\n val points = toPoints(c, toPoints.count(c).toInt)\n val ps = points.head.headers.zipWithIndex.map { case (h, i) =>\n h → points.map(_.values(i))\n }\n val cols = ps.map { case (h, vs) =>\n val arr = vs.map(v => toJson(v).toString)\n s\"\"\"\n $h <- c(${arr.mkString(\",\")})\n \"\"\"\n }.mkString(\"\\n\")\n val r = s\"\"\"\n $cols\n\n df <- data.frame(${ps.map(_._1).mkString(\",\")})\n \"\"\"\n import sys.process._\n \n val scriptFile = java.io.File.createTempFile(\"r-script\", \".R\")\n val img = java.io.File.createTempFile(\"r-plot\", \".png\")\n \n def addString(param:String, s:String) = s match {\n case \"\" => \"\"\n case x => s\"\"\", $param = \"$s\" \"\"\"\n }\n \n def addField(param:String, s:String) = s match {\n case \"\" => \"\"\n case x => s\"\"\", $param = $s \"\"\"\n }\n \n val plot = s\"\"\"\n library(ggplot2) \n\n\n q <- qplot(data=df\n ${addField(\"x\", x)}\n ${addField(\"y\", y)}\n ,geom=c(${geom.map(x => \"\\\"\"+x+\"\\\"\").mkString(\",\")}) \n ${addField(\"fill\", fill)}, \n alpha=I(.5), \n main=\"$main\", xlab=\"$xlab\", ylab=\"$ylab\"\n ${addString(\"method\", method)}\n ${addField(\"formula\", formula)}\n ${addField(\"color\", color)}\n )\n ggsave(\"${img.getAbsolutePath}\", plot=q)\n dev.off()\n \"\"\"\n val script = s\"\"\"\n $r\n\n $plot\n \"\"\"\n val w = new java.io.FileWriter(scriptFile)\n w.write(script)\n w.close()\n \n Process(s\"Rscript ${scriptFile.getAbsolutePath}\") !!\n \n val i = widgets.img(\"png\", width, height)\n i.file(img)\n i\n}", | |
"outputs" : [ ] | |
}, { | |
"metadata" : { | |
"id" : "0484E38D76AF4EFA87B9582587728FE7" | |
}, | |
"cell_type" : "markdown", | |
"source" : "#Cars" | |
}, { | |
"metadata" : { | |
"id" : "281343A362F3495399844B78225EC361" | |
}, | |
"cell_type" : "markdown", | |
"source" : "## Data" | |
}, { | |
"metadata" : { | |
"trusted" : true, | |
"input_collapsed" : false, | |
"collapsed" : false, | |
"id" : "E39FD8741B1C4992985C084AE6CB72BE" | |
}, | |
"cell_type" : "code", | |
"source" : "case class Car(model:String, mpg:Double, cyl:String, disp:Double, hp:Double, drat:Double, wt:Double, qsec:Double, vs:Double, am:String, gear:String, carb:Double)", | |
"outputs" : [ ] | |
}, { | |
"metadata" : { | |
"trusted" : true, | |
"input_collapsed" : false, | |
"collapsed" : false, | |
"id" : "23484468D5E745A88AC771249EA3FF3B" | |
}, | |
"cell_type" : "code", | |
"source" : ":sh wget https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv -O /tmp/mtcars.csv", | |
"outputs" : [ ] | |
}, { | |
"metadata" : { | |
"trusted" : true, | |
"input_collapsed" : false, | |
"collapsed" : false, | |
"id" : "45AA664E4979497B82DDA14B0E4A767D" | |
}, | |
"cell_type" : "code", | |
"source" : "val cars = {\n val headers :: lines = scala.io.Source.fromFile(\"/tmp/mtcars.csv\").getLines.toList\n \n lines.map(_.split(\",\").toList).map{case model::mpg::cyl::disp::hp::drat::wt::qsec::vs::am::gear::carb::Nil => \n Car(model,mpg.toDouble,cyl+\"cyl\",disp.toDouble,hp.toDouble,drat.toDouble,wt.toDouble,\n qsec.toDouble,vs.toDouble,if(am.toInt==1)\"Manual\"else\"Automatic\",gear+\"gears\",carb.toDouble)\n case _ => ???\n }\n}", | |
"outputs" : [ ] | |
}, { | |
"metadata" : { | |
"id" : "6CE76626C59340C59C9CC6ECE201598A" | |
}, | |
"cell_type" : "markdown", | |
"source" : "## Plots" | |
}, { | |
"metadata" : { | |
"id" : "A9C2E8E7C91343868AC1E50582DEFF49" | |
}, | |
"cell_type" : "markdown", | |
"source" : "Based on the example page on [qplot](http://www.statmethods.net/advgraphs/ggplot2.html)." | |
}, { | |
"metadata" : { | |
"trusted" : true, | |
"input_collapsed" : false, | |
"collapsed" : false, | |
"id" : "D21749B5D65C4AE8819E68FF8457FB22" | |
}, | |
"cell_type" : "code", | |
"source" : "qplot(cars, x=\"mpg\", geom=List(\"density\"), fill=\"gear\", main=\"Distribution of Gas Milage\", xlab=\"Miles Per Gallon\", ylab=\"Density\")", | |
"outputs" : [ ] | |
}, { | |
"metadata" : { | |
"trusted" : true, | |
"input_collapsed" : false, | |
"collapsed" : false, | |
"id" : "0A2A8CCF1B614B068E369219049886D6" | |
}, | |
"cell_type" : "code", | |
"source" : "qplot(cars, x=\"wt\", y=\"mpg\", geom=List(\"point\", \"smooth\"), \n method=\"lm\", formula=\"y~x\", color=\"cyl\", \n main=\"Regression of MPG on Weight\", \n xlab=\"Weight\", ylab=\"Miles per Gallon\",\n width=\"500px\", height=\"500px\")", | |
"outputs" : [ ] | |
} ], | |
"nbformat" : 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment