COD_20241204_MGK_IBS7048_week14_data_visualization_advanced

Minsik Kim

2024-12-04

Roading ggplot

from previous lecture.. we generated a phyloseq object. it can be saved to hard disk using the below command.

library(ggplot2)
library(tidyverse)

Advanced ggplot2

head(mpg)
## # A tibble: 6 × 11
##   manufacturer model displ  year   cyl trans      drv     cty   hwy fl    class 
##   <chr>        <chr> <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr> 
## 1 audi         a4      1.8  1999     4 auto(l5)   f        18    29 p     compa…
## 2 audi         a4      1.8  1999     4 manual(m5) f        21    29 p     compa…
## 3 audi         a4      2    2008     4 manual(m6) f        20    31 p     compa…
## 4 audi         a4      2    2008     4 auto(av)   f        21    30 p     compa…
## 5 audi         a4      2.8  1999     6 auto(l5)   f        16    26 p     compa…
## 6 audi         a4      2.8  1999     6 manual(m5) f        18    26 p     compa…

Generating plot

plot(mpg$cty, mpg$hwy)

mpg %>% 
        ggplot(aes(x = cty, y = hwy))

mpg %>% 
        ggplot(aes(x = cty, y = hwy)) +
        geom_point()

mpg %>% 
        ggplot(aes(x = cty, y = hwy)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)")

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer, shape = as.factor(cyl))) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)")

+ theme()

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.position = "left")

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(axis.title.x = element_text(family = "serif", size = 15))

theme() and element_markdown()

install.packages("ggtext")
## 
## The downloaded binary packages are in
##  /var/folders/zh/85dfy3gx6rd659vpk8_r_ppr0000gn/T//Rtmplz2otU/downloaded_packages
library(ggtext)

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(axis.title.y = element_markdown(family = "serif", size = 15))

Basic markdown syntax is at https://www.markdownguide.org/basic-syntax/

Italic

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("*Highway mileage (MPG)*") +
        theme(axis.title.y = element_markdown(family = "serif", size = 15))

Bold

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("**Highway mileage (MPG)**") +
        theme(axis.title.y = element_markdown(family = "serif", size = 15))

Italic

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("*Highway mileage (MPG)*") +
        theme(axis.title.y = element_markdown(family = "serif", size = 20))

