ggplot: Create a new ggplot

Description Usage Arguments Details See Also Examples

View source: R/plot.r

Description

This function creates the basic ggplot object which you can then furnish with graphical objects. Here you will set up the default data frame, default aesthetics and the formula that will determine how the panels are broken apart. See cast for more details on specifying the facetting formula and margin arguments. Note that ggplot creates a plot object without a "plot": you need to grobs (points, lines, bars, etc.) to create something that you can see.

Usage

1
2
3
4
5
ggplot(data, ...)

## Default S3 method:
ggplot(data = NULL, aesthetics = list(),
  formula = . ~ ., margins = FALSE, ...)

Arguments

data

default data frame

...

Not used

aesthetics

default list of aesthetic mappings (these can be colour, size, shape, line type – see individual grob functions for more details)

formula

formula describing row and column layout, see cast for more details

margins

a vector of names giving which margins to display, can include grand_row and grand_col or us TRUE to display all margins

Details

Steps to create a plot:

  1. Create a new plot. (p <- ggplot(mtcars, aesthetics=list(y=hp, x=mpg)))

  2. Set scales (if necessary)

  3. Add grobs to the plot (ggpoint(p))

Simple grobs:

Complex grobs:

Look at the documentation of these objects to see many examples of ggplot in action.

You will also want to add scales to the basic plot to give finer control over how the data values are mapped to aethetics attributes of the grobs. For scales that control position of the points see:

For other scales, see:

ggplot is different from base and lattice graphics in how you build up the plot. With ggplot you build up the plot object (rather than the plot on the screen as in base graphics, or all at once as in lattice graphics.)

Each of the grob and scale functions adds the grob to the plot and returns the modified plot object. This lets you quickly experiment with different versions of the plot, using different grobs or scales. You can see how this works in the examples

You can also use summary to give a quick description of a plot.

If you want to change the background colour, how the panel strips are displayed, or any other default graphical option, see ggopt.

See Also

http://had.co.nz/ggplot, stamp, cast, ggopt

Examples

 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
data("tips", package = "reshape")

p <- ggplot(tips)
summary(p)
ggpoint(p, aesthetic = list(y = tip, x = total_bill))
p <- ggplot(tips, aesthetic = list(y = tip, x = total_bill))
p$title <- "Tips"
summary(p)
ggpoint(p)
ggpoint(p, colour = "darkgreen", size = 3)
ggpoint(p, list(colour = sex))
ggpoint(ggplot(tips, formula = . ~ sex, aesthetics = list(y = tip, x = total_bill)))

# Create complex plots from simple components
p <- ggplot(tips, formula = smoker ~ sex, aesthetics = list(y = tip, x = total_bill))
p %>% ggpoint()

p %>%
  ggpoint() %>%
  ggsmooth()

p %>%
  ggpoint() %>%
  ggsmooth(method = lm, formula = y~x)

p %>%
  ggpoint() %>%
 ggabline(slope = c(0.1,0.15,0.2))

(p2 <- ggabline(ggpoint(p, aes = list(colour = tip/total_bill)), slope = c(0.1, 0.15, 0.2)))
summary(p2)
scgradient(p2)
scgradient(p2, midpoint = 0.15, high = "green", mid = "yellow")

p <- ggplot(tips, formula = sex ~ smoker, aesthetics = list(x = tip / total_bill), margins = TRUE)
gghistogram(p)
gghistogram(p, scale = "density", breaks = seq(0, 1, length = 20))
ggdensity(gghistogram(p))

p <- ggplot(tips, formula = . ~ smoker, aesthetics = list(x = sex, y = tip))
ggboxplot(p)
ggjitter(ggboxplot(p))

hadley/ggplot1 documentation built on Aug. 19, 2019, 2:42 p.m.