R Graphics Exercise (Solutions)

[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(repr.plot.width=4, repr.plot.height=3)
[3]:
data <- read_tsv('data/gene_counts_raw.txt')
Parsed with column specification:
cols(
  .default = col_double(),
  Label = col_character(),
  Media = col_character(),
  Strain = col_character()
)
See spec(...) for full column specifications.

0. The Label column has 3 pieces of information Sample, Method andd Person in a single cell. Fix this and save the tidy DataFrame as df.

[4]:
data[1:3, 1:5]
A tibble: 3 × 5
LabelMediaStraingene0gene1
<chr><chr><chr><dbl><dbl>
1_MA_JYPDH9910
1_RZ_JYPDH9900
2_MA_CYPDH9900
[5]:
n <- ncol(data)
data[1:3, (n-4):n]
A tibble: 3 × 5
gene995gene996gene997gene998gene999
<dbl><dbl><dbl><dbl><dbl>
27325637591 848
7227508345 641
312957377381006
[6]:
data %>%
separate(Label, sep='_', into=c("Sample", "Method", "Person")) %>%
select(1:5) %>%
head(3)
A tibble: 3 × 5
SampleMethodPersonMediaStrain
<chr><chr><chr><chr><chr>
1MAJYPDH99
1RZJYPDH99
2MACYPDH99
[7]:
data %>%
separate(Label, sep='_', into=c("Sample", "Method", "Person")) -> df
[8]:
df %>%
select(1:8) %>%
head
A tibble: 6 × 8
SampleMethodPersonMediaStraingene0gene1gene10
<chr><chr><chr><chr><chr><dbl><dbl><dbl>
1MA JYPDH99 1013
1RZ JYPDH99 0014
2MA CYPDH99 0010
2RZ CYPDH9910018
2TOTCYPDH99 10 0
3MA JYPDH99 00 8

1. Plot a scatter plot of gene100 against gene 1001. Color points by the method used. Save the image as a PNG file ‘fig1.png’ in the ‘figs’ folder.

[9]:
ggplot(df, aes(x=gene100, y=gene1001, color=Method)) +
geom_point()
ggsave('figs/fig1.png')
Saving 6.67 x 6.67 in image
../_images/cliburn_R09_Graphics_Exercise_Solutions_11_1.png

2. Make a boxplot plot of gene100 counts by method.

[11]:
ggplot(df, aes(x=Method, y=gene100)) +
geom_boxplot()
ggsave('figs/fig2.png')
Saving 6.67 x 6.67 in image
../_images/cliburn_R09_Graphics_Exercise_Solutions_13_1.png

3. Make a jitter plot of gene100 counts by Media and color the points by method. Set the jitter width to be 0.2.

[13]:
ggplot(df, aes(x=Media, y=gene100, color=Method)) +
geom_jitter(width=0.2)
ggsave('figs/fig3.png')
Saving 6.67 x 6.67 in image
../_images/cliburn_R09_Graphics_Exercise_Solutions_15_1.png

4. Make a grid of histograms of counts for gene100, with rows showing the person and columns showing the method used.

[16]:
ggplot(df, aes(x=gene100)) +
facet_grid(Person ~ Method) +
geom_histogram(binwidth=50)
ggsave('figs/fig4.png')
Saving 6.67 x 6.67 in image
../_images/cliburn_R09_Graphics_Exercise_Solutions_17_1.png

5. Make a row of boxplots of log counts of the top 5 genes where each column shows a different method.

Warning: This involves quite a bit of data processing.

[17]:
genes.top5 <- df %>%
select(starts_with('gene')) %>%
summarize_all(mean) %>%
gather() %>%
arrange(desc(value)) %>%
head(5)
[18]:
genes.top5
A tibble: 5 × 2
keyvalue
<chr><dbl>
gene1139884531.49
gene1136484816.47
gene7418132744.71
gene363 62325.08
gene7387 45986.86
[19]:
genes.top5$key
  1. 'gene1139'
  2. 'gene1136'
  3. 'gene7418'
  4. 'gene363'
  5. 'gene7387'
[22]:
df %>%
select(c('Method', genes.top5$key)) %>%
gather(gene, count, -Method) %>%
mutate(logcount = log(count)) %>%
ggplot(aes(x=gene, y=logcount)) +
geom_boxplot() +
facet_wrap(~ Method) +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggsave('figs/fig5.png')
Saving 6.67 x 6.67 in image
../_images/cliburn_R09_Graphics_Exercise_Solutions_22_1.png
[ ]: