R Graphics

[1]:
library(tidyverse)
Registered S3 methods overwritten by 'ggplot2':
  method         from
  [.quosures     rlang
  c.quosures     rlang
  print.quosures rlang
── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ──
 ggplot2 3.1.1      purrr   0.3.2
 tibble  2.1.2      dplyr   0.8.1
 tidyr   0.8.3      stringr 1.4.0
 readr   1.3.1      forcats 0.4.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
 dplyr::filter() masks stats::filter()
 dplyr::lag()    masks stats::lag()
[2]:
options.orig <- options(repr.plot.width=6, repr.plot.height=4)

Simulate some data for plotting

[3]:
n <- 100
x <- sort(runif(n))
y <- x^2 + x + 3 + 0.2*rnorm(n)
z <- x^2
g <- as.factor(sample(1:4, n, replace=T))
df <- tibble(x=x, y=y, z=z, g=g)

Grammar of Graphics

ggplot2 uses a Grammar of Graphics convention to define plots. See The fundamentals of ``gggplot2` explained <https://www.aridhia.com/technical-tutorials/the-fundamentals-of-ggplot-explained/>`__ for a simple epxlanation.

The main concents are

  • data source

  • map data elements to visual characteristics (aes)

  • type of visual elements in plot (geom)

  • layers (+)

  • facet to automatically show “group by” plots

  • scale to controls how the mapping is made

  • statistics to add summary data to the plot

  • themes to define the look and feel of the plot

[4]:
g0 <- ggplot(df, aes(x=x, y=y))

g1 <- g0 +
geom_point(aes(col=z)) +
geom_smooth(method='lm',
            formula=y ~ poly(x, 2, raw=TRUE))

g2 <- g1 +
labs(title="ggplot2",
     subtitle="Graphics example",
     caption="HTS 2018",
     x="x-coordinate",
     y="y-coordiante")

g3 <- g2 +
guides(col=FALSE)
[5]:
g0
../_images/cliburn_R08_Graphics_ggplot2_8_0.png
[6]:
g1
../_images/cliburn_R08_Graphics_ggplot2_9_0.png
[7]:
g2
../_images/cliburn_R08_Graphics_ggplot2_10_0.png
[8]:
g3
../_images/cliburn_R08_Graphics_ggplot2_11_0.png

Interpration

  • What is the data source?

  • What mappings are applid to the plot as a whole? To the points?

  • What labels are specified and where do they appear?

  • Wthat geometric objects are used for plotitng?

  • What statistics are added to the plot?

  • How was layering used to build the plot incrementally?

  • How were partially completed plot stages saved?

Themes

[9]:
g3 + theme_minimal()
../_images/cliburn_R08_Graphics_ggplot2_14_0.png
[10]:
g3 + theme_linedraw()
../_images/cliburn_R08_Graphics_ggplot2_15_0.png
[11]:
g3 + theme_dark()
../_images/cliburn_R08_Graphics_ggplot2_16_0.png

Facets

[12]:
g3 + facet_wrap(facets='g', nrow = 2)
../_images/cliburn_R08_Graphics_ggplot2_18_0.png
[13]:
g3 + facet_grid(~ g)
../_images/cliburn_R08_Graphics_ggplot2_19_0.png
[14]:
g3 + facet_grid(g ~ .)
../_images/cliburn_R08_Graphics_ggplot2_20_0.png

Scales

[15]:
g4 <- ggplot(df, aes(x=x, y=y, col=as.factor(g)) ) +
geom_point() +
geom_smooth(method='lm',
            formula=y ~ poly(x, 2, raw=TRUE)) +
labs(title="ggplot2",
     subtitle="Graphics example",
     caption="HTS 2018",
     x="x-coordinate",
     y="y-coordiante") +
facet_grid(~ g) +
guides(col=FALSE)

Colors

[16]:
g4
../_images/cliburn_R08_Graphics_ggplot2_24_0.png
[17]:
g4 + scale_color_brewer()
../_images/cliburn_R08_Graphics_ggplot2_25_0.png
[18]:
g4 + scale_color_brewer(type = 'seq', palette = 'Reds')
../_images/cliburn_R08_Graphics_ggplot2_26_0.png
[19]:
g4 + scale_color_brewer(type = 'qual', palette = 2)
../_images/cliburn_R08_Graphics_ggplot2_27_0.png

Palettes available from brewer

The 3 plots show color maps that are

  1. Sequential

  2. Qualitiative

  3. Divergent

[20]:
library(RColorBrewer)
[21]:
options.orig <- options(repr.plot.width=6, repr.plot.height=12)
display.brewer.all()
../_images/cliburn_R08_Graphics_ggplot2_30_0.png
[22]:
options.orig <- options(repr.plot.width=6, repr.plot.height=4)

Scales can be used for changing the mapping of color, fill, hue, size, coordinates

Here we scale the y-coordinate to show log values.

[23]:
g4 + scale_y_log10()
../_images/cliburn_R08_Graphics_ggplot2_33_0.png

Geoms

[24]:
g0 + geom_point()
../_images/cliburn_R08_Graphics_ggplot2_35_0.png
[25]:
g0 + geom_line()
../_images/cliburn_R08_Graphics_ggplot2_36_0.png
[26]:
ggplot(df, aes(x=z, fill=g)) +
geom_density(aes(alpha=0.1)) +
guides(alpha=F)
../_images/cliburn_R08_Graphics_ggplot2_37_0.png
[27]:
g0 +
geom_density_2d() +
geom_rug()
../_images/cliburn_R08_Graphics_ggplot2_38_0.png
[28]:
head(df)
A tibble: 6 × 4
xyzg
<dbl><dbl><dbl><fct>
0.0070045942.9301214.906434e-051
0.0271067263.2668027.347746e-041
0.0278226242.8584237.740984e-044
0.0498017533.0776672.480215e-031
0.0555866993.2661323.089881e-032
0.0667443963.1275794.454814e-031

geom_bar shows counts by default

[29]:
ggplot(df, aes(x=g)) +
geom_bar() +
labs(y="Sum of y")
../_images/cliburn_R08_Graphics_ggplot2_41_0.png

Specify weight parameter to sum over a column by group

[30]:
ggplot(df, aes(x=g, weight=y) )+
geom_bar() +
labs(y="Sum of y")
../_images/cliburn_R08_Graphics_ggplot2_43_0.png

If you already have counts, specify stat=identity

[31]:
df_ <- tibble(x=c('A', 'B', 'C'), y=c(10, 14, 18))
[32]:
head(df_)
A tibble: 3 × 2
xy
<chr><dbl>
A10
B14
C18
[33]:
ggplot(df_, aes(x=x, y=y, fill=x)) +
geom_bar(stat='identity') +
guides(fill=F)
../_images/cliburn_R08_Graphics_ggplot2_47_0.png
[34]:
ggplot(df, aes(x=g, y=z, fill=g)) +
geom_boxplot() +
geom_jitter(width=0.2)  +
guides(fill=FALSE)
../_images/cliburn_R08_Graphics_ggplot2_48_0.png
[35]:
ggplot(df, aes(x=g, y=z, fill=g)) +
geom_boxplot() +
geom_jitter(width=0.2) +
coord_flip() +
guides(fill=FALSE)
../_images/cliburn_R08_Graphics_ggplot2_49_0.png

Saving plots

[36]:
ggsave('figs/box.png')
Saving 6.67 x 6.67 in image
[37]:
ggsave('figs/g3.pdf', g3)
Saving 6.67 x 6.67 in image

Retrieve saved files