Giter Site home page Giter Site logo

ggscala2's Introduction

ggscala2

A Scala package that produces plots using R ggplot2.

Getting Started

%AddDeps org.ddahl rscala_2.11 2.3.1
import org.ddahl.rscala._
%AddDeps com.climate ggscala2_2.11 0.0.3 --repository file:/Users/ricardo.lemos/scala_jars/
import com.climate.ggscala2.Gigi
Gigi.inline = Some((h: String) => kernel.magics.html(h)) // method that converts html into an inline figure in Toree
Gigi.screenScale = 0.25 // smaller inline plots
Marking org.ddahl:rscala_2.11:2.3.1 for download
Preparing to fetch from:
-> file:/var/folders/zx/tybg3qc92z13w8lp81mlr2rjzqbsdw/T/toree_add_deps1862492987876009503/
-> https://repo1.maven.org/maven2
-> New file at /var/folders/zx/tybg3qc92z13w8lp81mlr2rjzqbsdw/T/toree_add_deps1862492987876009503/https/repo1.maven.org/maven2/org/ddahl/rscala_2.11/2.3.1/rscala_2.11-2.3.1.jar
Marking com.climate:ggscala2_2.11:0.0.3 for download
Preparing to fetch from:
-> file:/var/folders/zx/tybg3qc92z13w8lp81mlr2rjzqbsdw/T/toree_add_deps1862492987876009503/
-> file:/Users/ricardo.lemos/scala_jars/
-> https://repo1.maven.org/maven2
-> New file at /Users/ricardo.lemos/scala_jars/com/climate/ggscala2_2.11/0.0.3/ggscala2_2.11-0.0.3.jar

Use the following code to render an example plot:

Gigi("example = ggplot(data.frame(x = 1, y = 2), aes(x = x, y = y)) + geom_point()")
Gigi.print("example")

Gigi is the name of the package's only object, which you call to plot.

Prerequisites

You need the software R (https://www.r-project.org/) installed in your machine. You also need the R packages ggplot2 and grid: in an R terminal, type

install.packages("ggplot2")
install.packages("grid")

To confirm that an R interpreter can be instantiated from Scala, try

val R = org.ddahl.rscala.RClient()

If that does not work, ggscala2 has a workaround: type

Gigi.path2r = "/path/to/R/here"

early in your Scala code, to point rscala in the right direction.

Plotting in ggscala2

There are several built-in types of plots in this package:

Line plot

The instruction

Gigi.lineplot(y = Array(1.3, 2.3, 4.5, 1.7))

produces a simple line plot; at the other extreme,

Gigi.lineplot(x = Option(Array(1.0, 2.0, 1.0, 2.0)),
  y = Array(1.3, 2.3, 4.5, 1.7),
  z = Option(Array("g1", "g1", "g2", "g2")),
  xlab = "predictor", ylab = "response", zlab = "group", title = "ggscala2 lineplot",
  drawPoints = true)

demonstrates all the features you can include in a lineplot.

Scatter plot

Under the hood, ggscala2 employs the same method to build line and scatter plots. End users, however, use a different call

Gigi.scatterplot(x = Array(1.0, 2.0, 1.0, 2.0),
  y = Array(1.3, 2.3, 4.5, 1.7),
  z = Option(Array("g1", "g1", "g2", "g2")),
  xlab = "predictor", ylab = "response", zlab = "group", title = "ggscala2 scatterplot")

Time series plot

Deceptively similar to line plots, time series plots let you add confidence bands to the representation of a single variable whose distribution (mean, variance, etc.) changes over time. If you also have observations, you can add them using the argument z.

import org.joda.time.DateTime
val x: Array[DateTime] = Array("1980-01-15", "1980-01-16", "1980-01-17", "1980-01-18").map(new DateTime(_))
Gigi.timeseriesplot(x = x,
  y = Array(1.3, 2.3, 4.5, 1.7),
  z = Option(Array(1.4, 1.6, 3.6, 1.6)),
  ymin = Option(Array(0.7, 1.7, 3.6, 1.0)),
  ymax = Option(Array(1.7, 2.7, 5.6, 2.0)),
  xlab = "date", ylab = "response", title = "ggscala2 timeseries plot")

Density plot

Here you can compare the distribution of several random variables, for which you have samples: stack them all in a single array, x, and use z to identify the variables.

Gigi.densityplot(x = Array(1.0, 2.0, 2.1, 2.6, 0.7, 1.2, 1.4, 2.0),
  z = Option(Array("v1", "v1", "v1", "v1", "v2", "v2", "v2", "v2")),
  xlab = "", zlab = "variable", title = "ggscala2 densityplot")

Surface plot

To draw a simple 2D surface plot, do

Gigi.surfaceplot(x = Array(1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0),
  y = Array(1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0),
  z = Array(1.2, 2.3, 3.4, 1.4, 2.1, 2.8, 1.1, 2.0, 3.7),
  xlab = "longitude", ylab = "latitude", zlab = "height")

The color scheme changes automatically if z includes negative and positive values

Gigi.surfaceplot(x = Array(1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0),
  y = Array(1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0),
  z = Array(-1.2, 2.3, 3.4, -1.4, 2.1, 2.8, -1.1, 2.0, 3.7),
  xlab = "longitude", ylab = "latitude", zlab = "height")

You can add as many layers to a surface plot as you would like. For example, here we overlay a surface with a contour, vectors, points, and text.

val x = Array(1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0)
val y = Array(1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0)
val z = Array(-1.2, 2.3, 3.4, -1.4, 2.1, 2.8, -1.1, 2.0, 3.7)
Gigi.surfaceplot(
  layerType = List("surface", "contour", "vectors", "points", "text"),
  x = List(x, x, x, x, x),
  y = List(y, y, y, y, y),
  z = List(z, z, x.map(_ * 0.1) ++ y.map(_ * 0.1), z.map(_ * 0.5), z),
  text = Option(z.map(_.toString)),
  xlab = "longitude", ylab = "latitude", zlab = "height")

Reliability plot

To draw a Brier Score reliability plot (a.k.a. reliability diagram), do

Gigi.reliabilityplot(x = Array(0.1, 0.2, 0.1, 0.5, 0.3, 0.8, 0.9, 0.8, 0.05, 0.0, 0.99, 0.2), 
  y = Array(false, false, false, false, false, false, true, true, false, false, true, false))

Multipanel figures

There is a straightforward way of creating multipanel figures

Gigi.split(2,2)
Gigi.spool("lineplot", Option(0), Option(0))
Gigi.spool("timeseriesplot", Option(0), Option(1))
Gigi.spool("scatterplot", Option(1), Option(0))
Gigi.spool("densityplot", Option(1), Option(1))
Gigi.print()

Manual edits to built-in plots

Not pleased with the look of your built-in plot? Ask Gigi to change it.

Gigi("densityplot = densityplot + theme(legend.position='bottom')")
Gigi.print("densityplot")

You can change the color palettes for your plots:

Gigi.paletteZeroExtreme.changeColor(Array("yellow", "dark green"))
Gigi.changePalettes() // to activate change
Gigi.surfaceplot(x = Array(1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0),
  y = Array(1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0),
  z = Array(1.2, 2.3, 3.4, 1.4, 2.1, 2.8, 1.1, 2.0, 3.7),
  xlab = "longitude", ylab = "latitude", zlab = "height")

Similar operations can be applied to paletteLine, paletteFill, and paletteNegZeroPos.

Build your own plots

In the "Getting started" section we saw that it is possible to create a simple plot by sending all the R commands to Gigi. You can also plot data from your Scala session, using the set method, and you are not limited to the types of plots displayed above -- any instruction recognized by ggplot2 is available.

Gigi.set("x", Array(1,1,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,5,6,6,6,7,7,7,7,8,8,9,9))
Gigi("myplot = ggplot(data.frame(x=x), aes(x=x)) + geom_histogram(binwidth=2)")
Gigi.print("myplot")

Authors

License

Copyright (C) 2017 The Climate Corporation. Distributed under the Apache License, Version 2.0. You may not use this library except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

See the NOTICE file distributed with this work for additional information regarding copyright ownership. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

ggscala2's People

Contributors

rtltcc avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

fikovnik ehyaei

ggscala2's Issues

Add time/date-based x and y scales

Allow to specify break (i.e., day/week/month)

Basically, something like ... + scale_x_date(date_breaks = '1 month', labels = date_format('%b'))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.