superscript and subscript

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("High<sup>way</sup>
             mileage 
             <sub>(MPG)</sub>") +
        theme(axis.title.y = element_markdown(family = "serif", size = 25))

Comprehensive

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("*Hi***gh**<sub>way</sub> mile<sub>age</sub> ***(MPG)***") +
        theme(axis.title.y = element_markdown(family = "serif", size = 30))

+ guides()

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        guides(col=guide_legend(nrow=3))

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        guides(col=guide_legend(ncol=3, title.position = "bottom"))

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        guides(col=guide_legend(ncol=3, title.position = "bottom",
                                override.aes = list(size = 4)))

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        guides(col=guide_legend(ncol=3, title.position = "top", 
                                title.hjust = 0.5))

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        #theme() +
        guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>"))

theme_…

classic

The order matters (theme and theme_classic)

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>")) +
        theme_classic() 

bw

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_bw() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>"))

### dark

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_dark() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>"))

### minimal

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_minimal() + 
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        #theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>"))

geom_smooth

lm, one line

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>"))

### lm multiple line

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>")) +
        geom_smooth(method = "lm", aes(x = cty, y = hwy, col = manufacturer))

se, Confidence intervals

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
                guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>")) +
        geom_smooth(method = "lm", aes(x = cty, y = hwy, col = manufacturer),
                    se = F)

gam, curvy line

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
                guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>")) +
        geom_smooth(method = "gam", aes(x = cty, y = hwy, col = NA))

Colors

manual colors

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
                guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>")) +
        scale_color_manual(values = c("#e5f5f9", "#99d8c9", "#2ca25f", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey"))

Changing labels

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>")) +
        scale_color_manual(values = c("red", "blue", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey"), 
                           labels = c("Audi", "Chevy", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey", "grey"))

Automated colors1

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>")) +
        scale_color_viridis_d()

Automated colors2

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_brewer()

Automated colors3

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_brewer(type = "qual", palette = 6)

Automated colors4

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>")) +
        scale_color_brewer(type = "div", palette = 2)

Note https://colorbrewer2.org/#type=sequential&scheme=BuGn&n=3 for more colors, that they are distinguishable.

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title.theme = element_markdown(),
                                ncol=3,
                                title.position = "top",
                                title.hjust = 0.5,
                                keywidth = 0.2,
                                keyheight = 0.2,
                                title = "*Manufacturer*<sup>company</sup>")) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999'))

  • note for MetBrewer
# install.packages("MetBrewer")
# # or
# install.packages("devtools")
# devtools::install_github("BlakeRMills/MetBrewer")

facetting

facet_wrap

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        facet_wrap(~manufacturer)

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        facet_wrap(~cyl, nrow = 1)

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        facet_wrap(~cyl, nrow = 1, scales = "free_y")

facet_grid

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        facet_grid(year~cyl)

## Title

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        facet_grid(year~cyl) +
        ggtitle("Correlation between city mpg and high mpg")

labs

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        facet_grid(year~cyl) +
        labs(tag = "A")

ggarrange

#install.packages("ggpubr")
library(ggpubr)

ggarrange(
        mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        labs(tag = "A")
        
        ,
        
        
        
        mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown(),
              axis.text.x = element_blank()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        facet_grid(year~cyl) +
        labs(tag = "B")
        
        
)

### common.legend

ggarrange(
        mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        labs(tag = "A")
        
        ,
        
        
        
        mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown(),
              axis.text.x = element_blank()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        facet_grid(year~cyl) +
        labs(tag = "B")
        
        ,
        
        common.legend = T
        
)

widths = c()

plot1 <-
        mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        labs(tag = "A")
        

plot2 <- mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown(),
              axis.text.x = element_blank()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        facet_grid(year~cyl) +
        labs(tag = "B")





ggarrange(plot1, plot2, common.legend = T, widths = c(1,3))

Saving plot file

saving plot as an object

plot_ab <- ggarrange(
        mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        labs(tag = "A")
        
        ,
        
        
        
        mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown(),
              axis.text.x = element_blank()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        facet_grid(year~cyl) +
        labs(tag = "B")
        
        ,
        
        common.legend = T
        
        
        
        ,
        
        widths = c(1,3)
        
                
        
)


plot_ab

Final assignment

plot_abc <- ggarrange(#QUESTION 1
        mpg %>%
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() + #QUESTION 2
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) + ##QUESTION 3
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) + #QUESTION 4
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        labs(tag = "A")
        
        ,
        
        
        
        mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown(),
              axis.text.x = element_blank()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        facet_grid(year~cyl) + #QUESTION 5
        labs(tag = "B")
        
        ,
        
        
        
        mpg %>% 
        ggplot(aes(x = as.factor(cyl), y = hwy, col = manufacturer)) +
        theme_classic() +
        geom_boxplot() +
        xlab("Number of cylinders") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9',
                             '#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999')) +
        #facet_grid(year~cyl) + #QUESTION 5
        labs(tag = "C")
        
        , 
        
        
        ncol = 3,
        
        
        common.legend = T #QUESTION 6
        
        
        
        ,
        
        widths = c(1,1,2)#QUESTION 7
        
                
        
)


plot_ab

pdf

setwd("Inha/5_Lectures/2024/Advanced metagenomics/scripts/IBS7048_Advanced_metagenomics/")
#dir.create("Figures")

pdf(file = "Figures/Figure1.pdf",   # The directory you want to save the file in
    width = 10, # The width of the plot in inches
    height = 10 # The height of the plot in inches

) #fixing multiple page issue

plot_ab


dev.off()
## quartz_off_screen 
##                 2

png

setwd("Inha/5_Lectures/2024/Advanced metagenomics/scripts/IBS7048_Advanced_metagenomics/")

#dir.create("Figures")

png(file = "Figures/Figure1.png",   # The directory you want to save the file in
    width = 180, # The width of the plot in inches
    height = 170, # The height of the plot in inches
    units = "mm",
    res = 600
) #fixing multiple page issue

plot_ab


dev.off()
## quartz_off_screen 
##                 2

How to use kbl

kableExtra package

#devtools::install_github("kupietz/kableExtra")
install.packages("kabelExtra")

library(kableExtra)

kbl

mpg
## # A tibble: 234 × 11
##    manufacturer model      displ  year   cyl trans drv     cty   hwy fl    class
##    <chr>        <chr>      <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
##  1 audi         a4           1.8  1999     4 auto… f        18    29 p     comp…
##  2 audi         a4           1.8  1999     4 manu… f        21    29 p     comp…
##  3 audi         a4           2    2008     4 manu… f        20    31 p     comp…
##  4 audi         a4           2    2008     4 auto… f        21    30 p     comp…
##  5 audi         a4           2.8  1999     6 auto… f        16    26 p     comp…
##  6 audi         a4           2.8  1999     6 manu… f        18    26 p     comp…
##  7 audi         a4           3.1  2008     6 auto… f        18    27 p     comp…
##  8 audi         a4 quattro   1.8  1999     4 manu… 4        18    26 p     comp…
##  9 audi         a4 quattro   1.8  1999     4 auto… 4        16    25 p     comp…
## 10 audi         a4 quattro   2    2008     4 manu… 4        20    28 p     comp…
## # ℹ 224 more rows
mpg %>% 
        head() %>% 
        kbl(format = "html", escape = 0)
