/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

  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. # DATA: source("demographics")
  14. # DATA: longitude, latitude = map(source("World"))
  15. # TRANS: bd = max(birth - death, 0)
  16. # COORD: project.mercator()
  17. # ELEMENT: point(position(lon * lat), size(bd), color(color.red))
  18. # ELEMENT: polygon(position(longitude * latitude))
  19. # This is relatively simple to adapt to the syntax of ggplot2:
  20. # ggplot() is used to specify the default data and default aesthetic mappings.
  21. # Data is provided as standard R data.frames existing in the global environment;
  22. # it does not need to be explicitly loaded. We also use a slightly
  23. # different world dataset, with columns lat and long. This lets us use the
  24. # same aesthetic mappings for both datasets. Layers can override the default
  25. # data and aesthetic mappings provided by the plot.
  26. # We replace TRANS with an explicit transformation by R code.
  27. # ELEMENTs are replaced with layers, which explicitly specify the data
  28. # source. Each geom has a default statistic which is used to transform the
  29. # data prior to plotting. For the geoms in this example, the default statistic
  30. # is the identity function. Fixed aesthetics (the colour red in this example)
  31. # are supplied as additional arguments to the layer, rather than as special
  32. # constants.
  33. # The SCALE component has been omitted from this example (so that the
  34. # defaults are used). In both the ggplot2 and GoG examples, scales are
  35. # defined by default. In ggplot you can override the defaults by adding a
  36. # scale object, e.g., scale colour or scale size.
  37. # COORD uses a slightly different format. In general, most of the components
  38. # specifications in ggplot are slightly different to those in GoG, in order to
  39. # be more familiar to R users.
  40. # Each component is added together with + to create the final plot.
  41. # Resulting ggplot2 code:
  42. # demographics <- transform(demographics, bd = pmax(birth - death, 0))
  43. # p <- ggplot(demographic, aes(lon, lat))
  44. # p <- p + geom_polyogon(data = world)
  45. # p <- p + geom_point(aes(size = bd), colour = "red")
  46. # p <- p + coord_map(projection = "mercator")
  47. # print(p)
  48. }