/man/translate_qplot_ggplot.Rd

http://github.com/hadley/ggplot2 · Unknown · 86 lines · 72 code · 14 blank · 0 comment · 0 complexity · 8feeb91c8e5d9077dc5a37ed1808af46 MD5 · raw file

  1. % Generated by roxygen2: do not edit by hand
  2. % Please edit documentation in R/translate-qplot-ggplot.r
  3. \name{translate_qplot_ggplot}
  4. \alias{translate_qplot_ggplot}
  5. \title{Translating between qplot and ggplot}
  6. \description{
  7. Within ggplot2, there are two basic methods to create plots, with qplot()
  8. and ggplot(). qplot() is designed primarily for interactive use: it makes
  9. a number of assumptions that speed most cases, but when designing multilayered
  10. plots with different data sources it can get in the way. This section
  11. describes what those defaults are, and how they map to the fuller ggplot()
  12. syntax.
  13. }
  14. \examples{
  15. # By default, qplot() assumes that you want a scatterplot,
  16. # i.e., you want to use geom_point()
  17. # qplot(x, y, data = data)
  18. # ggplot(data, aes(x, y)) + geom_point()
  19. # Using Aesthetics
  20. # If you map additional aesthetics, these will be added to the defaults. With
  21. # qplot() there is no way to use different aesthetic mappings (or data) in
  22. # different layers
  23. # qplot(x, y, data = data, shape = shape, colour = colour)
  24. # ggplot(data, aes(x, y, shape = shape, colour = colour)) + geom_point()
  25. #
  26. # Aesthetic parameters in qplot() always try to map the aesthetic to a
  27. # variable. If the argument is not a variable but a value, effectively a new column
  28. # is added to the original dataset with that value. To set an aesthetic to a
  29. # value and override the default appearance, you surround the value with I() in
  30. # qplot(), or pass it as a parameter to the layer.
  31. # qplot(x, y, data = data, colour = I("red"))
  32. # ggplot(data, aes(x, y)) + geom_point(colour = "red")
  33. # Changing the geom parameter changes the geom added to the plot
  34. # qplot(x, y, data = data, geom = "line")
  35. # ggplot(data, aes(x, y)) + geom_line()
  36. # Not all geoms require both x and y, e.g., geom_bar() and geom_histogram().
  37. # For these two geoms, if the y aesthetic is not supplied, both qplot and
  38. # ggplot commands default to "count" on the y-axis
  39. # ggplot(data, aes(x)) + geom_bar()
  40. # qplot(x, data = data, geom = "bar")
  41. # If a vector of multiple geom names is supplied to the geom argument, each
  42. # geom will be added in turn
  43. # qplot(x, y, data = data, geom = c("point", "smooth"))
  44. # ggplot(data, aes(x, y)) + geom_point() + geom_smooth()
  45. # Unlike the rest of ggplot2, stats and geoms are independent
  46. # qplot(x, y, data = data, stat = "bin")
  47. # ggplot(data, aes(x, y)) + geom_point(stat = "bin")
  48. #
  49. # Any layer parameters will be passed on to all layers. Most layers will ignore
  50. # parameters that they don't need
  51. # qplot(x, y, data = data, geom = c("point", "smooth"), method = "lm")
  52. # ggplot(data, aes(x, y)) + geom_point(method = "lm") + geom_smooth(method = "lm")
  53. # Scales and axes
  54. # You can control basic properties of the x and y scales with the xlim, ylim,
  55. # xlab and ylab arguments
  56. # qplot(x, y, data = data, xlim = c(1, 5), xlab = "my label")
  57. # ggplot(data, aes(x, y)) + geom_point() +
  58. # scale_x_continuous("my label", limits = c(1, 5))
  59. # qplot(x, y, data = data, xlim = c(1, 5), ylim = c(10, 20))
  60. # ggplot(data, aes(x, y)) + geom_point() +
  61. # scale_x_continuous(limits = c(1, 5)) + scale_y_continuous(limits = c(10, 20))
  62. # Like plot(), qplot() has a convenient way of log transforming the axes.
  63. # qplot(x, y, data = data, log = "xy")
  64. # ggplot(data, aes(x, y)) + geom_point() + scale_x_log10() + scale_y_log10()
  65. # There are many other possible transformations, but not all are
  66. # accessible from within qplot(), see ?scale_continuous for more
  67. # Plot options
  68. # qplot() recognises the same options as plot does, and converts them to their
  69. # ggplot2 equivalents. See ?theme for more on ggplot options
  70. # qplot(x, y, data = data, main="title", asp = 1)
  71. # ggplot(data, aes(x, y)) + geom_point() + labs(title = "title") + theme(aspect.ratio = 1)
  72. }