RstatisTik/RstatisTikPortal/RcourSe/CourseOutline/GridGraphics

The ggplot2 Package

Structure of a ggplot Object

begin with an empty object to see the structure:

   1 > po <- ggplot()
   2 > summary(po)
   3 data: [x]
   4 faceting: facet_null()

   1 > str(po)
   2 List of 9
   3   List of 9
   4   $ data       : list()
   5   ..- attr(*, "class")= chr "waiver"
   6   $ layers     : list()
   7   $ scales     :Reference class 'Scales' [package "ggplot2"] with 1 fields
   8   ..$ scales: NULL
   9   ..and 21 methods, of which 9 are possibly relevant:
  10   ..  add, clone, find, get_scales, has_scale, initialize, input, n,
  11   ..  non_position_scales
  12   $ mapping    : list()
  13   $ theme      : list()
  14   $ coordinates:List of 1
  15   ..$ limits:List of 2
  16   .. ..$ x: NULL
  17   .. ..$ y: NULL
  18   ..- attr(*, "class")= chr [1:2] "cartesian" "coord"
  19   $ facet      :List of 1
  20   ..$ shrink: logi TRUE
  21   ..- attr(*, "class")= chr [1:2] "null" "facet"
  22   $ plot_env   :<environment: R_GlobalEnv>
  23   $ labels     : list()
  24   - attr(*, "class")= chr [1:2] "gg" "ggplot"

Structure of a ggplot Object

Now we fill this structure - first the three main steps:

Feed the Object

   1 > x1 <- 1:10; y1 <- 1:10; z1 <- 10:1
   2 > l1 <- LETTERS[1:10]
   3 > a <- 10; b <- (0:-9)/10:1
   4 > ex <- data.frame(x=x1,y=y1,z=z1,l=l1,a=a,b=b)
   5 > ex
   6 x  y  z l  a          b
   7 1   1  1 10 A 10  0.0000000
   8 2   2  2  9 B 10 -0.1111111
   9 3   3  3  8 C 10 -0.2500000
  10 4   4  4  7 D 10 -0.4285714
  11 5   5  5  6 E 10 -0.6666667
  12 6   6  6  5 F 10 -1.0000000
  13 7   7  7  4 G 10 -1.5000000
  14 8   8  8  3 H 10 -2.3333333
  15 9   9  9  2 I 10 -4.0000000
  16 10 10 10  1 J 10 -9.0000000

   1 > po <- ggplot(ex,aes(x=x1,y=y1))
   2 > summary(po)
   3 > p1 <- po + geom_point()

ggp1.pdf

Layers

Layers

   1 > p2 <- po +
   2 +    geom_point() +
   3 +        geom_text(aes(label=l), hjust=1.1, vjust=-0.2)
   4 > p2

ggp2.pdf

Layers

   1 > ## the new data
   2 > ex2 <- data.frame(x1=sample(1:20),
   3 +                   y1=sample(1:10),
   4 +                   l=letters[1:20])
   5 > head(ex2,10)
   6 x1 y1 l
   7 1   3  6 a
   8 2   6  2 b
   9 3  14  1 c
  10 4  19 10 d
  11 5  12  4 e
  12 6  15  8 f
  13 7  20  5 g
  14 8  17  7 h
  15 9  13  3 i
  16 10 16  9 j

ggp3.pdf

Layers

   1 > p2 %+% ex2

Layers

   1 > pn <- p %+% ex2 ## replace data in p
   2 > pn + geom_line()

ggp4.pdf

Layers

   1 > my.text <- geom_text(aes(label=l),
   2 +                          hjust=1.1,
   3 +                          vjust=-0.2)
   4 > pn + geom_path() + my.text

ggp5.pdf

Layers

