## ----child = "../setup.Rmd"---------------------------------------------------- ## ----setup, include=FALSE------------------------------------------------------ # R options options( htmltools.dir.version = FALSE, dplyr.print_min = 6, dplyr.print_max = 6, tibble.width = 65, width = 65 ) # figure height, width, dpi knitr::opts_chunk$set(echo = TRUE, fig.width = 8, fig.asp = 0.618, out.width = "60%", fig.align = "center", dpi = 300, message = FALSE) # ggplot2 ggplot2::theme_set(ggplot2::theme_gray(base_size = 16)) # set seed set.seed(1234) # fontawesome htmltools::tagList(rmarkdown::html_dependency_font_awesome()) # magick dev.off <- function(){ invisible(grDevices::dev.off()) } # conflicted library(conflicted) conflict_prefer("filter", "dplyr") # xaringanExtra library(xaringanExtra) xaringanExtra::use_panelset() # output number of lines hook_output <- knitr::knit_hooks$get("output") knitr::knit_hooks$set(output = function(x, options) { lines <- options$output.lines if (is.null(lines)) { return(hook_output(x, options)) # pass to default hook } x <- unlist(strsplit(x, "\n")) more <- "..." if (length(lines)==1) { # first n lines if (length(x) > lines) { # truncate the output, but add .... x <- c(head(x, lines), more) } } else { x <- c(more, x[lines], more) } # paste these lines together x <- paste(c(x, ""), collapse = "\n") hook_output(x, options) }) ## ----packages, echo=FALSE, message=FALSE, warning=FALSE------------------------ library(tidyverse) library(magick) library(Tmisc) #library(dsbox) ## ----echo=FALSE, out.width="80%"----------------------------------------------- knitr::include_graphics("img/ggplot2-part-of-tidyverse.png") ## ----echo=FALSE, out.width="100%"---------------------------------------------- knitr::include_graphics("img/grammar-of-graphics.png") ## ------------------------------------------------------------------------------ glimpse(starwars) ## ----mass-height, fig.width = 8, out.width = "52%", warning=T------------------ ggplot(data = starwars, mapping = aes(x = height, y = mass)) + geom_point() + labs(title = "Mass vs. height of Starwars characters", x = "Height (cm)", y = "Weight (kg)") ## ----fig.width = 8, warning = FALSE, echo=FALSE, out.width = "50%"------------- ggplot(data = starwars, mapping = aes(x = height, y = mass)) + geom_point() + labs(title = "Mass vs. height of Starwars characters", x = "Height (cm)", y = "Weight (kg)") + geom_point(data = starwars %>% filter(name == "Jabba Desilijic Tiure"), size = 5, pch = 1, color = "pink", stroke = 3) ## ----echo = FALSE, warning = FALSE, cache = TRUE, out.width = "80%"------------ jabba <- image_read("img/jabba.png") fig <- image_graph(width = 1600, height = 900, res = 200) ggplot(data = starwars, mapping = aes(x = height, y = mass)) + geom_point() + labs(title = "Mass vs. height of Starwars characters", x = "Height (cm)", y = "Weight (kg)") + geom_point(data = starwars %>% filter(name == "Jabba Desilijic Tiure"), size = 5, pch = 1, color = "pink", stroke = 3) dev.off() out <- fig %>% image_composite(jabba, offset = "+1000+30") image_write(out, "img/jabbaplot.png", format = "png") knitr::include_graphics("img/jabbaplot.png") ## ----ref.label="mass-height", fig.show = "hide"-------------------------------- ## ----fig.width = 8, out.width = "52%"------------------------------------------ plot(starwars$height, starwars$mass, main = "Mass vs. height of Starwars characters", xlab = "Height (cm)", ylab = "Weight (kg)") ## ------------------------------------------------------------------------------ library(tidyverse) ## ----quartet-for-show, eval = FALSE, echo = FALSE------------------------------ # library(Tmisc) # quartet ## ----quartet-view1, echo = FALSE----------------------------------------------- quartet[1:22,] ## ----quartet-view2, echo = FALSE----------------------------------------------- quartet[23:44,] ## ----quartet-summary----------------------------------------------------------- quartet %>% group_by(set) %>% summarise( mean_x = mean(x), mean_y = mean(y), sd_x = sd(x), sd_y = sd(y), r = cor(x, y) ) ## ----quartet-plot, echo = T, out.width = "80%", fig.asp = 0.5------------------ ggplot(quartet, aes(x = x, y = y)) + geom_point() + facet_wrap(~ set, ncol = 4) ## ----quartet-R, fig.show='hide'------------------------------------------------ str(quartet) attach(quartet) par(mfrow=c(1,4)) for(j in levels(set)) plot(x[set==j], y[set==j], main=j) detach(quartet) ## ----ref.label="quartet-R", out.width = "80%", fig.asp = 0.5, echo=F, results='hide'---- ## ----out.width = "80%", fig.asp = 0.5------------------------------------------ coplot(y ~ x | set, data = quartet) ## ----echo=FALSE, out.width="80%"----------------------------------------------- knitr::include_graphics("img/penguins.png") ## ------------------------------------------------------------------------------ library(palmerpenguins) glimpse(penguins) ## ----ref.label = "penguins", echo = FALSE, warning = FALSE, out.width = "70%", fig.width = 8---- ## ----penguins, fig.show = "hide"----------------------------------------------- ggplot(data = penguins, mapping = aes(x = bill_depth_mm, y = bill_length_mm, colour = species)) + geom_point() + labs(title = "Bill depth and length", subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", x = "Bill depth (mm)", y = "Bill length (mm)", colour = "Species") ## ----penguins-0, fig.show = "hide", warning = FALSE---------------------------- ggplot(data = penguins) #<< ## ----ref.label = "penguins-0", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8---- ## ----penguins-1, fig.show = "hide", warning = FALSE---------------------------- ggplot(data = penguins, mapping = aes(x = bill_depth_mm)) #<< ## ----ref.label = "penguins-1", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8---- ## ----penguins-2, fig.show = "hide", warning = FALSE---------------------------- ggplot(data = penguins, mapping = aes(x = bill_depth_mm, y = bill_length_mm)) #<< ## ----ref.label = "penguins-2", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8---- ## ----penguins-3, fig.show = "hide", warning = FALSE---------------------------- ggplot(data = penguins, mapping = aes(x = bill_depth_mm, y = bill_length_mm)) + geom_point() #<< ## ----ref.label = "penguins-3", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8---- ## ----penguins-4, fig.show = "hide", warning = FALSE---------------------------- ggplot(data = penguins, mapping = aes(x = bill_depth_mm, y = bill_length_mm, colour = species)) + #<< geom_point() ## ----ref.label = "penguins-4", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8---- ## ----penguins-5, fig.show = "hide", warning = FALSE---------------------------- ggplot(data = penguins, mapping = aes(x = bill_depth_mm, y = bill_length_mm, colour = species)) + geom_point() + labs(title = "Bill depth and length") #<< ## ----ref.label = "penguins-5", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8---- ## ----penguins-6, fig.show = "hide", warning = FALSE---------------------------- ggplot(data = penguins, mapping = aes(x = bill_depth_mm, y = bill_length_mm, colour = species)) + geom_point() + labs(title = "Bill depth and length", subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins") #<< ## ----ref.label = "penguins-6", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8---- ## ----penguins-7, fig.show = "hide", warning = FALSE---------------------------- ggplot(data = penguins, mapping = aes(x = bill_depth_mm, y = bill_length_mm, colour = species)) + geom_point() + labs(title = "Bill depth and length", subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", x = "Bill depth (mm)", y = "Bill length (mm)") #<< ## ----ref.label = "penguins-7", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8---- ## ----penguins-8, fig.show = "hide", warning = FALSE---------------------------- ggplot(data = penguins, mapping = aes(x = bill_depth_mm, y = bill_length_mm, colour = species)) + geom_point() + labs(title = "Bill depth and length", subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", x = "Bill depth (mm)", y = "Bill length (mm)", colour = "Species") #<< ## ----ref.label = "penguins-8", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8---- ## ----penguins-9, fig.show = "hide", warning = FALSE---------------------------- ggplot(data = penguins, mapping = aes(x = bill_depth_mm, y = bill_length_mm, colour = species)) + geom_point() + labs(title = "Bill depth and length", subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", x = "Bill depth (mm)", y = "Bill length (mm)", colour = "Species", caption = "Source: Palmer Station LTER / palmerpenguins package") #<< ## ----ref.label = "penguins-9", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8---- ## ----penguins-10, fig.show = "hide", warning = FALSE--------------------------- ggplot(data = penguins, mapping = aes(x = bill_depth_mm, y = bill_length_mm, colour = species)) + geom_point() + labs(title = "Bill depth and length", subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", x = "Bill depth (mm)", y = "Bill length (mm)", colour = "Species", caption = "Source: Palmer Station LTER / palmerpenguins package") + scale_colour_viridis_d() #<< ## ----ref.label = "penguins-10", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8---- ## ----ref.label="penguins-10-nohighlight", echo = FALSE, warning = FALSE, out.width = "70%", fig.width = 8---- ## ----penguins-10-nohighlight, fig.show = "hide"-------------------------------- ggplot(data = penguins, mapping = aes(x = bill_depth_mm, y = bill_length_mm, colour = species)) + geom_point() + labs(title = "Bill depth and length", subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", x = "Bill depth (mm)", y = "Bill length (mm)", colour = "Species", caption = "Source: Palmer Station LTER / palmerpenguins package") + scale_colour_viridis_d() ## ----named-args, eval = FALSE-------------------------------------------------- # ggplot(data = penguins, # mapping = aes(x = bill_depth_mm, # y = bill_length_mm, # colour = species)) + # geom_point() + # scale_colour_viridis_d() ## ----not-named-args, eval = FALSE---------------------------------------------- # ggplot(penguins, # aes(x = bill_depth_mm, # y = bill_length_mm, # colour = species)) + # geom_point() + # scale_colour_viridis_d() ## ----colour, fig.show = "hide", warning = FALSE-------------------------------- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, colour = species)) + #<< geom_point() + scale_colour_viridis_d() ## ----ref.label = "colour", echo = FALSE, warning = FALSE, out.width = "100%"---- ## ----shape-species, fig.show = "hide", warning = FALSE------------------------- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, colour = species, shape = species)) + #<< geom_point() + scale_colour_viridis_d() ## ----ref.label = "shape-species", echo = FALSE, warning = FALSE, out.width = "100%"---- ## ----size, fig.show = "hide", warning = FALSE---------------------------------- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, colour = species, shape = species, size = body_mass_g)) + #<< geom_point() + scale_colour_viridis_d() ## ----ref.label = "size", echo = FALSE, warning = FALSE, out.width = "100%"----- ## ----alpha, fig.show = "hide", warning = FALSE--------------------------------- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, colour = species, shape = species, size = body_mass_g, alpha = flipper_length_mm)) + #<< geom_point() + scale_colour_viridis_d() ## ----ref.label = "alpha", echo = FALSE, warning = FALSE, out.width = "100%"---- ## ----warning = FALSE, out.width = "100%"--------------------------------------- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, size = body_mass_g, #<< alpha = flipper_length_mm)) + #<< geom_point() ## ----warning = FALSE, out.width = "100%"--------------------------------------- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm)) + geom_point(size = 2, alpha = 0.5) #<< ## ----ref.label = "facet", echo = FALSE, warning = FALSE, out.width = "70%"----- ## ----facet, fig.show = "hide"-------------------------------------------------- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm)) + geom_point() + facet_grid(species ~ island) #<< ## ----warning = FALSE----------------------------------------------------------- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm)) + geom_point() + facet_grid(species ~ sex) #<< ## ----warning = FALSE----------------------------------------------------------- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm)) + geom_point() + facet_grid(sex ~ species) #<< ## ----warning = FALSE, fig.asp = 0.5-------------------------------------------- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm)) + geom_point() + facet_wrap(~ species) #<< ## ----warning = FALSE, fig.asp = 0.5-------------------------------------------- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm)) + geom_point() + facet_grid(. ~ species) #<< ## ----warning = FALSE----------------------------------------------------------- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm)) + geom_point() + facet_wrap(~ species, ncol = 2) #<< ## ----facet-color-legend, fig.show = "hide", warning = FALSE-------------------- ggplot( penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) + #<< geom_point() + facet_grid(species ~ sex) + scale_color_viridis_d() #<< ## ----ref.label = "facet-color-legend", echo = FALSE, warning = FALSE, out.width = "100%"---- ## ----facet-color-no-legend, fig.show = "hide", warning = FALSE----------------- ggplot( penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) + geom_point() + facet_grid(species ~ sex) + scale_color_viridis_d() + guides(color = "none") #<< ## ----ref.label = "facet-color-no-legend", echo = FALSE, warning = FALSE, out.width = "100%"----