manufacturer model displ year cyl trans drv cty hwy fl class
audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
audi a4 2.0 2008 4 auto(av) f 21 30 p compact
audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
audi a4 2.8 1999 6 manual(m5) f 18 26 p compact

kable_styling

mpg %>% 
        head() %>% 
        kbl(format = "html", escape = 0) %>%
        kable_styling(full_width = 0, html_font = "sans") 
manufacturer model displ year cyl trans drv cty hwy fl class
audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
audi a4 2.0 2008 4 auto(av) f 21 30 p compact
audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
audi a4 2.8 1999 6 manual(m5) f 18 26 p compact

Application

audi <- mpg %>% 
        filter(manufacturer == "audi") %>%
        lm(data = ., cty ~ year + cyl + displ) 

chevy <- mpg %>% 
        filter(manufacturer == "chevrolet") %>%
        lm(data = ., cty ~ year + cyl + displ) 

audi %>% summary
## 
## Call:
## lm(formula = cty ~ year + cyl + displ, data = .)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.3056 -0.6181 -0.2250  0.7736  2.7500 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## (Intercept) -468.2617   715.7159  -0.654    0.524
## year           0.2451     0.3559   0.689    0.502
## cyl            0.8083     6.4229   0.126    0.902
## displ         -3.6667    12.0577  -0.304    0.766
## 
## Residual standard error: 1.441 on 14 degrees of freedom
## Multiple R-squared:  0.5613, Adjusted R-squared:  0.4673 
## F-statistic:  5.97 on 3 and 14 DF,  p-value: 0.007711
audi$coefficients 
##  (Intercept)         year          cyl        displ 
## -468.2617284    0.2450617    0.8083333   -3.6666667
confint(audi)
##                     2.5 %      97.5 %
## (Intercept) -2003.3195699 1066.796113
## year           -0.5181988    1.008322
## cyl           -12.9674927   14.584159
## displ         -29.5279287   22.194595
paste0(audi$coefficients %>%
               round(2),
       ", (",
       confint(audi)[,1] %>%
               round(2),
       ", ",
       confint(audi)[,2] %>%
               round(2),
       ")")
## [1] "-468.26, (-2003.32, 1066.8)" "0.25, (-0.52, 1.01)"        
## [3] "0.81, (-12.97, 14.58)"       "-3.67, (-29.53, 22.19)"
summary(audi)$coefficients %>% 
        data.frame(check.names = F) %>% 
        .$`Pr(>|t|)` %>%
        round(., 4) %>%
        format(nsmall = 4)
## [1] "0.5235" "0.5023" "0.9016" "0.7655"
audi2 <- cbind (
        `Effect (95% CIs)` = 
               paste0(
                       audi$coefficients %>%
                               round(2),
                       ", (",
                       confint(audi)[,1] %>%
                               round(2),
                       ", ",
                       confint(audi)[,2] %>%
                               round(2),
                       ")"),
       `<i>p</i>-value` = summary(audi)$coefficients %>% 
               data.frame(check.names = F) %>% 
                .$`Pr(>|t|)` %>%
                round(4) %>%
               format(nsmall = 4),
       ` ` = ifelse(summary(audi)$coefficients %>% 
               data.frame(check.names = F) %>% 
                .$`Pr(>|t|)` %>%
                       as.numeric() < 0.05,
               "*",
               "")
       )


chevy2 <- cbind (
        `Effect (95% CIs)` = 
               paste0(
                       chevy$coefficients %>%
                               round(2),
                       ", (",
                       confint(chevy)[,1] %>%
                               round(2),
                       ", ",
                       confint(chevy)[,2] %>%
                               round(2),
                       ")"),
       `<i>p</i>-value` = summary(chevy)$coefficients %>% 
               data.frame(check.names = F) %>% 
                .$`Pr(>|t|)` %>%
                round(4) %>%
               format(nsmall = 4),
       ` ` = ifelse(summary(chevy)$coefficients %>% 
               data.frame(check.names = F) %>% 
                .$`Pr(>|t|)`  %>%
                       as.numeric() < 0.05,
               "*",
               "")
       )

data <- cbind(audi2, chevy2) %>% 
        as.data.frame()
        