Adding extra lines:

   1 > ## one line
   2 > p + geom_abline(intercept=10,slope=-1,
   3 +                          colour=rgb(.5,.5,.9))
   4 > ## two lines
   5 > p + geom_abline(intercept=c(10,9),slope=c(-1,-2),
   6 +                              colour=rgb(.5,.5,.9))
   7 > more lines
   8 > p + geom_abline(intercept=10:1,slope=-(10:1)/10,

attachment:ggp6.png

Layers

   1 > p1 +
   2 +   geom_abline(aes(slope=b,intercept=a,colour=x1)) +
   3 +   scale_x_continuous(limits=c(0,10))

ggp7.pdf

Layers

   1 > p1 + geom_hline(yintercept=1:10)
   2 > p1 + geom_hline(yintercept=1:10) +
   3 +     geom_vline(xintercept=1:10)

ggp8.pdf

Other Common Layers

Exercises

Exercises

   1 > data$EC1 <- factor(str_sub(data$Event.Code,1,2))
   2 > head(data)
   3 Subject Sex Age_PRETEST Trial Event.Type Code   Time TTime Uncertainty
   4 1       1   f        3.11     7   Response    2 103745  2575           1
   5 2       1   f        3.11    12   Response    2 156493  2737           1
   6 3       1   f        3.11    17   Response    2 214772  6630           1
   7 4       1   f        3.11    22   Response    1 262086  5957           1
   8 5       1   f        3.11    27   Response    2 302589   272           1
   9 6       1   f        3.11    32   Response    1 352703  7197           1
  10 Duration Uncertainty.1 ReqTime ReqDur Stim.Type Pair.Index    Type Event.Code
  11 1     2599             3       0   next       hit          7 Picture   RO26.jpg
  12 2     2800             2       0   next incorrect         12 Picture   RO19.jpg
  13 3     6798             2       0   next       hit         17 Picture   RS23.jpg
  14 4     5999             2       0   next incorrect         22 Picture   OF22.jpg
  15 5      400             2       0   next       hit         27 Picture   AT08.jpg
  16 6     7398             2       0   next       hit         32 Picture   AT30.jpg
  17 testid EC1
  18 1  test2  RO
  19 2  test2  RO
  20 3  test2  RS
  21 4  test2  OF
  22 5  test2  AT
  23 6  test2  AT

Exercises

Create the five plots and save them into a file.

Exercises

   1 > ggplot(data,aes(x=EC1)) +
   2 +     geom_bar()
   3 >
   4 > ggsave("plot1.png")
   5 Saving 16 x 9.13 in image

attachment:plot1.png

Exercises

   1 > ggplot(data,aes(x=EC1,fill=Stim.Type)) +
   2 +     geom_bar()
   3 >
   4 > ggsave("plot2.png")
   5 Saving 16 x 9.13 in image

<img alt='sesssion2/plot2.png' src='-1' />

Exercises

   1 > ggplot(data,aes(x=EC1,fill=Stim.Type)) +
   2 +     geom_bar(position = "fill")
   3 >
   4 > ggsave("plot3.png")
   5 Saving 16 x 9.13 in image

<img alt='sesssion2/plot3.png' src='-1' />

Exercises

   1 > ggplot(data,aes(x=EC1,fill=Stim.Type)) +
   2 +     geom_bar(position = "fill") +
   3 +     facet_wrap(~testid)
   4 >
   5 > ggsave("plot4.png")
   6 Saving 16 x 9.13 in image

<img alt='sesssion2/plot4.png' src='-1' />

Exercises

   1 > ggplot(data,aes(x=EC1,fill=Stim.Type)) +
   2 +     geom_bar(position = "fill") +
   3 +     facet_wrap(~testid,scales = "free")
   4 >
   5 > ggsave("plot4a.png")
   6 Saving 16 x 9.13 in image

<img alt='sesssion2/plot4a.png' src='-1' />

Exercises

   1 > ggplot(data,aes(x=testid,fill=Stim.Type)) +
   2 +     geom_bar(position = "fill") +
   3 +     facet_wrap(~ Subject)
   4 > ggsave("plot5.png")
   5 Saving 16 x 9.13 in image

<img alt='sesssion2/plot5.png' src='-1' />

Introduction

The dplyr package makes each of these steps as fast and easy as possible by:

Scales

What if we want to change the colours?

Changing a Scale

   1 > ggplot(data,aes(x=EC1,fill=Stim.Type)) +
   2 +     geom_bar(position = "fill") +
   3 +     facet_wrap(~testid,scales = "free") +
   4 +     scale_fill_manual(values=c("forestgreen","firebrick"))

<img alt='sesssion2/ggp10.png' src='-1' />

Changing a Scale

There are other ways to customize a discrete colour/fill scales

Changing a Scale

   1 > ggplot(data,aes(x=EC1,fill=Stim.Type)) +
   2 +     geom_bar(position = "fill") +
   3 +     facet_wrap(~testid,scales = "free") +
   4 +         scale_fill_grey()
   5 > ggplot(data,aes(x=EC1,fill=Stim.Type)) +
   6 +     geom_bar(position = "fill") +
   7 +     facet_wrap(~testid,scales = "free") +
   8 +         scale_fill_hue(h=c(180,360))
   9 > ggplot(data,aes(x=EC1,fill=Stim.Type)) +
  10 +     geom_bar(position = "fill") +
  11 +     facet_wrap(~testid,scales = "free") +
  12 +     scale_fill_brewer(type = "div",palette = 2)

RstatisTik/RstatisTikPortal/RcourSe/CourseOutline/GridGraphics (zuletzt geändert am 2015-05-01 11:50:17 durch mandy.vogel@googlemail.com)