1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
|
.. _graphics:
********
Graphics
********
Introduction
============
This section shows how to make R graphics from rpy2,
using some of the different graphics systems available to R users.
The purpose of this section is to get users going, and be able to figure out
by reading the R documentation how to perform the same plot in rpy2.
.. module:: rpy2.robjects.lib.grdevices
:synopsis: High-level interface with R
Graphical devices
-----------------
With `R`, all graphics are plotted into a so-called graphical device.
Graphical devices can be interactive, like for example `X11`,
or non-interactive, like `png` or `pdf`. Non-interactive devices
appear to be files. It is possible to create custom graphical devices
from Python/rpy2, but this an advanced topic (see :ref:`graphicaldevices-custom`).
By default an interactive R session will open an interactive device
when needing one. If a non-interactive graphical device is needed,
one will have to specify it.
.. note::
Do not forget to close a non-interactive device when done.
This can be required to flush pending data from the buffer.
The module :mod:`grdevices` aims at representing the R package
grDevices*. Example with the R functions *png* and *dev.off*:
.. code-block:: python
from rpy2.robjects.packages import importr
grdevices = importr('grDevices')
grdevices.png(file="path/to/file.png", width=512, height=512)
# plotting code here
grdevices.dev_off()
The package contains an :class:`Environment` :data:`grdevices_env` that
can be used to access an object known to belong to that R packages, e.g.:
>>> palette = grdevices.palette()
>>> print(palette)
[1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow"
[8] "gray"
Getting ready
-------------
To run examples in this section we first import
:mod:`rpy2.robjects` and define few helper
functions.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- setup-begin
:end-before: #-- setup-end
Package *lattice*
=================
Introduction
------------
Importing the package `lattice` is done the
same as it is done for other R packages.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- setuplattice-begin
:end-before: #-- setuplattice-end
Scatter plot
------------
We use the dataset *mtcars*, and will use
the lattice function *xyplot* to make scatter plots.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- setupxyplot-begin
:end-before: #-- setupxyplot-end
Lattice is working with formulae (see :ref:`robjects-formula`),
therefore we build one and store values in its environment.
Making a plot is then a matter of calling
the function *xyplot* with the *formula* as
as an argument.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- xyplot1-begin
:end-before: #-- xyplot1-end
.. image:: _static/graphics_lattice_xyplot_1.png
:scale: 50
The display of group information can be done
simply by using the named parameter groups.
This will indicate the different groups by
color-coding.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- xyplot2-begin
:end-before: #-- xyplot2-end
.. image:: _static/graphics_lattice_xyplot_2.png
:scale: 50
An alternative to color-coding is to have
points is different *panels*. In lattice,
this done by specifying it in the formula.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- xyplot3-begin
:end-before: #-- xyplot3-end
.. image:: _static/graphics_lattice_xyplot_3.png
:scale: 50
Box plot
--------
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- bwplot1-begin
:end-before: #-- bwplot1-end
.. image:: _static/graphics_lattice_bwplot_1.png
:scale: 50
Other plots
-----------
The R package lattice contains a number of other plots, which unfortunately cannot all be detailled here.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- wireframe1-begin
:end-before: #-- wireframe1-end
.. image:: _static/graphics_lattice_wireframe_1.png
:scale: 50
Splitting the information into different panels can also be specified in the formula. Here we show an artifial
example where the split is made according to the values plotted on the Z axis.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- wireframe2-begin
:end-before: #-- wireframe2-end
.. image:: _static/graphics_lattice_wireframe_2.png
:scale: 50
Package *ggplot2*
=================
Introduction
------------
The R package *ggplot2* implements the Grammar of Graphics.
While more documentation on the package and its usage with R can be found
on the `ggplot2 website`_, this section will introduce the basic concepts required
to build plots. Obviously, the *R* package *ggplot2* is expected to be installed in the *R*
used from *rpy2*.
.. _ggplot2 website: http://had.co.nz/ggplot2/
The package is using the *grid* lower-level plotting infrastructure, that can be accessed
through the module :mod:`rpy2.robjects.lib.grid`. Whenever separate plots on the same device,
or arbitrary graphical elements overlaid, or significant plot customization, or editing,
are needed some knowledge of *grid* will be required.
Here again, having data in a :class:`DataFrame` is expected
(see :ref:`robjects-dataframes` for more information on such objects).
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- setupggplot2-begin
:end-before: #-- setupggplot2-end
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- dataset-begin
:end-before: #-- dataset-end
Plot
----
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2mtcars-begin
:end-before: #-- ggplot2mtcars-end
.. image:: _static/graphics_ggplot2mtcars.png
:scale: 50
Aesthethics mapping
^^^^^^^^^^^^^^^^^^^
An important concept for the grammar of graphics is the
mapping of variables, or columns in a data frame, to
graphical representations.
Like it was shown for *lattice*, a third variable can be represented
on the same plot using color encoding, and this is now done by
specifying that as a mapping (the parameter *col* when calling
the constructor for the :class:`AesString`).
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2mtcarscolcyl-begin
:end-before: #-- ggplot2mtcarscolcyl-end
.. image:: _static/graphics_ggplot2mtcarscolcyl.png
:scale: 50
The size of the graphical symbols plotted (here circular dots) can
also be mapped to a variable:
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2aescolsize-begin
:end-before: #-- ggplot2aescolsize-end
.. image:: _static/graphics_ggplot2aescolsize.png
:scale: 50
Geometry
^^^^^^^^
The *geometry* is how the data are represented. So far we used a scatter
plot of points, but there are other ways to represent our data.
Looking at the distribution of univariate data can be achieved with
an histogram:
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2geomhistogram-begin
:end-before: #-- ggplot2geomhistogram-end
.. image:: _static/graphics_ggplot2geomhistogram.png
:scale: 50
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2geomhistogramfillcyl-begin
:end-before: #-- ggplot2geomhistogramfillcyl-end
.. image:: _static/graphics_ggplot2geomhistogramfillcyl.png
:scale: 50
Barplot-based representations of several densities on the same
figure can often be lacking clarity and line-based representation,
either :func:`geom_freqpoly` (representation of the frequency as broken
lines) or :func:`geom_density` (plot a density estimate),
can be in better.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2geomfreqpolyfillcyl-begin
:end-before: #-- ggplot2geomfreqpolyfillcyl-end
.. image:: _static/graphics_ggplot2geomfreqpolyfillcyl.png
:scale: 50
Whenever a large number of points are present, it can become interesting
to represent the density of "dots" on the scatterplot.
With 2D bins:
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2geombin2d-begin
:end-before: #-- ggplot2geombin2d-end
With a kernel density estimate:
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2geomdensity2d-begin
:end-before: #-- ggplot2geomdensity2d-end
With hexagonal bins:
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2geomhexbin-begin
:end-before: #-- ggplot2geomhexbin-end
.. image:: _static/graphics_ggplot2geombin2d.png
:scale: 50
Box plot:
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2geomboxplot-begin
:end-before: #-- ggplot2geomboxplot-end
.. image:: _static/graphics_ggplot2geomboxplot.png
:scale: 50
Boxplots can be used to represent a *summary* of the data with an emphasis
on location and spread.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2aescolboxplot-begin
:end-before: #-- ggplot2aescolboxplot-end
.. image:: _static/graphics_ggplot2aescolboxplot.png
:scale: 50
Models fitted to the data are also easy to add to a plot:
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2addsmooth-begin
:end-before: #-- ggplot2addsmooth-end
.. image:: _static/graphics_ggplot2addsmooth.png
:scale: 50
The *method* can be one of {*glm*, *gam*, *loess*, *rlm*},
and formula can be specified to declared the fitting (see example below).
.. image:: _static/graphics_ggplot2addsmoothmethods.png
:scale: 50
The constructor for :class:`GeomSmooth` also accepts a parameter
*groupr* that indicates if the fit should be done according to groups.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2smoothbycyl-begin
:end-before: #-- ggplot2smoothbycyl-end
.. image:: _static/graphics_ggplot2smoothbycyl.png
:scale: 50
Encoding the information in the column *cyl* is again
only a matter of specifying it in the :class:`AesString` mapping.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2smoothbycylwithcolours-begin
:end-before: #-- ggplot2smoothbycylwithcolours-end
.. image:: _static/graphics_ggplot2_smoothbycylwithcolours.png
:scale: 50
As can already be observed in the examples with :class:`GeomSmooth`,
several *geometry* objects can be added on the top of each other
in order to create the final plot. For example, a marginal *rug*
can be added to the axis of a regular scatterplot:
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2geompointandrug-begin
:end-before: #-- ggplot2geompointandrug-end
.. image:: _static/graphics_ggplot2geompointandrug.png
:scale: 50
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2geompointdensity2d-begin
:end-before: #-- ggplot2geompointdensity2d-end
.. image:: _static/graphics_ggplot2geompointdensity2d.png
:scale: 50
Polygons can be used for maps, as shown in the relatively artificial
example below:
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2mappolygon-begin
:end-before: #-- ggplot2mappolygon-end
.. image:: _static/graphics_ggplot2map_polygon.png
:scale: 50
Axes
^^^^
Axes can be transformed and configured in various ways.
A common transformation is the log-transform of the coordinates.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2mtcarscoordtrans-begin
:end-before: #-- ggplot2mtcarscoordtrans-end
.. image:: _static/graphics_ggplot2mtcars_coordtrans.png
:scale: 50
.. note::
The red square is an example of adding graphical
elements to a ggplot2 figure.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2mtcarscoordtransannot-begin
:end-before: #-- ggplot2mtcarscoordtransannot-end
Facets
^^^^^^
Splitting the data into panels, in a similar fashion to what we did
with *lattice*, is now a matter of adding *facets*.
A central concept to *ggplot2* is that plot are made of added
graphical elements, and adding specifications such as "I want my data
to be split in panel" is then a matter of adding that information
to an existing plot.
For example, splitting the plots on the data in column *cyl*
is still simply done by adding a :class:`FacetGrid`.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2smoothbycylfacetcyl-begin
:end-before: #-- ggplot2smoothbycylfacetcyl-end
.. image:: _static/graphics_ggplot2smoothbycylfacetcyl.png
:scale: 50
The way data are represented (the *geometry* in the terminology
used the grammar of graphics) are still specified the usual way.
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2histogramfacetcyl-begin
:end-before: #-- ggplot2histogramfacetcyl-end
.. image:: _static/graphics_ggplot2histogramfacetcyl.png
:scale: 50
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- qplot4-begin
:end-before: #-- qplot4-end
.. image:: _static/graphics_ggplot2_qplot_4.png
:scale: 50
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- qplot3addline-begin
:end-before: #-- qplot3addline-end
.. image:: _static/graphics_ggplot2_qplot_5.png
:scale: 50
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2smoothblue-begin
:end-before: #-- ggplot2smoothblue-end
.. image:: _static/graphics_ggplot2smoothblue.png
:scale: 50
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- ggplot2smoothblue-begin
:end-before: #-- ggplot2smoothblue-end
.. image:: _static/graphics_ggplot2smoothblue.png
:scale: 50
Extensions and new features
---------------------------
The R package `ggplot2` is under active development, and new
methods (geometry, summary statistics, theme customizations)
are added regularly. In addition to this there exists a
dynamic ecosystem of R packages proposing extensions, and
a user may need R code for ggplot2 not included in our
module. The following steps should make writing the Python
wrapper code a very minimal effort in many cases:
1. Identify the matching type of extension in our class diagram for
:mod:`rpy2.robjects.lib.ggplot2` (for example, is this an new
"geometry", statistics, coordinates, theme ?). For example the ggplot2
function `stat_quantile` is a statistics and the best matching class is
:class:`rpy2.robjects.lib.ggplot2.Stat`. The more general ancestor
class :class:`rpy2.robjects.lib.ggplot2.GBaseObject` could also be used.
2. Implement a child class that assign to the class attribute
:attr:`_constructor` the R constructor function. For example
`stat_quantile`. The Python callable mapping the R constructor
is then the class method :meth:`new`. The complete implementation
for `stat_quantile` is then:
.. code-block:: python
from rpy2.robjects.packages import importr
ggplot2_rpack = importr('ggplot2')
class StatQuantile(Stat):
""" Continuous quantiles """
_constructor = ggplot2_rpack.stat_quantile
stat_quantile = StatQuantile.new
The callable :func:`stat_quantile` can now be used like any
other ggplot2 statistics in :mod:`rpy2.robjects.lib.ggplot2`
(and the object it returns can be added to compose a final
ggplot figure).
Class diagram
-------------
.. inheritance-diagram:: rpy2.robjects.lib.ggplot2.GBaseObject
rpy2.robjects.lib.ggplot2.Coord
rpy2.robjects.lib.ggplot2.Element
rpy2.robjects.lib.ggplot2.Facet
rpy2.robjects.lib.ggplot2.Geom
rpy2.robjects.lib.ggplot2.GGPlot
rpy2.robjects.lib.ggplot2.Scale
rpy2.robjects.lib.ggplot2.Stat
rpy2.robjects.lib.ggplot2.Theme
:parts: 1
Package *grid*
==============
The *grid* package is the underlying plotting environment for *lattice*
and *ggplot2* figures. In few words, it consists in pushing and poping systems
of coordinates (*viewports*) into a stack, and plotting graphical elements into them.
The system can be thought of as a scene graph, with each *viewport* a node in
the graph.
>>> from rpy2.robjects.lib import grid
Getting a new page is achieved by calling the function :func:`grid.newpage`.
Calling :func:`layout` will create a layout, e.g. create a layout with one row
and 3 columns:
>>> lt = grid.layout(1, 3)
That layout can be used to construct a viewport:
>>> vp = grid.viewport(layout = lt)
The created viewport corresponds to a graphical entity.
Pushing into the current viewport, can be done by using the class method
:meth:`grid.Viewport.push`:
>>> vp.push()
Example:
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- grid-begin
:end-before: #-- grid-end
.. image:: _static/graphics_grid.png
:scale: 50
Custom ggplot2 layout with grid
-------------------------------
.. literalinclude:: _static/demos/graphics.py
:start-after: #-- gridwithggplot2-begin
:end-before: #-- gridwithggplot2-end
.. image:: _static/graphics_ggplot2withgrid.png
:scale: 50
Classes
-------
.. autoclass:: rpy2.robjects.lib.grid.Viewport(o)
:show-inheritance:
:members:
:undoc-members:
.. autoclass:: rpy2.robjects.lib.grid.Grob(o)
:show-inheritance:
:members:
:undoc-members:
.. autoclass:: rpy2.robjects.lib.grid.GTree(o)
:show-inheritance:
:members:
:undoc-members:
Class diagram
-------------
.. inheritance-diagram:: rpy2.robjects.lib.grid
:parts: 1
|