data
##              Effect (95% CIs) <i>p</i>-value           Effect (95% CIs)
## 1 -468.26, (-2003.32, 1066.8)         0.5235   -1.95, (-387.77, 383.86)
## 2         0.25, (-0.52, 1.01)         0.5023        0.02, (-0.18, 0.21)
## 3       0.81, (-12.97, 14.58)         0.9016       -2.58, (-4.25, -0.9)
## 4      -3.67, (-29.53, 22.19)         0.7655        0.85, (-0.82, 2.52)
##   <i>p</i>-value  
## 1         0.9915  
## 2         0.8649  
## 3         0.0051 *
## 4         0.2939
row.names(data) <- c("Intercept",
                     "Year produced<sup>a</sup>",
                     "Number of cylinders",
                     "Displacement")

data 
##                                      Effect (95% CIs) <i>p</i>-value  
## Intercept                 -468.26, (-2003.32, 1066.8)         0.5235  
## Year produced<sup>a</sup>         0.25, (-0.52, 1.01)         0.5023  
## Number of cylinders             0.81, (-12.97, 14.58)         0.9016  
## Displacement                   -3.67, (-29.53, 22.19)         0.7655  
##                                   Effect (95% CIs) <i>p</i>-value  
## Intercept                 -1.95, (-387.77, 383.86)         0.9915  
## Year produced<sup>a</sup>      0.02, (-0.18, 0.21)         0.8649  
## Number of cylinders           -2.58, (-4.25, -0.9)         0.0051 *
## Displacement                   0.85, (-0.82, 2.52)         0.2939

https://blog.hubspot.com/website/how-to-bold-in-html#:~:text=To%20italicize%20the%20text%20in,style%20property%20set%20to%20italic.

data %>%
        kbl(format = "html", escape = 0) %>% 
        kable_styling(full_width = 0, html_font = "sans") %>%
        add_header_above(c(" " = 1, "Audi" = 3, "Chevrolet" = 3)) %>% 
        kable_styling(full_width = 0, html_font = "sans") 
Audi
Chevrolet
Effect (95% CIs) p-value Effect (95% CIs) p-value
Intercept -468.26, (-2003.32, 1066.8) 0.5235 -1.95, (-387.77, 383.86) 0.9915
Year produceda 0.25, (-0.52, 1.01) 0.5023 0.02, (-0.18, 0.21) 0.8649
Number of cylinders 0.81, (-12.97, 14.58) 0.9016 -2.58, (-4.25, -0.9) 0.0051
Displacement -3.67, (-29.53, 22.19) 0.7655 0.85, (-0.82, 2.52) 0.2939

Saving kbl file

setwd("Inha/5_Lectures/2024/Advanced metagenomics/scripts/IBS7048_Advanced_metagenomics/")


table <- data %>%
        kbl(format = "html", escape = 0) %>% 
        kable_styling(full_width = 0, html_font = "sans") %>%
        add_header_above(c(" " = 1, "Audi" = 3, "Chevrolet" = 3)) %>% 
        add_footnote("Only data from 1999 and 2008 were used.") %>%
        kable_styling(full_width = 0, html_font = "sans")
        
table
Audi
Chevrolet
Effect (95% CIs) p-value Effect (95% CIs) p-value
Intercept -468.26, (-2003.32, 1066.8) 0.5235 -1.95, (-387.77, 383.86) 0.9915
Year produceda 0.25, (-0.52, 1.01) 0.5023 0.02, (-0.18, 0.21) 0.8649
Number of cylinders 0.81, (-12.97, 14.58) 0.9016 -2.58, (-4.25, -0.9) 0.0051
Displacement -3.67, (-29.53, 22.19) 0.7655 0.85, (-0.82, 2.52) 0.2939
a Only data from 1999 and 2008 were used.
save_kable(table, "Figures/Table1.html")

End of lecture

Bibliography

## Computing. R Foundation for Statistical Computing, Vienna, Austria. <https://www.R-project.org/>. We have invested a lot of time and effort in creating R, please cite it when using it for data analysis. See also 'citation("pkgname")' for citing R packages.
## Grolemund G, Hayes A, Henry L, Hester J, Kuhn M, Pedersen TL, Miller E, Bache SM, Müller K, Ooms J, Robinson D, Seidel DP, Spinu V, Takahashi K, Vaughan D, Wilke C, Woo K, Yutani H (2019). "Welcome to the tidyverse." Journal of Open Source Software_, *4*(43), 1686. doi:10.21105/joss.01686 <https://doi.org/10.21105/joss.01686>.
## version 0.4.4, <https://CRAN.R-project.org/package=reactable>.
## J, reikoch, Beasley W, O'Connor B, Warnes GR, Quinn M, Kamvar ZN, Gao C (2024). yaml: Methods to Convert R Data to YAML and Back_. R package version 2.3.10, <https://CRAN.R-project.org/package=yaml>. ATTENTION: This citation information has been auto-generated from the package DESCRIPTION file and may need manual editing, see 'help("citation")'.
## R. version 0.5.0. Buffalo, New York. http://github.com/trinker/pacman