PageRenderTime 26ms CodeModel.GetById 24ms app.highlight 1ms RepoModel.GetById 0ms app.codeStats 0ms

/man/translate_qplot_gpl.Rd

http://github.com/hadley/ggplot2
Unknown | 58 lines | 48 code | 10 blank | 0 comment | 0 complexity | e6f02c978528db770e703bddb2fbde94 MD5 | raw file
Possible License(s): GPL-2.0
 1\name{translate_qplot_gpl}
 2\alias{translate_qplot_gpl}
 3\title{Translating between qplot and Graphics Production Library (GPL)}
 4\description{
 5  The Grammar of Graphics uses two specifications. A
 6  concise format is used to caption figures, and a more
 7  detailed xml format stored on disk.
 8}
 9\examples{
10# The following example of the concise format is adapted from Figure 1.5,
11# page 13, of Leland Wilkinson's "The Grammar of Graphics."
12# Springer, 2nd edition, 2005.
13
14# DATA: source("demographics")
15# DATA: longitude, latitude = map(source("World"))
16# TRANS: bd = max(birth - death, 0)
17# COORD: project.mercator()
18# ELEMENT: point(position(lon * lat), size(bd), color(color.red))
19# ELEMENT: polygon(position(longitude * latitude))
20
21# This is relatively simple to adapt to the syntax of ggplot2:
22
23# ggplot() is used to specify the default data and default aesthetic mappings.
24# Data is provided as standard R data.frames existing in the global environment;
25# it does not need to be explicitly loaded. We also use a slightly
26# different world dataset, with columns lat and long. This lets us use the
27# same aesthetic mappings for both datasets. Layers can override the default
28# data and aesthetic mappings provided by the plot.
29
30# We replace TRANS with an explicit transformation by R code.
31
32# ELEMENTs are replaced with layers, which explicitly specify the data
33# source. Each geom has a default statistic which is used to transform the
34# data prior to plotting. For the geoms in this example, the default statistic
35# is the identity function. Fixed aesthetics (the colour red in this example)
36# are supplied as additional arguments to the layer, rather than as special
37# constants.
38
39# The SCALE component has been omitted from this example (so that the
40# defaults are used). In both the ggplot2 and GoG examples, scales are
41# defined by default. In ggplot you can override the defaults by adding a
42# scale object, e.g., scale colour or scale size.
43
44# COORD uses a slightly different format. In general, most of the components
45# specifications in ggplot are slightly different to those in GoG, in order to
46# be more familiar to R users.
47
48# Each component is added together with + to create the final plot.
49
50# Resulting ggplot2 code:
51# demographics <- transform(demographics, bd = pmax(birth - death, 0))
52# p <- ggplot(demographic, aes(lon, lat))
53# p <- p + geom_polyogon(data = world)
54# p <- p + geom_point(aes(size = bd), colour = "red")
55# p <- p + coord_map(projection = "mercator")
56# print(p)
57}
58