# ggplot2 ggplot2::theme_set(ggplot2::theme_gray(base_size = 16)) # set seed set.seed(1234) # fontawesome htmltools::tagList(rmarkdown::html_dependency_font_awesome()) # conflicted library(conflicted) conflict_prefer("filter", "dplyr") ## ----packages, echo=FALSE, message=FALSE, warning=FALSE--------- library(tidyverse) library(Tmisc) # quartet install.packages("remotes") remotes::install_github("rstudio-education/dsbox") library(dsbox) # dataset: student_survey 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)") ## ----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) ## --------------------------------------------------------------- str(student_survey) ## ----echo = FALSE, warning = FALSE------------------------------ ggplot(student_survey, aes(x = first_kiss)) + geom_histogram(binwidth = 1) + labs( title = "How old were you when you had your first kiss?", x = "Age (years)", y = NULL ) ## ----echo = FALSE, warning = FALSE------------------------------ ggplot(student_survey, aes(x = fb_visits_per_day)) + geom_histogram(binwidth = 1) + labs( title = "How many times do you go on Facebook per day?", x = "Number of times", y = NULL ) ## --------------------------------------------------------------- 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%"----