COD_20231129_MGK_IBS7048_week14_data_visualization_advanced

Minsik Kim

2023-11-29

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

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)) +
        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 = "top")

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")
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(ncol=5))

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 = "bottom", title.hjust = 1))

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(ncol=3, title.position = "bottom", title.hjust = 1, 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(ncol=3, title.position = "bottom", title.hjust = 1, keywidth = 0.2, keyheight = 0.2, title = "*Manufacturer*<sup>company</sup>")) +
        theme_classic() 

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(ncol=3, title.position = "bottom", title.hjust = 1, keywidth = 0.2, keyheight = 0.2, title = "*Manufacturer*<sup>company</sup>"))

### bw

mpg %>% 
        ggplot(aes(x = cty, y = hwy, col = manufacturer)) +
        theme_bw() +
        geom_point() +
        xlab("City mileage (MPG)") +
        ylab("Highway mileage (MPG)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(ncol=3, title.position = "bottom", title.hjust = 1, 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)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(ncol=3, title.position = "bottom", title.hjust = 1, 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(ncol=3, title.position = "bottom", title.hjust = 1, 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(ncol=3, title.position = "bottom", title.hjust = 1, keywidth = 0.2, keyheight = 0.2, title = "*Manufacturer*<sup>company</sup>")) +
        geom_smooth(method = "lm", aes(x = cty, y = hwy, col = NA))

### 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(ncol=3, title.position = "bottom", title.hjust = 1, 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(ncol=3, title.position = "bottom", title.hjust = 1, 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(ncol=3, title.position = "bottom", title.hjust = 1, 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)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        scale_color_manual(values = c("red", "blue", "grey", "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)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        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)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        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)") +
        theme(legend.title = element_markdown()) +
        guides(col=guide_legend(title = "Manufacturer", override.aes = list(size = 4))) +
        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 = "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'))

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(~cyl)

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")

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()

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)
        
                
        
)

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

pdf

#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

#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")


library(kableExtra)

kbl

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) 
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()
        


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

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")
        


save_kable(table, "Figures/Table1.html")

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>.
## R. version 0.5.0. Buffalo, New York. http://github.com/trinker/pacman
## J, reikoch, Beasley W, O'Connor B, Warnes GR, Quinn M, Kamvar ZN (2023). yaml: Methods to Convert R Data to YAML and Back_. R package version 2.3.7, <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")'.
## Generation in R. R package version 1.45, <https://yihui.org/knitr/>. Yihui Xie (2015) Dynamic Documents with R and knitr. 2nd edition. Chapman and Hall/CRC. ISBN 978-1498716963 Yihui Xie (2014) knitr: A Comprehensive Tool for Reproducible Research in R. In Victoria Stodden, Friedrich Leisch and Roger D. Peng, editors, Implementing Reproducible Computational Research. Chapman and Hall/CRC. ISBN 978-1466561595
## Atkins A, Wickham H, Cheng J, Chang W, Iannone R (2023). rmarkdown: Dynamic Documents for R. R package version 2.25, <https://github.com/rstudio/rmarkdown>. Xie Y, Allaire J, Grolemund G (2018). R Markdown: The Definitive Guide. Chapman and Hall/CRC, Boca Raton, Florida. ISBN 9781138359338, <https://bookdown.org/yihui/rmarkdown>. Xie Y, Dervieux C, Riederer E (2020). R Markdown Cookbook_. Chapman and Hall/CRC, Boca Raton, Florida. ISBN 9780367563837, <https://bookdown.org/yihui/rmarkdown-cookbook>.
## 'rmarkdown' Documents. R package version 1.0.4, <https://CRAN.R-project.org/package=rmdformats>.